Formatted question description: https://leetcode.ca/all/487.html
487. Max Consecutive Ones II Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at most one 0. Example 1: Input: [1,0,1,1,0] Output: 4 Explanation: Flip the first zero will get the the maximum number of consecutive 1s. After flipping, the maximum number of consecutive 1s is 4. Note: The input array will only contain 0 and 1. The length of input array is a positive integer and will not exceed 10,000 Follow up: What if the input numbers come in one by one as an infinite stream? In other words, you can't store all numbers coming from the stream as it's too large to hold in memory. Could you solve it efficiently?
What if the question says that it can be flipped k times?
It is best to use a general solution to deal with this kind of problem. A window
[left,right] can be maintained to hold at least k zeros. When it encounters 0, it accumulates the number of zero, and then judges if the number of 0 is greater than k at this time, then shifts the left boundary left to the right, and if the removed nums[left] is 0, then zero decrements by 1. If it is not greater than k, use the number of digits in the window to update res.
The above method cannot be used in the case of follow up, because nums[left] needs to access the previous number. We can save all the positions of 0 we encountered in a
queue, so that we know where to move when we need to move left.