Skip to content

Commit ba5d5e9

Browse files
committed
Create 0209-minimum-size-subarray-sum.js
1 parent 2d0e0cc commit ba5d5e9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number} target
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
var minSubArrayLen = function (target, nums) {
7+
let minLength = Infinity;
8+
let leftWindow = 0;
9+
let currentSum = 0;
10+
11+
for (let rightWindow = 0; rightWindow < nums.length; rightWindow++) {
12+
currentSum += nums[rightWindow];
13+
while (currentSum >= target) {
14+
minLength = Math.min(minLength, rightWindow - leftWindow + 1);
15+
currentSum -= nums[leftWindow];
16+
leftWindow++;
17+
}
18+
}
19+
return minLength === Infinity ? 0 : minLength;
20+
};

0 commit comments

Comments
 (0)