Find the length of the shortest contiguous subarray whose sum is at least target. Because every value is positive, a variable-size window grows on the right and shrinks on the left in a single linear pass.
Return the length of the shortest contiguous subarray of nums (all positive) whose sum is ≥ target, or 0 if none qualifies. For target=7, nums=[2,3,1,2,4,3] the answer is 2 — the subarray [4,3] sums to 7 and no shorter window reaches 7.
target, you can greedily shrink from the left to find the shortest window ending at the current right — no element ever needs to be re-added. One forward pass with two pointers suffices.left=0, sum=0, best=Infinity.right across the array, adding nums[right] to sum — this extends the window because more length means more sum.sum ≥ target, record right - left + 1 as a candidate minimum, then subtract nums[left] and advance left — squeezing out slack to find the tightest valid window.best, or 0 if it was never updated.sum ≥ target. If you shrink first and then measure, you log a window that may have already fallen below the target and report a wrong (too-long or invalid) length.Although the inner while nests inside the for, left only ever moves forward and never past right. Across the whole run each element is added once and removed once → 2n pointer moves total, i.e. O(n).
nums can contain negatives or zeros, the sum is no longer monotonic and shrinking is unsafe — switch to a prefix-sum + binary search or a monotonic-deque approach instead.7. Grow a window from the right; whenever its sum reaches the target, record the length and shrink from the left.1function minSubArrayLen(target: number, nums: number[]): number {2▶ let left = 0;3▶ let sum = 0;4▶ let best = Infinity;56 for (let right = 0; right < nums.length; right++) {7 sum += nums[right]; // grow the window8 while (sum >= target) { // window qualifies9 best = Math.min(best, right - left + 1);10 sum -= nums[left]; // shrink from the left11 left++;12 }13 }14 return best === Infinity ? 0 : best;15}
function minSubArrayLen(target: number, nums: number[]): number {
let left = 0;
let sum = 0;
let best = Infinity;
for (let right = 0; right < nums.length; right++) {
sum += nums[right]; // grow the window
while (sum >= target) { // window qualifies
best = Math.min(best, right - left + 1);
sum -= nums[left]; // shrink from the left
left++;
}
}
return best === Infinity ? 0 : best;
}left marks the window's start, sum is the running window sum, and best starts at Infinity so any real window beats it.right across the array, adding each nums[right] into sum. Positivity guarantees the sum only goes up as the window widens.right - left + 1 as a candidate minimum. Recording before removing keeps the measured window valid.nums[left] and advance left. The loop keeps shrinking while still ≥ target, so it finds the tightest window ending at this right.best was never lowered, no window ever reached the target → return 0; otherwise return the shortest length found.2n moves despite the nested loop. O(1) extra space.left index whenever you update best and slice [bestLeft, bestLeft + best) at the end.sum > target; the structure is identical.function minSubArrayLen(target: number, nums: number[]): number {
let left = 0;
let sum = 0;
let best = Infinity;
for (let right = 0; right < nums.length; right++) {
sum += nums[right]; // grow the window
while (sum >= target) { // window qualifies
best = Math.min(best, right - left + 1);
sum -= nums[left]; // shrink from the left
left++;
}
}
return best === Infinity ? 0 : best;
}2n moves despite the nested loop. O(1) extra space.Fix each starting index and extend rightward until the running sum reaches the target, tracking the shortest such window.
function minSubArrayLen(target: number, nums: number[]): number {
let best = Infinity;
for (let i = 0; i < nums.length; i++) {
let sum = 0;
for (let j = i; j < nums.length; j++) {
sum += nums[j];
if (sum >= target) {
best = Math.min(best, j - i + 1);
break; // shortest window for this start
}
}
}
return best === Infinity ? 0 : best;
}left backward.Build a strictly increasing prefix-sum array, then for each end binary-search the earliest start whose window still meets the target. This is the approach to reach for if values are not guaranteed positive only for the increasing-prefix trick.
function minSubArrayLen(target: number, nums: number[]): number {
const n = nums.length;
const prefix = new Array<number>(n + 1).fill(0);
for (let i = 0; i < n; i++) prefix[i + 1] = prefix[i] + nums[i];
let best = Infinity;
for (let end = 1; end <= n; end++) {
const need = prefix[end] - target; // want prefix[start] <= need
let lo = 0, hi = end; // find largest start with prefix[start] <= need
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (prefix[mid] <= need) lo = mid + 1;
else hi = mid;
}
if (lo - 1 >= 0 && prefix[lo - 1] <= need) {
best = Math.min(best, end - (lo - 1));
}
}
return best === Infinity ? 0 : best;
}| "shortest/longest contiguous subarray" | variable-size sliding window |
| all values positive + sum threshold | grow right, shrink left (monotonic) |
| record length right − left + 1 | candidate min before shrinking |
| negatives/zeros present | prefix sum + binary search instead |
let left = 0, sum = 0, best = Infinity;
for (let right = 0; right < nums.length; right++) {
sum += nums[right];
while (sum >= target) {
best = Math.min(best, right - left + 1);
sum -= nums[left];
left++;
}
}
return best === Infinity ? 0 : best;