3.19 Minimum Size Subarray Sum
3.19.1 Problem Metadata
- Platform: LeetCode
- Problem ID: 209
- Difficulty: Medium
- URL: https://leetcode.com/problems/minimum-size-subarray-sum/
- Tags: NeetCode 150
- Techniques: Sliding Window, Two Pointers, Array, Binary Search, Prefix Sum
3.19.2 Description
Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray whose sum is greater than or equal to target. If no such subarray exists, return 0.
3.19.3 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
3.19.5 Solution - Sliding Window
3.19.5.1 Walkthrough
This solution uses a dynamic sliding window approach with two pointers (l and r) that expand and contract to find the minimum subarray length.
Core Strategy:
- Expand the window: Move the right pointer
rforward and add elements to the running sum - Contract when valid: Once
sum >= target, try to shrink the window from the left while maintaining the validity - Track minimum: Record the smallest window size that satisfies the condition
Key Insight: Since all elements are positive, adding elements always increases the sum, and removing elements always decreases it. This monotonic property guarantees the sliding window technique works optimally.
Why \(r \geq l\) is Always Guaranteed:
The inner while loop condition sum >= target provides an implicit guarantee that \(r \geq l\):
- Initialization: Both pointers start at 0, so initially \(r == l\)
- Outer loop invariant: The right pointer
rmoves forward first (in the outerforloop), soris always ≥ the initiall - Inner loop protection:
- The
whileloop only executes whensum >= target - We subtract
nums[l]before incrementingl - If
sum >= target, it means the current window[l, r]contains at least one element (otherwise sum would be 0) - The loop stops when
sum < target, which happens when we’ve removed too many elements - At this point, the window still contains
[l, r]wherel <= r
- The
- Cannot overtake: If
lwere to catch up tor(i.e.,l == r), we have a single-element window. After removing it,sumwould become less thantarget(since all elements are positive and at least 1), causing thewhileloop to exit beforelincrements pastr
Mathematical Proof:
- For l to exceed r, we’d need l = r + 1
- This would mean the window is empty: [r+1, r]
- An empty window has sum = 0, which is < target (target ≥ 1)
- Therefore, the while condition sum >= target would be false
- The inner loop exits before l can overtake r
This invariant ensures \(r - l + 1\) is always a valid positive length.
3.19.5.2 Analysis
- Time Complexity: O(n)
- The outer loop iterates
ntimes (right pointer moves from 0 to n-1) - The inner loop also processes each element at most once (left pointer moves from 0 to n-1)
- Key observation: Each element is added to
sumexactly once (byr) and removed at most once (byl) - Total operations: O(n + n) = O(n)
- This is not O(n²) because the inner loop doesn’t reset; both pointers only move forward
- The outer loop iterates
- Space Complexity: O(1)
- Only uses a constant number of variables (
result,sum,l) - No additional data structures that grow with input size
- Only uses a constant number of variables (
3.19.5.3 Implementation Steps
- Initialize variables:
result = Integer.MAX_VALUEto track the minimum window lengthsum = 0to maintain the current window suml = 0as the left pointer
- Outer loop: Iterate
rfrom 0 ton-1(right pointer):- Add
nums[r]to the running sum - Inner loop: While
sum >= target(window is valid):- Subtract
nums[l]from sum (prepare to shrink) - Update
result = min(result, r - l + 1)(record current window size) - Increment
lto shrink the window from the left
- Subtract
- Add
- After the loop:
- If
resultis stillInteger.MAX_VALUE, no valid subarray exists → return 0 - Otherwise, return
result
- If
3.19.5.4 Code - Java
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int result = Integer.MAX_VALUE;
int sum = 0;
//left pointer
int l = 0;
//traverse array with a right pointer
for(int r = 0; r < nums.length; r++) {
sum += nums[r];
//sum is greater than or equal to target
while(sum >= target) {
sum -= nums[l];
//the inner loop condition makes sure r >= l
result = Math.min(result, r - l + 1);
l++;
}
}
if(result == Integer.MAX_VALUE) {
return 0;
} else {
return result;
}
}
}