|
| 1 | +--- |
| 2 | +id: minimum-operations-to-make-binary-array-elements-equal-to-one-ii |
| 3 | +title: Minimum Operations to Make Binary Array Elements Equal to One II |
| 4 | +sidebar_label: Minimum Operations II |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Two Pointers |
| 8 | + - Greedy |
| 9 | + - C++ |
| 10 | + - Java |
| 11 | + - Python |
| 12 | +description: "This document provides solutions for the Minimum Operations to Make Binary Array Elements Equal to One II problem on LeetCode." |
| 13 | +--- |
| 14 | + |
| 15 | +## Problem Statement |
| 16 | + |
| 17 | +You are given a binary array `nums`. |
| 18 | + |
| 19 | +You can perform the following operation any number of times: choose two indices `L` and `R` such that `0 <= L, R < nums.length` and flip every `0` to `1` and every `1` to `0` in the subarray `nums[L...R]` (inclusive). |
| 20 | + |
| 21 | +Return the minimum number of operations needed to make `nums` contain only `1`s. |
| 22 | + |
| 23 | +**Example 1:** |
| 24 | + |
| 25 | +Input: `nums = [1,1,0,1]` |
| 26 | +Output: `1` |
| 27 | + |
| 28 | +Explanation: Flip the element at index 2 (0-indexed) to get `[1,1,1,1]`. |
| 29 | + |
| 30 | +**Example 2:** |
| 31 | + |
| 32 | +Input: `nums = [0,1,1,0]` |
| 33 | +Output: `2` |
| 34 | + |
| 35 | +Explanation: Flip the elements at index 0 and 3 (0-indexed) to get `[1,1,1,1]`. |
| 36 | + |
| 37 | +Constraints: |
| 38 | +- `1 <= nums.length <= 10^5` |
| 39 | +- `nums[i]` is either `0` or `1`. |
| 40 | + |
| 41 | +## Solutions |
| 42 | + |
| 43 | +### Approach |
| 44 | + |
| 45 | +The goal is to minimize the number of operations required to convert all elements of the array `nums` to `1`. The operations allowed involve flipping subarrays of `nums`. |
| 46 | + |
| 47 | +#### Intuition |
| 48 | + |
| 49 | +To achieve this efficiently: |
| 50 | +1. **Prefix Sum Calculation**: Compute the prefix sums of `nums` such that `prefix[i]` represents the number of `0`s in `nums[0...i-1]`. |
| 51 | + |
| 52 | +2. **Two Pointers Technique**: Use two pointers (`left` and `right`) to determine the range `[L, R]` where flipping would be most effective. This involves: |
| 53 | + - Calculating the total number of `0`s in `nums` initially. |
| 54 | + - Iteratively adjusting the range `[L, R]` to minimize the number of `0`s. |
| 55 | + |
| 56 | +#### Steps |
| 57 | + |
| 58 | +1. **Initialization**: Initialize `left` and `right` pointers at the start of the array. Compute the initial count of `0`s in `nums`. |
| 59 | + |
| 60 | +2. **Iterative Adjustment**: Iterate through possible ranges `[L, R]`: |
| 61 | + - Update the count of `0`s by including `nums[right]` and excluding `nums[left]`. |
| 62 | + - Update the minimum operations required based on the count of `0`s. |
| 63 | + |
| 64 | +3. **Edge Cases**: Handle arrays where all elements are initially `1`. |
| 65 | + |
| 66 | +#### Complexity |
| 67 | + |
| 68 | +- **Time Complexity**: `O(n)` where `n` is the length of `nums`. This is because we iterate through the array with two pointers. |
| 69 | +- **Space Complexity**: `O(1)` extra space for variables. |
| 70 | + |
| 71 | +### Implementation (Java) |
| 72 | + |
| 73 | +```java |
| 74 | +class Solution { |
| 75 | + public int minOperations(int[] nums) { |
| 76 | + int n = nums.length; |
| 77 | + int left = 0, right = 0; |
| 78 | + int countZeros = 0; |
| 79 | + int minOps = Integer.MAX_VALUE; |
| 80 | + |
| 81 | + for (int num : nums) { |
| 82 | + if (num == 0) { |
| 83 | + countZeros++; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + int currentOps = countZeros; |
| 88 | + |
| 89 | + while (right < n) { |
| 90 | + if (nums[right] == 0) { |
| 91 | + countZeros--; |
| 92 | + } |
| 93 | + right++; |
| 94 | + |
| 95 | + while (countZeros * 2 <= right - left) { |
| 96 | + if (nums[left] == 0) { |
| 97 | + countZeros++; |
| 98 | + } |
| 99 | + left++; |
| 100 | + } |
| 101 | + |
| 102 | + minOps = Math.min(minOps, currentOps); |
| 103 | + } |
| 104 | + |
| 105 | + return minOps == Integer.MAX_VALUE ? 0 : minOps; |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Implementation (Python) |
| 111 | + |
| 112 | +```python |
| 113 | +class Solution: |
| 114 | + def minOperations(self, nums): |
| 115 | + n = len(nums) |
| 116 | + left = 0 |
| 117 | + right = 0 |
| 118 | + countZeros = sum(1 for num in nums if num == 0) |
| 119 | + minOps = float('inf') |
| 120 | + currentOps = countZeros |
| 121 | + |
| 122 | + while right < n: |
| 123 | + if nums[right] == 0: |
| 124 | + countZeros -= 1 |
| 125 | + right += 1 |
| 126 | + |
| 127 | + while countZeros * 2 <= right - left: |
| 128 | + if nums[left] == 0: |
| 129 | + countZeros += 1 |
| 130 | + left += 1 |
| 131 | + |
| 132 | + minOps = min(minOps, currentOps) |
| 133 | + |
| 134 | + return minOps if minOps != float('inf') else 0 |
| 135 | +``` |
| 136 | + |
| 137 | +## Conclusion |
| 138 | +This implementation efficiently calculates the minimum operations required to convert the binary array nums into an array containing only 1s using a two-pointer technique. Adjustments can be made according to specific platform requirements or further customization needs. |
0 commit comments