From df50420be7953e3ce07c24f354f3ab44ac3e54eb Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 22 Aug 2024 14:15:29 +0800 Subject: [PATCH] 20240822 Finished --- src/problem/mod.rs | 3 +- src/problem/p3133_minimum_array_end.rs | 43 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/problem/p3133_minimum_array_end.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 5b39622..6d63cc6 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -212,4 +212,5 @@ mod p551_student_attendance_record_i; mod p552_student_attendance_record_ii; 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; \ No newline at end of file +mod p3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k; +mod p3133_minimum_array_end; \ No newline at end of file diff --git a/src/problem/p3133_minimum_array_end.rs b/src/problem/p3133_minimum_array_end.rs new file mode 100644 index 0000000..79e293c --- /dev/null +++ b/src/problem/p3133_minimum_array_end.rs @@ -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)); + } +}