Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 1004 · Variable Window – Longest

Max Consecutive Ones III

LeetCode 1004 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Longest window with ≤ k zeros.

நடுத்தரம்ArrayBinary SearchSliding WindowPrefix Sum
LeetCode இல் திற ↗

நான் Max Consecutive Ones III (LeetCode 1004) ஐ இப்படித் தீர்த்தேன் — Variable Window – Longest பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Flipping at most k zeros means: find the longest window containing at most k zeros. Grow the window to the right; when it holds too many zeros, slide the left edge.

Approach

  1. 1

    Count zeros in the window.

  2. 2

    If zeros > k, move l right (decrement if a zero leaves).

  3. 3

    Track the window length.

நான் எப்படித் தீர்த்தேன்

Sliding windowPythonO(n) time · O(1) space
class Solution:
    def longestOnes(self, nums: List[int], k: int) -> int:
        #sliding window and two pointer
        #count the number of zeroes in the current window
        #if it exceeds, shrink from left, take care of count of 0 - left most position
        l=0
        r=0
        cnt_zeros = 0
        max_length = float("-inf")
        while r<len(nums):
            cur = nums[r]
            if cur==0:
               cnt_zeros+=1
            if cnt_zeros>k:
                if nums[l]==0:
                    cnt_zeros-=1
                l+=1
            max_length=max(max_length,r-l+1)
            r+=1
        return max_length
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Max Consecutive Ones III — அடிக்கடி கேட்கப்படும் கேள்விகள்

Max Consecutive Ones III பின்னாலுள்ள intuition என்ன?

Flipping at most k zeros means: find the longest window containing at most k zeros. Grow the window to the right; when it holds too many zeros, slide the left edge.

Max Consecutive Ones III (LeetCode 1004) ஐ எப்படித் தீர்ப்பது?

1. Count zeros in the window. 2. If zeros > k, move l right (decrement if a zero leaves). 3. Track the window length.

Max Consecutive Ones III இன் time மற்றும் space complexity என்ன?

Time: O(n). Space: O(1). (Accepted approach: Sliding window.)

Max Consecutive Ones III இல் பொதுவான தவறுகள் என்ன?

The window never shrinks, only slides — that's fine because we only need the max length.