[Medium] 209. Minimum Size Subarray Sum
Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0.
A subarray is a contiguous non-empty sequence of elements within an array.
Examples
Example 1:
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.
Example 2:
Input: target = 4, nums = [1,4,4]
Output: 1
Example 3:
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0
Constraints
1 <= target <= 10^91 <= nums.length <= 10^51 <= nums[i] <= 10^4
Thinking Process
- Prefix Sum + Binary Search:
- Good when you need to query multiple ranges
- O(n log n) time, O(n) space
- More complex but flexible
- The search space must shrink monotonically each step.
- Decide which half still satisfies the predicate, discard the other.
- Use
mid = left + (right - left) / 2to avoid overflow.
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Standard binary search (this problem) | O(log n) | O(1) | Sorted array, left <= right |
| Lower / upper bound | O(log n) | O(1) | First/last position, insert index |
| Binary search on rotated array | O(log n) | O(1) | Identify sorted half, discard other |
| Binary search on answer | O(n log M) | O(1) | Monotonic predicate over search space |
Solution
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
if(nums.empty()) return 0;
const int N = nums.size();
int rtn = INT_MAX;
vector<int> sums(N + 1, 0);
for(int i = 1; i <= N; i++) {
sums[i] = sums[i - 1] + nums[i - 1];
}
for(int i = 1; i <= N; i++) {
int currTarget = target + sums[i - 1];
auto it = lower_bound(sums.begin(), sums.end(), currTarget);
if(it != sums.end()) {
rtn = min(rtn, (int)(it - sums.begin()) - (i - 1));
}
}
return rtn == INT_MAX? 0 : rtn;
}
};
Solution Explanation
Approach: Standard binary search (this problem)
Key idea: 1. Prefix Sum + Binary Search:
How the code works:
- Prefix Sum + Binary Search:
- Good when you need to query multiple ranges
- O(n log n) time, O(n) space
- More complex but flexible
- The search space must shrink monotonically each step.
- Decide which half still satisfies the predicate, discard the other.
Walkthrough — input target = 7, nums = [2,3,1,2,4,3], expected output 2:
The subarray [4,3] has the minimal length under the problem constraint.
Common Mistakes
- Empty array:
nums = []→ return0 - No valid subarray:
nums = [1,1,1],target = 10→ return0 - Single element:
nums = [5],target = 5→ return1 - Entire array needed:
nums = [1,2,3],target = 6→ return3 -
First element:
nums = [10,1,1],target = 10→ return1 - Wrong binary search target: Forgetting to add
sums[i-1]to target - Index calculation: Wrong length calculation
(it - sums.begin()) - (i - 1) - Not checking bounds: Not checking if
it != sums.end() - Sliding window: Not shrinking window when sum >= target
- Return value: Returning
INT_MAXinstead of0when no solution
Related Problems
- LC 3: Longest Substring Without Repeating Characters - Sliding window pattern
- LC 76: Minimum Window Substring - Similar sliding window
- LC 209: Minimum Size Subarray Sum - This problem
- LC 862: Shortest Subarray with Sum at Least K - Similar with negative numbers
- LC 53: Maximum Subarray - Maximum sum subarray
Key Takeaways
- Prefix Sum + Binary Search:
- Good when you need to query multiple ranges
- O(n log n) time, O(n) space
- More complex but flexible
- Sliding Window:
- More intuitive and efficient
- O(n) time, O(1) space
- Preferred for single query problems
-
Monotonic Property: Prefix sums are non-decreasing (all positive), enabling binary search
- Window Shrinking: Once sum >= target, shrink from left to find minimum length
References
- LC 209: Minimum Size Subarray Sum on LeetCode
- LeetCode Discuss — LC 209: Minimum Size Subarray Sum
- LeetCode Editorial (may require premium)