20240822 Finished

This commit is contained in:
jackfiled 2024-08-22 14:15:29 +08:00
parent 2203504fd8
commit df50420be7
2 changed files with 45 additions and 1 deletions

View File

@ -213,3 +213,4 @@ mod p551_student_attendance_record_i;
mod p552_student_attendance_record_ii; mod p552_student_attendance_record_ii;
mod p3154_find_number_of_ways_to_reach_the_k_th_stair; mod p3154_find_number_of_ways_to_reach_the_k_th_stair;
mod p3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k; mod p3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k;
mod p3133_minimum_array_end;

View File

@ -0,0 +1,43 @@
/**
* [3133] Minimum Array End
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn min_end(n: i32, x: i32) -> i64 {
let (n, x) = (n as i64, x as i64);
let mut result = x;
let bit_count = 128 - n.leading_zeros() - x.leading_zeros();
let mut pos = 0;
for i in 0..bit_count {
if (result >> i) & 1 == 0 {
if ((n - 1) >> pos) & 1 == 1 {
result |= 1 << i;
}
pos += 1;
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3133() {
assert_eq!(6, Solution::min_end(3, 4));
assert_eq!(15, Solution::min_end(2, 7));
}
}