3191. Minimum Operations to Make Binary Array Elements Equal to One I Medium

@problem@discussion
#Array#Bit Manipulation#Queue#Sliding Window#Prefix Sum



1/**
2 * [3191] Minimum Operations to Make Binary Array Elements Equal to One I
3 *
4 * You are given a <span data-keyword="binary-array">binary array</span> nums.
5 * You can do the following operation on the array any number of times (possibly zero):
6 * 
7 * 	Choose any 3 consecutive elements from the array and flip all of them.
8 * 
9 * Flipping an element means changing its value from 0 to 1, and from 1 to 0.
10 * Return the minimum number of operations required to make all elements in nums equal to 1. If it is impossible, return -1.
11 *  
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">nums = [0,1,1,1,0,0]</span>
15 * Output: <span class="example-io">3</span>
16 * Explanation:<br />
17 * We can do the following operations:
18 * 
19 * 	Choose the elements at indices 0, 1 and 2. The resulting array is nums = [<u>1</u>,<u>0</u>,<u>0</u>,1,0,0].
20 * 	Choose the elements at indices 1, 2 and 3. The resulting array is nums = [1,<u>1</u>,<u>1</u>,<u>0</u>,0,0].
21 * 	Choose the elements at indices 3, 4 and 5. The resulting array is nums = [1,1,1,<u>1</u>,<u>1</u>,<u>1</u>].
22 * </div>
23 * <strong class="example">Example 2:
24 * <div class="example-block">
25 * Input: <span class="example-io">nums = [0,1,1,1]</span>
26 * Output: <span class="example-io">-1</span>
27 * Explanation:<br />
28 * It is impossible to make all elements equal to 1.
29 * </div>
30 *  
31 * Constraints:
32 * 
33 * 	3 <= nums.length <= 10^5
34 * 	0 <= nums[i] <= 1
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/
40// discuss: https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn min_operations(nums: Vec<i32>) -> i32 {
46        0
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_3191() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.