diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 56d00ed..45b26f0 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -267,4 +267,5 @@ mod p887_super_egg_drop; mod p3200_maximum_height_of_a_triangle; mod p3194_minimum_average_of_smallest_and_largest_elements; mod p3193_count_the_number_of_inversions; -mod p3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i; \ No newline at end of file +mod p3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i; +mod p3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii; \ No newline at end of file diff --git a/src/problem/p3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii.rs b/src/problem/p3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii.rs new file mode 100644 index 0000000..8d98804 --- /dev/null +++ b/src/problem/p3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii.rs @@ -0,0 +1,48 @@ +/** + * [3192] Minimum Operations to Make Binary Array Elements Equal to One II + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn min_operations(nums: Vec) -> i32 { + let mut result = 0; + let mut change_time = 0; + + for i in 0..nums.len() { + let num = if change_time % 2 == 1 { + if nums[i] == 1 { + 0 + } else { + 1 + } + } else { + nums[i] + }; + + if num == 1 { + continue; + } + + change_time += 1; + result += 1; + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3192() { + assert_eq!(4, Solution::min_operations(vec![0, 1, 1, 0, 1])); + assert_eq!(1, Solution::min_operations(vec![1, 0, 0, 0])); + } +}