1004. Max Consecutive Ones III
https://leetcode.com/problems/max-consecutive-ones-iii/
solution
滑动窗口中的window里不超过k个0的最长子串
class Solution:
    def longestOnes(self, nums: List[int], k: int) -> int:
        l = 0
        counter = collections.defaultdict(int)
        res = 0
        for r in range(len(nums)):
            counter[nums[r]] += 1
            while counter[0] > k:
                counter[nums[l]] -= 1
                l += 1
            res = max(res, r - l + 1)
        return res时间复杂度:O(n) 空间复杂度:O(1)
follow up
Previous3. Longest Substring Without Repeating CharactersNext1658. Minimum Operations to Reduce X to Zero
Last updated