***Max Consecutive Ones II

Problem Link: https://www.lintcode.com/problem/883/

class Solution:
    """
    @param nums: a list of integer
    @return: return a integer, denote  the maximum number of consecutive 1s
    """
    def find_max_consecutive_ones(self, nums: List[int]) -> int:
        # write your code here
        n=len(nums)
        j=0
        c=0
        ans=0
        for i in range(n):
            if(nums[i]==0):
                c+=1
            while(c>1):
                if(nums[j]==0):
                    c-=1
                    j+=1
                else:
                    j+=1
            ans=max(ans,i-j+1)
        return ans

Last updated