Welcome to Subscribe On Youtube
Question
Formatted question description: https://leetcode.ca/all/487.html
Given a binary array nums
, return the maximum number of consecutive 1
's in the array if you can flip at most one 0
.
Example 1:
Input: nums = [1,0,1,1,0] Output: 4 Explanation:  If we flip the first zero, nums becomes [1,1,1,1,0] and we have 4 consecutive ones.  If we flip the second zero, nums becomes [1,0,1,1,1] and we have 3 consecutive ones. The max number of consecutive ones is 4.
Example 2:
Input: nums = [1,0,1,1,0,1] Output: 4 Explanation:  If we flip the first zero, nums becomes [1,1,1,1,0,1] and we have 4 consecutive ones.  If we flip the second zero, nums becomes [1,0,1,1,1,1] and we have 4 consecutive ones. The max number of consecutive ones is 4.
Constraints:
1 <= nums.length <= 10^{5}
nums[i]
is either0
or1
.
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?
Algorithm
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.
Follow up
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.
Code

public class Max_Consecutive_Ones_II { public class Solution { public int findMaxConsecutiveOnes(int[] nums) { int res = 0, zero = 0, left = 0, k = 1; for (int right = 0; right < nums.length; ++right) { if (nums[right] == 0) ++zero; while (zero > k) { if (nums[left++] == 0) zero; } res = Math.max(res, right  left + 1); } return res; } } public class Solution_followup { public int findMaxConsecutiveOnes(int[] nums) { int res = 0, left = 0, k = 1; Queue<Integer> q = new LinkedList<>(); for (int right = 0; right < nums.length; ++right) { if (nums[right] == 0) q.offer(right); if (q.size() > k) { left = q.poll() + 1; } res = Math.max(res, right  left + 1); } return res; } } } ############ class Solution { public int findMaxConsecutiveOnes(int[] nums) { int l = 0, r = 0; int k = 1; while (r < nums.length) { if (nums[r++] == 0) { k; } if (k < 0 && nums[l++] == 0) { ++k; } } return r  l; } }

// OJ: https://leetcode.com/problems/maxconsecutiveonesii/ // Time: O(N) // Space: O(1) class Solution { public: int findMaxConsecutiveOnes(vector<int>& A) { int zero = 0, i = 0, j = 0, N = A.size(), ans = 0; while (j < N) { zero += A[j++] == 0; while (zero > 1) zero = A[i++] == 0; ans = max(ans, j  i); } return ans; } };

from collections import deque class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) > int: res, zero, left, k = 0, 0, 0, 1 for right in range(len(nums)): if nums[right] == 0: zero += 1 while zero > k: if nums[left] == 0: zero = 1 left += 1 res = max(res, right  left + 1) # maxcheck every for iteration return res class Solution: # followup, extra space def findMaxConsecutiveOnes(self, nums: List[int]) > int: res, left, k = 0, 0, 1 q = deque() for right in range(len(nums)): if nums[right] == 0: q.append(right) if len(q) > k: left = q.popleft() + 1 res = max(res, right  left + 1) # maxcheck every for iteration return res # maintain a sliding window of size 2, that keeps track of the counts. class Solution: # follow up, optimized def findMaxConsecutiveOnes(self, nums: List[int]) > int: count = 0 # Count of consecutive ones prev_count = 0 # Count of consecutive ones before a zero max_count = 0 # Maximum count of consecutive ones for num in nums: if num == 1: count += 1 prev_count += 1 else: count = prev_count + 1 prev_count = 0 max_count = max(max_count, count) # Reset the count and prev_count if we encounter two zeros in a row if num == 0 and prev_count == 0: count = 0 prev_count = 0 return max_count ############ class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) > int: l = r = 0 k = 1 while r < len(nums): if nums[r] == 0: k = 1 if k < 0: if nums[l] == 0: k += 1 l += 1 r += 1 return r  l ############ class Solution(object): def __init__(self): self.ans = 0 self.count = 0 self.lastCount = 0 def findMaxConsecutiveOnes(self, nums): """ :type nums: List[int] :rtype: int """ for num in nums: self.readNum(num) # stream the input return self.ans def readNum(self, num): """ :type nums: int """ if num == 1: self.count += 1 else: self.count = self.count  self.lastCount + 1 self.lastCount = self.count self.ans = max(self.ans, self.count)

func findMaxConsecutiveOnes(nums []int) int { l, r := 0, 0 k := 1 for ; r < len(nums); r++ { if nums[r] == 0 { k } if k < 0 { if nums[l] == 0 { k++ } l++ } } return r  l }