Advertisement
smj007

Max consective ones III

Apr 19th, 2025
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. class Solution:
  2.     def longestOnes(self, nums: List[int], k: int) -> int:
  3.        
  4.         left = 0
  5.         right = 0
  6.         count_zeros = 0
  7.         max_window = 0
  8.  
  9.         while right < len(nums):
  10.             # if encountered add the number of zeros seen
  11.             if nums[right] == 0:
  12.                 count_zeros += 1
  13.  
  14.             # shift left untill the number of zeros <=k
  15.             while count_zeros > k:
  16.                 if nums[left] == 0:
  17.                     count_zeros -= 1
  18.                 left += 1
  19.  
  20.             max_window = max(max_window, right - left + 1)
  21.             right += 1
  22.  
  23.         return max_window
  24.  
  25.            
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement