20241019 finished.

This commit is contained in:
jackfiled 2024-10-19 14:24:51 +08:00
parent be822927b6
commit a0c965b416
2 changed files with 50 additions and 1 deletions

View File

@ -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;
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;

View File

@ -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>) -> 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]));
}
}