Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
- # take a sliding window approach
- # sliding window approaches are based on expansion and contractions
- # when you find an ideal state - you expand
- # when you find a valid stte - you contract
- # ideal condition is decided by the number of zeros in the sliding window
- left = 0
- right = 0
- countZeroes = 0
- maxConsecutiveOnes = -1
- while (right < len(nums)):
- if nums[right]==0:
- countZeroes += 1
- if countZeroes >= 2:
- if nums[left]==0:
- countZeroes -= 1
- left += 1
- maxConsecutiveOnes = max(maxConsecutiveOnes, right-left+1)
- right += 1
- return maxConsecutiveOnes
Advertisement
Add Comment
Please, Sign In to add comment