Max Consecutive Ones III
Find the longest run of 1s in an array of 0s and 1s when up to k of the 0s may be changed to 1s.
Given an array numskk
Example 1
Output: 6
Explanation: change the 0s at index 5 and index 10 into 1s, and index 5 to 10 is a run of six 1s.
Example 2
Output: 10
Explanation: change the 0s at index 4, 5 and 9 into 1s, and index 2 to 11 is a run of ten 1s.
The Idea
Picture a row of eleven light bulbs: on, on, on, off, off, off, on, on, on, on, off. Write on as 1 and off as 0, and you have Example 1. You may switch on at most two bulbs, because k = 2
Where should the two switches go? Switching on the bulbs at index 3 and 4 joins them to the three 1s on their left, so index 0 to 4 is a run of five. Switching on index 4 and 5 instead joins them to the four 1s at index 6 to 9, a run of six. Trying every choice of zeros works for eleven bulbs, but the choices multiply fast. Can we find the answer without choosing zeros at all?
Turn the question around and look at stretches instead. A stretch is a run of positions next to each other, such as index 4 to 9. It can become all 1s exactly when it holds at most kk
To search the stretches, use two markers, leftrightleftrightrightleft
In the row labelled right = 4, the window covers index 0 to 4. It holds 2 zeros, which is allowed, and its length is 5. In the row labelled right = 5, rightleft
In the row labelled right = 9, right
Moving one marker forward and pulling the other after it is called a sliding window. Both markers only ever move forward, so each position enters the window once and leaves it at most once.
Solution Approach
Turning that idea into code means settling three things: how to know the number of zeros in the window, when to move left
Counting the zeros in the window
How do we know how many zeros the window holds without looking through it every time? Keep a counter, zerosrightleft
Without the counter, the simple way is to try every start and every end and count the zeros in each. For an array of n numbers that is about n²/2 stretches, or 50 million for 10,000 numbers. The counter lets a single pass do the same job.
Shrinking the window
When should leftzeroskleftzerosright
Why is it safe never to move leftrightright
Recording the longest window
When is the window worth measuring? After the shrinking, it holds at most kright - left + 1best
Java Solution
The method longestOnes
- First, set three variables to 0:
, the start of the window;left , the number of 0s inside it; andzeros , the longest window seen so far.best - A
loop movesfor from the first index to the last. Whenright is 0, add one tonums[right] .zeros - Inside that loop, a
loop runs as long aswhile is greater thanzeros . Each time, ifk is 0, take one offnums[left] , then movezeros forward.left - The window from
toleft now holds at mostright zeros. Compare its length,k , withright - left + 1 and keep the larger.best - After
has reached the last index, returnright , the longest stretch that needed at mostbest changes.k
The maink = 0[1, 1, 0, 1, 1, 1]k = 0k
public class MaxConsecutiveOnesIII {
public static int longestOnes(int[] nums, int k) {
int left = 0;
int zeros = 0; // zeros in nums[left..right]
int best = 0;
for (int right = 0; right < nums.length; right++) {
if (nums[right] == 0) {
zeros++;
}
while (zeros > k) { // more zeros than flips: shrink
if (nums[left] == 0) {
zeros--;
}
left++;
}
best = Math.max(best, right - left + 1);
}
return best;
}
public static void main(String[] args) {
System.out.println(longestOnes(new int[] {1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0}, 2));
int[] b = {0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1};
System.out.println(longestOnes(b, 3));
System.out.println(longestOnes(new int[] {1, 1, 0, 1, 1, 1}, 0));
System.out.println(longestOnes(new int[] {0, 0, 0}, 0));
System.out.println(longestOnes(new int[] {0, 1, 0, 1}, 5));
}
}
Running it prints:
6
10
3
0
4
Example 1 gives 6 and Example 2 gives 10, as expected. In the third line, k = 0[1, 1, 0, 1, 1, 1]
The fourth line is 0. Every 0 in [0, 0, 0]leftk = 5[0, 1, 0, 1]
The whilezeros > kzeros >= k>=kk - 1
Complexity
The work is one pass of rightleft
- Time: O(n), because
visits each of the n numbers inright once.nums only moves forward, so theleft loop runs at most n times over the whole method, not n times per step ofwhile .right - Space: O(1). The method keeps only
,left ,right andzeros , however long the array is, and readsbest without copying it.nums
With k = 1
The same window works on letters too. In