Leetcode - 1004. Max Consecutive Ones III
Coding Test

Leetcode - 1004. Max Consecutive Ones III

일시불

문제

Loading...
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

풀이

슬라이딩 윈도우

class Solution:
    def longestOnes(self, nums: List[int], k: int) -> int:
        max_freq = res = 0
        count = 0

        left = right = 0
        while right < len(nums):
            if nums[right] == 1:
                count += 1
            max_freq = max(max_freq, count)

            if res < max_freq + k:
                res += 1
            else:
                if nums[left] == 1:
                    count -= 1
                left += 1

            right += 1

        return res