Max Consecutive Ones III
Given a binary array nums and an integer k, you may flip at most k zeros to ones. Return the length of the longest contiguous subarray containing only ones after performing at most k flips.
Open official problem prompt ↗Find the longest stretch of the array that can be made all-ones by flipping no more than k of its zeros.
You are painting a fence and have only k patches of white paint for the dark boards. You want the longest continuous section of fence you can make fully white — slide a frame along the fence, and whenever it swallows a (k+1)th dark board, pull the back edge forward until you are back within your paint budget.
- Input
- nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
- Output
- 6
- Why
- Flipping the two zeros at indices 4 and 5 turns nums[4..9] = [0,0,1,1,1,1] into six consecutive ones, giving length 6; no window of length 7 works because every 7-element window contains at least three zeros.
1 <= nums.length <= 10^5nums[i] is 0 or 10 <= k <= nums.length