Minimum Size Subarray Sum
Minimum Size Subarray Sum is LeetCode 209 (Medium). Given an array of positive integers nums and a number target, return the length of the shortest contiguous subarray whose sum is at least target. If no subarray reaches target, return 0.
- With up to 10⁵ elements, summing every subarray (O(n²)) is too slow.
- The problem adds a follow-up: after the O(n) solution, write one that runs in O(n log n).
- 1 <= target <= 10⁹
- 1 <= nums.length <= 10⁵
- 1 <= nums[i] <= 10⁴
Intuition
Because every number is positive, a window's sum only goes up when it grows and only goes down when it shrinks. That makes a window you can slide:
- Grow the right edge one element at a time.
- Shrink from the left while the sum is still at least
target, recording the length before each removal. Every window you shrink from is valid, and the last one before the sum drops is the shortest valid window ending at this right edge.
A start that was dropped never needs to come back: for a later right edge, the window from that start is longer than one already recorded. So each element enters once and leaves once, which gives O(n).
"Shortest (or longest) subarray whose sum meets a bound", with only non-negative numbers, is a variable-size sliding window. If negatives are allowed the window breaks, and it becomes Shortest Subarray with Sum at Least K (problem 862), which needs a monotonic deque.
Approach
Before reading on, decide what should happen to a window whose sum already reaches target when you look for something shorter. Then check that the left edge never needs to move back. Aim for O(n) time and O(1) space.
Two ways to find the shortest window
Grow on the right, shrink on the left while the sum is enough.
- Fastest: each element is added once and removed once.
- Needs positive numbers, which this problem guarantees.
For each start i, binary search the prefix sums for the first end where the sum reaches target.
- The follow-up answer: the problem asks for this running time.
- Fixed starts: handy when each start needs its own answer.
The sliding window is the answer to the main question and is traced below. The prefix sum version answers the O(n log n) follow-up.
Start with an empty window
Set left = 0, total = 0 and best = n + 1. Using n + 1 as "nothing found" works because no real subarray is longer than n.
Add the next element on the right
For each right, add nums[right] to total. The new element always joins, since every window considered from now on ends at right.
Record and shrink while the sum is enough
While total >= target, update best with right - left + 1, then subtract nums[left] and move left forward. The length is recorded first because the removal may push the sum below target.
Return 0 if nothing was found
If best is still n + 1, even the whole array falls short of target, so return 0. Otherwise return best.
Code in Python, C++ and Java
Prefix sums and binary search
prefix[j] is the sum of the first j numbers, and it only increases because every number is positive. The subarray from i to j - 1 reaches target when prefix[j] >= prefix[i] + target, so for each start i a binary search finds the first such j.
Common pitfalls
Shrinking with if instead of while
if total >= target:
best = min(best, right - left + 1)
total -= nums[left]; left += 1while total >= target:
best = min(best, right - left + 1)
total -= nums[left]; left += 1One big element can make several left elements unnecessary at once. For nums = [1, 1, 1, 1, 1, 8], target = 8, the answer is 1 (just the 8), but with if the window drops only one element per step. The 8 arrives last, so the code records the window of 6 and returns 6.
Returning n + 1 when nothing reaches target
return best
return best if best <= n else 0
The placeholder leaks out as an answer. For nums = [1, 1, 1, 1], target = 11, the code returns 5, a length longer than the array, instead of 0.
Edge cases
The window shrinks down to that single element, so best becomes 1, the smallest possible answer.
The window only becomes valid at the last element, and removing anything drops below target, so the answer is n.
Complexity
right passes each element once, and left only moves forward, so it also passes each element at most once. Only a few integers are stored. The sum fits in 32 bits: at most 10⁵ × 10⁴ = 10⁹.Minimum Size Subarray Sum FAQ
Why does the sliding window fail with negative numbers?
Shrinking assumes that removing an element lowers the sum. A negative number breaks that. For [84, -37, 32, 40, 95] with target = 167, the sum first reaches 167 with all five numbers; dropping the 84 falls to 130, so the window stops there and returns 5. Yet [32, 40, 95] sums to 167 with length 3. Shortest Subarray with Sum at Least K handles negatives with a monotonic deque of prefix sums.
Is this the same as Smallest Subarray With Sum Greater Than X?
Nearly. That version needs a sum strictly greater than x, so the loop condition becomes total > x. Everything else, including the window and the proof, stays the same.
How do I return the subarray itself instead of its length?
Save left and right whenever best improves. The saved pair marks the shortest window; if several share the length, this keeps the first one found.