Max Consecutive Ones II
Find the longest run of 1s in an array of 0s and 1s when you may turn one 0 into a 1.
You are given an array nums
Example 1
Output: 4
Explanation: change the first 0 to get [1, 1, 1, 1, 0].
Example 2
Output: 4
Explanation: changing either 0 gives a run of four 1s.
The Idea
Picture a row of six lamps, where 1 means on and 0 means off: on, off, on, on, off, on. You may switch on one lamp that is off, and you want the longest row of lit lamps side by side.
That is Example 2, [1, 0, 1, 1, 0, 1][1, 1, 1, 1, 0, 1][1, 0, 1, 1, 1, 1]
Trying each 0 in turn works, but it means counting the whole array again for every 0. Is there a way to see the answer without choosing the 0 first?
Look at what a good run is made of. The run from index 0 to 3 was 1, 0, 1, 1 before the change: a stretch of the array with exactly one 0 in it. The run from index 2 to 5 was 1, 1, 0, 1, again one 0. Changing that 0 turns the whole stretch into 1s. A stretch with no 0 at all is already a run of 1s. So the question becomes: what is the longest stretch of the array that holds at most one 0?
A stretch can be marked by two positions, leftright
In the first row, the window runs from left = 0right = 3right
In the third row, leftright
So rightleft
Solution Approach
Turning that idea into code means settling three things: how the window grows, how it shrinks when it holds two 0s, and when to measure it.
Why not try every 0
What would the simple way cost? For each 0, pretend it is a 1 and scan the array for the longest run. An array of n elements can hold up to n zeros, and each scan takes n steps, so this takes about n² steps. For 100,000 elements that is ten billion steps, while the window needs only about 200,000.
Growing the window
How does the window know how many 0s it holds? A counter, zerosrightzeros
Shrinking the window
What happens when zerosleftzeros
Measuring the window
When is the window measured? After every step of rightright - left + 1
This finds the best run because, for each rightleftrightright
Jumping left past the earlier 0 in one step
As an optional improvement, the code can remember the index of the last 0 it saw. When a new 0 arrives, left
Java Solution
The method findMaxConsecutiveOnes
- First, set up three integers:
, the start of the window, at 0;left , the number of 0s inside the window, at 0; andzeros , the longest window so far, at 0.best - Move
along the array with aright loop. Each step addsfor to the window, and adds one tonums[right] if it is a 0.zeros - Inside that loop, a
loop runs as long aswhile is more than 1. It takeszeros out of the window, lowersnums[left] if that element was a 0, and moveszeros one place right.left - With at most one 0 inside, compare the window length
withright - left + 1 and keep the larger.best - When
has passed the last element, returnright .best
The main
public class MaxConsecutiveOnesII {
public static int findMaxConsecutiveOnes(int[] nums) {
int left = 0;
int zeros = 0; // zeros inside the window nums[left..right]
int best = 0;
for (int right = 0; right < nums.length; right++) {
if (nums[right] == 0) {
zeros++;
}
while (zeros > 1) { // two zeros: shrink from the left
if (nums[left] == 0) {
zeros--;
}
left++;
}
best = Math.max(best, right - left + 1);
}
return best;
}
public static void main(String[] args) {
System.out.println(findMaxConsecutiveOnes(new int[] {1, 0, 1, 1, 0}));
System.out.println(findMaxConsecutiveOnes(new int[] {1, 0, 1, 1, 0, 1}));
System.out.println(findMaxConsecutiveOnes(new int[] {1, 0, 1, 1, 0, 1, 1}));
System.out.println(findMaxConsecutiveOnes(new int[] {1, 1, 1}));
System.out.println(findMaxConsecutiveOnes(new int[] {0, 0, 0}));
System.out.println(findMaxConsecutiveOnes(new int[] {}));
}
}
Running it prints:
4
4
5
3
1
0
Look at the first two lines: 4 for [1, 0, 1, 1, 0][1, 0, 1, 1, 0, 1]
The third line, 5, comes from [1, 0, 1, 1, 0, 1, 1]leftleft = right
The fourth line is 3: with no 0 to change, the whole array [1, 1, 1][0, 0, 0]forbest
Complexity
The work is one pass of rightleft
- Time: O(n), for an array of n numbers, because
takes n steps, and across the whole run theright loop moveswhile at most n steps in total, becauseleft only moves forward and never goes pastleft .right - Space: O(1). The method keeps only
,left ,right andzeros , however long the array is.best
When up to k zeros may be changed instead of one, only the limit in the while