***Max Consecutive Ones III (Leetcode 1004)
Problem Link: https://leetcode.com/problems/max-consecutive-ones-iii/
class Solution:
def longestOnes(self, nums: List[int], k: int) -> int:
n=len(nums)
j=0
ans=0
c=0
for i in range(n):
if(nums[i]==0):
c+=1
while(c>k):
if(nums[j]==0):
c-=1
j+=1
else:
j+=1
ans=max(ans,i-j+1)
return ans
Previous***Max Consecutive Ones IINext***Longest Subarray of 1's After Deleting One Element (Leetcode 1493)
Last updated