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.

Note:

  • The input array will only contain0and1.

  • The length of input array is a positive integer and will not exceed 10,000

Example

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

滑动窗口,找最长包括最多k个0的窗口长度,这里k是1

两种写法,外循环分别是右边界和左边界,用左边界的时候会多算count,需要减回去并且跳出循环

类似上面那题

Code

Last updated