Skip to main content
  1. LeetCode/

LeetCode 209: Minimum Size Subarray Sum (Sliding Window)

·1 min· ·
LeetCode Daily Medium Array Sliding Window Two Pointers Prefix Sum Binary-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: Medium First Attempt: 2025-08-09

  • Total time: 00:00.00

Intuition
#

This is a sliding window problem. We expand the right pointer to accumulate the window sum. Whenever the sum becomes greater than or equal to the target, we shrink the window from the left to minimize the length that still satisfies the condition.

Approach
#

  • Maintain a window [left, right] and a running window_sum.
  • For each right, add nums[right] to window_sum.
  • While window_sum ≥ target, update the answer with the current window length and move left forward, subtracting nums[left] from window_sum to try to shorten the window.
from typing import List

class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        left = 0
        window_sum = 0
        min_length = float("inf")

        for right, value in enumerate(nums):
            window_sum += value
            while window_sum >= target:
                min_length = min(min_length, right - left + 1)
                window_sum -= nums[left]
                left += 1

        return 0 if min_length == float("inf") else min_length

Complexity
#

  • Time: O(n)
  • Space: O(1)

Findings
#

  • Sliding window works because all numbers are positive; shrinking the window only decreases the sum.
  • Key invariant: when window_sum ≥ target, increment left to find the minimal valid window.
  • Alternative: prefix sums + binary search yields O(n log n), but sliding window is optimal here.

Encountered Problems
#

Related

LeetCode 1695: Maximum Erasure Value (Sliding Window)
·1 min
LeetCode Daily Medium Sliding Window Two Pointers Array Hashset
Sliding window with a set and running sum; two pointers to keep the subarray unique.
LeetCode 3439: Reschedule Meetings for Maximum Free Time (Sliding Window)
·3 mins
LeetCode Daily Medium Sliding Window Two Pointers Intervals Array
Sliding window over meetings; track total duration in a k-sized window and compute free span between boundaries.
LeetCode 30: Substring with Concatenation of All Words (Brute Force → Counting → Sliding Window)
·3 mins
LeetCode Daily Hard String Sliding Window Two Pointers Hash Map Frequency-Counter Brute-Force
Three approaches: brute force, counting optimization, and sliding window over word-length offsets.
LeetCode 1004: Max Consecutive Ones III
·3 mins
LeetCode Daily Medium Sliding Window Array Two Pointers Binary Array
LeetCode Problem Solving - Variable Size Sliding Window with Zero Counting
LeetCode 1052: Grumpy Bookstore Owner
·3 mins
LeetCode Daily Medium Sliding Window Array Optimization Fixed Window
LeetCode Problem Solving - Fixed Window with Customer Satisfaction Optimization
LeetCode 1658: Minimum Operations to Reduce X to Zero
·3 mins
LeetCode Daily Medium Sliding Window Array Reverse Thinking Prefix Sum
LeetCode Problem Solving - Reverse Thinking with Sliding Window