Skip to content

Commit 53c0de4

Browse files
committed
Add problem 3133: Minimum Array End
1 parent 40cc97c commit 53c0de4

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,7 @@ pub mod problem_3120_count_the_number_of_special_characters_i;
20992099
pub mod problem_3121_count_the_number_of_special_characters_ii;
21002100
pub mod problem_3127_make_a_square_with_the_same_color;
21012101
pub mod problem_3131_find_the_integer_added_to_array_i;
2102+
pub mod problem_3133_minimum_array_end;
21022103
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21032104

21042105
#[cfg(test)]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn min_end(n: i32, x: i32) -> i64 {
7+
let mut extra = u64::from(n.cast_unsigned() - 1);
8+
let mut result = u64::from(x.cast_unsigned());
9+
let mut processed = 0;
10+
11+
while extra != 0 {
12+
processed += (result >> processed).trailing_ones();
13+
14+
let zeroes = (result >> processed).trailing_zeros();
15+
16+
result |= (extra & u64::unbounded_shl(1, zeroes).wrapping_sub(1)) << processed;
17+
extra = u64::unbounded_shr(extra, zeroes);
18+
processed += zeroes;
19+
}
20+
21+
result.cast_signed()
22+
}
23+
}
24+
25+
// ------------------------------------------------------ snip ------------------------------------------------------ //
26+
27+
impl super::Solution for Solution {
28+
fn min_end(n: i32, x: i32) -> i64 {
29+
Self::min_end(n, x)
30+
}
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
#[test]
36+
fn test_solution() {
37+
super::super::tests::run::<super::Solution>();
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod bit_manipulation;
2+
3+
pub trait Solution {
4+
fn min_end(n: i32, x: i32) -> i64;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [((3, 4), 6), ((2, 7), 15), ((2, 2), 3)];
13+
14+
for ((n, x), expected) in test_cases {
15+
assert_eq!(S::min_end(n, x), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)