Skip to content

Commit 96e9e9d

Browse files
committed
Add problem 3192: Minimum Operations to Make Binary Array Elements Equal to One II
1 parent 1a4ad1c commit 96e9e9d

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,7 @@ pub mod problem_3185_count_pairs_that_form_a_complete_day_ii;
21292129
pub mod problem_3186_maximum_total_damage_with_spell_casting;
21302130
pub mod problem_3190_find_minimum_operations_to_make_all_elements_divisible_by_three;
21312131
pub mod problem_3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i;
2132+
pub mod problem_3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii;
21322133
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21332134

21342135
#[cfg(test)]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn min_operations(nums: Vec<i32>) -> i32 {
7+
let mut mask = 0_u32;
8+
let mut result = 0;
9+
10+
for num in nums {
11+
if num.cast_unsigned() == mask {
12+
mask ^= 1;
13+
result += 1;
14+
}
15+
}
16+
17+
result
18+
}
19+
}
20+
21+
// ------------------------------------------------------ snip ------------------------------------------------------ //
22+
23+
impl super::Solution for Solution {
24+
fn min_operations(nums: Vec<i32>) -> i32 {
25+
Self::min_operations(nums)
26+
}
27+
}
28+
29+
#[cfg(test)]
30+
mod tests {
31+
#[test]
32+
fn test_solution() {
33+
super::super::tests::run::<super::Solution>();
34+
}
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn min_operations(nums: Vec<i32>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(&[0, 1, 1, 0, 1] as &[_], 4), (&[1, 0, 0, 0], 1)];
13+
14+
for (nums, expected) in test_cases {
15+
assert_eq!(S::min_operations(nums.to_vec()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)