***Max Consecutive Ones (Leetcode 485)
Problem Link: https://leetcode.com/problems/max-consecutive-ones/
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
n=len(nums)
j=0
ans=0
c=0
for i in range(n):
if(nums[i]==0):
c+=1
while(c>0):
if(nums[j]==0):
c-=1
j+=1
else:
j+=1
ans=max(ans,i-j+1)
return ans
Last updated