Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 209 · Variable Window – Shortest / Minimum

Minimum Size Subarray Sum

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

Positive numbers ⇒ expand then shrink.

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

நான் Minimum Size Subarray Sum (LeetCode 209) ஐ இப்படித் தீர்த்தேன் — Variable Window – Shortest / Minimum பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

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

Intuition

All numbers are positive, so extending a window only increases its sum and shrinking only decreases it. Expand right until the sum reaches the target, then shrink from the left while it still does, recording the shortest length.

Approach

  1. 1

    Add nums[r] to the sum.

  2. 2

    While sum ≥ target: record r − l + 1, subtract nums[l], l++.

  3. 3

    Return 0 if nothing qualified.

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

Sliding windowPythonO(n) time · O(1) space
class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        l=0
        r=0
        sum=0
        min_length = float("inf")
        while r<len(nums):
            sum+=nums[r]
            while sum>=target:
                min_length = min(min_length,r-l+1)
                sum-=nums[l]
                l+=1
            r+=1
        return 0 if min_length==float("inf") else min_length
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Minimum Size Subarray Sum — அடிக்கடி கேட்கப்படும் கேள்விகள்

Minimum Size Subarray Sum பின்னாலுள்ள intuition என்ன?

All numbers are positive, so extending a window only increases its sum and shrinking only decreases it. Expand right until the sum reaches the target, then shrink from the left while it still does, recording the shortest length.

Minimum Size Subarray Sum (LeetCode 209) ஐ எப்படித் தீர்ப்பது?

1. Add nums[r] to the sum. 2. While sum ≥ target: record r − l + 1, subtract nums[l], l++. 3. Return 0 if nothing qualified.

Minimum Size Subarray Sum இன் time மற்றும் space complexity என்ன?

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

Minimum Size Subarray Sum இல் பொதுவான தவறுகள் என்ன?

Sliding window relies on positivity — with negatives use prefix sums.