forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0209-minimum-size-subarray-sum.js
49 lines (42 loc) · 1.23 KB
/
0209-minimum-size-subarray-sum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 209. Minimum Size Subarray Sum
* https://leetcode.com/problems/minimum-size-subarray-sum/
* Difficulty: Medium
*
* 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 instead.
*/
/**
* @param {number} target
* @param {number[]} nums
* @return {number}
*/
var minSubArrayLen = function(target, nums) {
let result = Infinity;
for (let right = 0, sum = 0, left = 0; right < nums.length; right++) {
sum += nums[right];
while (sum >= target) {
result = Math.min(result, right - left + 1);
sum -= nums[left];
left++;
}
}
return result === Infinity ? 0 : result;
};
// var minSubArrayLen = function(target, nums) {
// let begin = 0
// let window_state = 0
// let result = Infinity;
// for (let end = 0; end < nums.length; end++) {
// window_state += nums[end]
// while (window_state >= target) {
// let window_size = end - begin + 1
// result = Math.min(result, window_size)
// window_state -= nums[begin]
// begin++
// }
// }
// if (result === Infinity) return 0
// return result
// };