Home
DSA Patterns
Sliding Window
Minimum Size Subarray Sum
Minimum Size Subarray Sum (LeetCode 209)
Minimum Size Subarray Sum (LeetCode 209): find the shortest subarray whose sum reaches a target in O(n) with a sliding window that grows on the right and shrinks from the left.
Problem Understanding
Minimum Size Subarray Sum: given an array of positive integers and a target, return the length of the shortest contiguous subarray whose sum is at least target, or 0 if there is none.
Example: target = 7, nums = [2, 3, 1, 2, 4, 3] returns 2: [4, 3].
The Intuition: Stretch, Then Squeeze
Because every number is positive, a window's sum goes up when it grows and down when it shrinks. That makes a variable-size sliding window work:
- Move
rightone step and addnums[right]tototal. - While
total ≥ target, the window is good enough: record its length, then removenums[left]and movelefton, looking for something shorter. - When the sum drops below the target, go back to growing.
Each number enters and leaves the window at most once, so the whole scan is O(n). Compare Longest Substring Without Repeating Characters, which shrinks for the opposite reason: to make an invalid window valid again.
Minimum Size Subarray Sum Visualization
SHORTEST WINDOW WITH SUM ≥ 7 · GREEN UNDERLINE = BEST SO FAR
EMPTY WINDOW
0
1
2
3
4
5
total
0
best length
:
Answer
:
Find the shortest window whose sum is at least 7. Start empty: total = 0
Next
Add nums[0] = 2: window 0..0, total = 2 < 7
nums
Target
The shaded cells are the current window L..R; the number just added or dropped is filled. The best window so far is underlined in green. Watch the window stretch until the sum is enough, then pull in from the left.
Minimum Size Subarray Sum Dry Run
Input: target = 7, [2, 3, 1, 2, 4, 3]
| Add | Window | Total | Shrink? | Best |
|---|---|---|---|---|
| 2, 3, 1 | 0..2 | 6 | no | - |
| 2 | 0..3 | 8 | record 4, drop 2 | 4 |
| 4 | 1..4 | 10 | record 4, drop 3; 7: record 3, drop 1 | 3 |
| 3 | 3..5 | 9 | record 3, drop 2; 7: record 2, drop 4 | 2 |
Return 2.
Minimum Size Subarray Sum LeetCode Solution
Variable-size sliding window: grow right, shrink left while the sum is enough.
Minimum Size Subarray Sum Code
1var minSubArrayLen = function(target, nums) {2 let best = Infinity, total = 0, left = 0;3 for (let right = 0; right < nums.length; right++) {4 total += nums[right];5 while (total >= target) {6 best = Math.min(best, right - left + 1);7 total -= nums[left++];8 }9 }10 return best === Infinity ? 0 : best;11};
Minimum Size Subarray Sum in Python, Java, C++, TypeScript and C
Minimum Size Subarray Sum in Python
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
best = float('inf')
total = left = 0
for right, x in enumerate(nums):
total += x
while total >= target:
best = min(best, right - left + 1)
total -= nums[left]
left += 1
return 0 if best == float('inf') else bestMinimum Size Subarray Sum in Java
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int best = Integer.MAX_VALUE, total = 0, left = 0;
for (int right = 0; right < nums.length; right++) {
total += nums[right];
while (total >= target) {
best = Math.min(best, right - left + 1);
total -= nums[left++];
}
}
return best == Integer.MAX_VALUE ? 0 : best;
}
}Minimum Size Subarray Sum in C++
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int best = INT_MAX, total = 0, left = 0;
for (int right = 0; right < (int)nums.size(); right++) {
total += nums[right];
while (total >= target) {
best = min(best, right - left + 1);
total -= nums[left++];
}
}
return best == INT_MAX ? 0 : best;
}
};Minimum Size Subarray Sum in TypeScript
function minSubArrayLen(target: number, nums: number[]): number {
let best = Infinity, total = 0, left = 0;
for (let right = 0; right < nums.length; right++) {
total += nums[right];
while (total >= target) {
best = Math.min(best, right - left + 1);
total -= nums[left++];
}
}
return best === Infinity ? 0 : best;
}Minimum Size Subarray Sum in C
int minSubArrayLen(int target, int* nums, int numsSize) {
int best = INT_MAX, total = 0, left = 0;
for (int right = 0; right < numsSize; right++) {
total += nums[right];
while (total >= target) {
if (right - left + 1 < best) best = right - left + 1;
total -= nums[left++];
}
}
return best == INT_MAX ? 0 : best;
}Edge Cases & Common Mistakes
- No window reaches the target: return 0, not infinity.
- A single number is enough: the answer is 1.
- Using
ifinstead ofwhileto shrink: the window may need to drop several numbers before it stops being enough. - Recording the length after shrinking: record it while the sum still reaches the target, before removing
nums[left].
Summary
With only positive numbers, a window's sum grows and shrinks predictably. Extend the window on the right; while its sum reaches the target, record its length and drop numbers from the left. Each number enters and leaves once: O(n) time, O(1) space.
More practice: DSA interview questions with answers · patterns cheat sheet
