-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum-size-subarray-sum.js
49 lines (40 loc) · 1.3 KB
/
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
// 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.
var minSubArrayLen = function (target, nums) {
let prevTotal = 0;
let lowestRunCount = 0;
for (let i = 0; i < nums.length; i++) {
prevTotal += nums[i];
lowestRunCount++;
if (prevTotal >= target) {
break;
}
}
if (prevTotal < target) {
return 0;
}
for (let i = 1; i < nums.length; i++) {
const currentTotal = prevTotal - nums[i - 1];
if (currentTotal >= target) {
lowestRunCount--;
prevTotal = currentTotal;
} else {
prevTotal = currentTotal + nums[i + lowestRunCount - 1];
}
}
return lowestRunCount;
};
//optimal solution
var minSubArrayLen = function (target, nums) {
let minLength = Infinity;
let currentSum = 0;
let start = 0;
for (let end = 0; end < nums.length; end++) {
currentSum += nums[end];
while (currentSum >= target) {
minLength = Math.min(minLength, end - start + 1);
currentSum -= nums[start];
start++;
}
}
return minLength === Infinity ? 0 : minLength;
};