The problem
You get a binary array nums (only 0s and 1s) and an integer k. You may flip at most k zeros to ones. Return the length of the longest run of consecutive 1s you can end up with.
Input: nums = [1, 0, 0, 1, 1, 0, 1], k = 1
Output: 4Flip the zero at index 5: indices 3 to 6 become 1, 1, 1, 1, a run of 4.
How to think about it
Start with brute force. Try every subarray, count its zeros, and keep the longest with at most k zeros: O(n²) even with running counts. You could also try every set of k zeros to flip, which is far worse.
Reframe the flips. You never need to decide which zeros to flip up front. A subarray can be turned into all 1s exactly when it contains at most k zeros: flip those and you’re done. So the problem becomes: find the longest subarray with at most k zeros.
The key insight: that condition suits a variable window. Grow the window to the right one element at a time, counting zeros. If the count goes over k, the window is invalid, and since any bigger window containing it is also invalid, the fix is to move the left edge right until a zero falls out. Every valid window you see is a candidate for the answer.
Which pattern is this? A variable-size sliding window with a counter. The tell is “longest subarray with at most k bad elements”.
Approach
- Keep
start = 0,zeros = 0,best = 0. - For each
i, ifnums[i]is 0, increasezeros. - While
zeros > k, movestartright, decreasingzeroswhen the element leaving is a 0. - Update
best = max(best, i - start + 1). - Return
best.
Solution
def longest_ones(nums, k):
start = 0; zeros = 0; best = 0
for i, x in enumerate(nums):
if x == 0: zeros += 1
while zeros > k: # too many flips needed
if nums[start] == 0: zeros -= 1
start += 1
best = max(best, i - start + 1)
return best
# longest_ones([1,1,1,0,0,0,1,1,1,1,0], 2) -> 6Dry run
On nums = [1, 0, 0, 1, 1, 0, 1], k = 1:
i |
x |
zeros after shrinking |
start |
Window length | best |
|---|---|---|---|---|---|
| 0 | 1 | 0 | 0 | 1 | 1 |
| 1 | 0 | 1 | 0 | 2 | 2 |
| 2 | 0 | 1 | 2 | 1 | 2 |
| 4 | 1 | 1 | 2 | 3 | 3 |
| 5 | 0 | 1 | 3 | 3 | 3 |
| 6 | 1 | 1 | 3 | 4 | 4 |
(At i = 3 the window is indices 2 to 3, length 2.) The answer is 4.
Complexity
- Time: O(n).
iandstarteach move right at most n times. - Space: O(1). Three integers.
Edge cases and pitfalls
k = 0: the answer is the longest existing run of 1s.kat least the number of zeros: the whole array is valid, so returnn.- All zeros with
k > 0returnsmin(k, n). - Off-by-one on length. The window
nums[start..i]has lengthi - start + 1. - Greedy “flip the next zero” without a window gets messy fast. Thinking in terms of “at most k zeros in the window” keeps it simple.
Related problems
- Longest Subarray of 1’s After Deleting One Element (LeetCode 1493): the same window with
k = 1, minus the deleted slot. - Longest Repeating Character Replacement (LeetCode 424): the multi-letter version of a replacement budget.
- Minimum Size Subarray Sum (LeetCode 209): the “shortest window” twin of this “longest window” template.
- 1.Best Time to Buy and Sell Stock (LeetCode 121)
- 2.Maximum Average Subarray I (LeetCode 643)
- 3.Longest Substring Without Repeating Characters (LeetCode 3)
- 4.Longest Repeating Character Replacement (LeetCode 424)
- 5.Permutation in String (LeetCode 567)
- 6.Find All Anagrams in a String (LeetCode 438)
- 7.Fruit Into Baskets (LeetCode 904)
- 8.Max Consecutive Ones III (LeetCode 1004)
- 9.Minimum Size Subarray Sum (LeetCode 209)
- 10.Longest Subarray of 1's After Deleting One Element (LeetCode 1493)
- 11.Sliding Window Maximum (LeetCode 239)
- 12.Minimum Window Substring (LeetCode 76)