3192. Minimum Operations to Make Binary Array Elements Equal to One II Medium
1/**
2 * [3192] Minimum Operations to Make Binary Array Elements Equal to One II
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 index i from the array and flip all the elements from index i to the end of the array.
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.
11 *
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">nums = [0,1,1,0,1]</span>
15 * Output: <span class="example-io">4</span>
16 * Explanation:<br />
17 * We can do the following operations:
18 *
19 * Choose the index i = 1<span class="example-io">. The resulting array will be nums = [0,<u>0</u>,<u>0</u>,<u>1</u>,<u>0</u>].</span>
20 * Choose the index i = 0<span class="example-io">. The resulting array will be nums = [<u>1</u>,<u>1</u>,<u>1</u>,<u>0</u>,<u>1</u>].</span>
21 * Choose the index i = 4<span class="example-io">. The resulting array will be nums = [1,1,1,0,<u>0</u>].</span>
22 * Choose the index i = 3<span class="example-io">. The resulting array will be nums = [1,1,1,<u>1</u>,<u>1</u>].</span>
23 * </div>
24 * <strong class="example">Example 2:
25 * <div class="example-block">
26 * Input: <span class="example-io">nums = [1,0,0,0]</span>
27 * Output: <span class="example-io">1</span>
28 * Explanation:<br />
29 * We can do the following operation:
30 *
31 * Choose the index i = 1<span class="example-io">. The resulting array will be nums = [1,<u>1</u>,<u>1</u>,<u>1</u>].</span>
32 * </div>
33 *
34 * Constraints:
35 *
36 * 1 <= nums.length <= 10^5
37 * 0 <= nums[i] <= 1
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-ii/
43// discuss: https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-ii/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn min_operations(nums: Vec<i32>) -> i32 {
49 0
50 }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_3192() {
61 }
62}
63
Back
© 2025 bowen.ge All Rights Reserved.