[
array
sliding window
]
BinarySearch 0711 Longest Sublist of 1s After K Sets
Problem statement
https://binarysearch.com/problems/Longest-Sublist-of-1s-After-K-Sets/
Solution
Equal to Leetcode 1004. Max Consecutive Ones III.
Complexity
It is O(n)
for time and O(1)
for space.
Code
class Solution:
def solve(self, nums, k):
beg, end, zeroes, ans = 0, 0, 0, 0
while end < len(nums):
if zeroes + (nums[end] == 0) <= k:
zeroes += nums[end] == 0
end += 1
ans = max(ans, end - beg)
else:
zeroes -= nums[beg] == 0
beg += 1
return ans