487. Max Consecutive Ones II
T: o(n)
S: O(1)```java
class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int left = 0;
        int right = 0;
        int zeroIdx = -1;
        int result = 0;
        while (right < nums.length) {
            int rightInt = nums[right];
            right++;
            if (rightInt == 0) {
                if (zeroIdx == -1) {
                    zeroIdx = right-1;
                } else {
                    left = zeroIdx+1;
                    zeroIdx = right-1;
                }
            }
            result = Math.max(result, right - left);
        }
        return result;
    }
}
/**
sliding win
T: o(n)
S: O(1)
if win only has one 0 -> it's a 1
zeroidx = 1
1,0,1,1,0
0,0, 0
idea, update zero's postion,
and left = zeroIdx+1; so our win only has 0 zero (flip to one)
       
 */
```Last updated
Was this helpful?