Smallest Subarray With Sum At Least Target
Solve this Problemnums and an integer target, find the length of the smallest contiguous subarray whose sum is greater than or equal to target. If no such subarray exists, return 0.
Extending a window from every start and checking the sum works, but it re-derives the same ground repeatedly. A sliding windowSliding WindowMaintaining a running result over a contiguous range that grows or shrinks one element at a time, instead of recomputing the result for every range from scratch. with a variable size does better here: expand the right edge until the sum is enough, then greedily shrink the left edge for as long as it stays enough — every subarray that reaches target gets checked, but each element is added and removed from the running sum at most once, giving O(n) instead of O(n²).
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ nums.length ≤ 10⁵ - ◆
1 ≤ nums[i] ≤ 10⁴ - ◆
1 ≤ target ≤ 10⁹
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public int smallestSubarrayWithSumAtLeastTarget(int[] nums, int target) { |
| 3 | int left = 0, sum = 0, minLen = Integer.MAX_VALUE; |
| 4 | for (int right = 0; right < nums.length; right++) { |
| 5 | sum += nums[right]; |
| 6 | while (sum >= target) { |
| 7 | minLen = Math.min(minLen, right - left + 1); |
| 8 | sum -= nums[left]; |
| 9 | left++; |
| 10 | } |
| 11 | } |
| 12 | return minLen == Integer.MAX_VALUE ? 0 : minLen; |
| 13 | } |
| 14 | } |
| 15 |
00infinityStart left at 0, sum at 0, and minLen at infinity — no window has been checked yet.
Approach & Solutions
Brute Force
BruteFor every possible starting index, extend the window to the right, accumulating a running sum, until that sum reaches the target — then record the window length and stop extending from this start (going further can only make it longer, never shorter). Correct, but every start re-walks ground a smarter approach could reuse.
O(n²)O(1)1class Solution {
2 public int smallestSubarrayWithSumAtLeastTarget(int[] nums, int target) {
3 int minLen = Integer.MAX_VALUE;
4 for (int i = 0; i < nums.length; i++) {
5 int sum = 0;
6 for (int j = i; j < nums.length; j++) {
7 sum += nums[j];
8 if (sum >= target) {
9 minLen = Math.min(minLen, j - i + 1);
10 break;
11 }
12 }
13 }
14 return minLen == Integer.MAX_VALUE ? 0 : minLen;
15 }
16}Optimal — Sliding Window
OptimalExpand a window from the right, accumulating a running sum. The moment that sum reaches the target, the window is valid — but a shorter one might still work, so greedily shrink from the left (subtracting as you go) for as long as the sum stays at or above target, recording the length at every valid point along the way.
O(n)O(1)1class Solution {
2 public int smallestSubarrayWithSumAtLeastTarget(int[] nums, int target) {
3 int left = 0, sum = 0, minLen = Integer.MAX_VALUE;
4 for (int right = 0; right < nums.length; right++) {
5 sum += nums[right];
6 while (sum >= target) {
7 minLen = Math.min(minLen, right - left + 1);
8 sum -= nums[left];
9 left++;
10 }
11 }
12 return minLen == Integer.MAX_VALUE ? 0 : minLen;
13 }
14}