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.