Skip to content

Latest commit

 

History

History
48 lines (34 loc) · 1.15 KB

_1004. Max Consecutive Ones III.md

File metadata and controls

48 lines (34 loc) · 1.15 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : February 17, 2025

Last updated : February 17, 2025


Related Topics : Array, Binary Search, Sliding Window, Prefix Sum

Acceptance Rate : 65.29 %


Solutions

Python

class Solution:
    def longestOnes(self, nums: List[int], k: int) -> int:
        left, right = 0, 0
        zeros = 0

        maxx = 0
        for right, num in enumerate(nums) :
            if not num :
                zeros += 1

            if zeros > k :
                while left <= right and nums[left] != 0 :
                    left += 1
                zeros -= 1
                left += 1

            if maxx < right - left + 1 :
                maxx = right - left + 1

        return maxx