Max Consecutive Ones III
Pattern: Sliding window: variable size
Flipping at most k zeros means finding the longest window with at most k zeros. Grow the window, and shrink it from the left whenever it holds one zero too many.
The problem
You are given an array of 0s and 1s and a number k. You may flip at most k zeros into ones. Return the length of the
longest block of neighbouring ones you can then have. For example, [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0] with k = 2
gives 6, and [0, 0, 0] with k = 0 gives 0.
The function name longestOnes matches LeetCode. Use the link above for the exact statement and limits.
Think first
- After the flips, the answer is a block of ones. What did that block look like before flipping?
- Restate the question without the word "flip".
- When the window holds one zero too many, which edge should move?
Hints
Brute force, and why it fails
Try every block and count its zeros: O(n²) with a running count per start. The window is O(n).
The key insight
"Flip at most k zeros" is the same as "allow at most k zeros in the window". That rule only gets harder as the window grows, so the longest-window template applies directly.
Dry run
| zeros | 2 |
|---|---|
| best | 5 |
nums = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0] and k = 2. Grow the window. Up to index 4 it holds two zeros, which is allowed.
The plan
left = 0, zeros = 0, best = 0
for right from 0 to n - 1:
if nums[right] == 0: zeros = zeros + 1
while zeros > k:
if nums[left] == 0: zeros = zeros - 1
left = left + 1
best = max(best, right - left + 1)
return best
Complexity
- Time
O(n)- Space
O(1)
Each edge moves forwards at most n times. Only counters are stored.
Edge cases
k = 0: the longest block of ones already there.kat least the number of zeros: the whole array.- All zeros with
k = 0: 0.
I've built my logic, show the code
Java
class Solution {
public int longestOnes(int[] nums, int k) {
int left = 0, zeros = 0, best = 0;
for (int right = 0; right < nums.length; right++) {
if (nums[right] == 0) zeros++;
while (zeros > k) { // too many zeros to flip: shrink from the left
if (nums[left] == 0) zeros--;
left++;
}
best = Math.max(best, right - left + 1);
}
return best;
}
}C++
class Solution {
public:
int longestOnes(vector<int>& nums, int k) {
int left = 0, zeros = 0, best = 0;
for (int right = 0; right < static_cast<int>(nums.size()); right++) {
if (nums[right] == 0) zeros++;
while (zeros > k) { // too many zeros to flip: shrink from the left
if (nums[left] == 0) zeros--;
left++;
}
best = max(best, right - left + 1);
}
return best;
}
};Python
class Solution:
def longestOnes(self, nums: list[int], k: int) -> int:
left = zeros = best = 0
for right, v in enumerate(nums):
if v == 0:
zeros += 1
while zeros > k: # too many zeros to flip: shrink from the left
if nums[left] == 0:
zeros -= 1
left += 1
best = max(best, right - left + 1)
return bestCommon mistakes
- Actually flipping zeros in the array and trying to undo them later.
- Moving
leftonly once when the rule breaks. If the element it drops is a 1, the window still has too many zeros, so keep moving until a zero leaves. - Counting ones instead of zeros, which does not tell you when the rule breaks.