Repository navigation
Expand file tree
/
Copy pathNo209.minimum-size-subarray-sum.js
More file actions
72 lines (68 loc) · 1.63 KB
/
Copy pathNo209.minimum-size-subarray-sum.js
File metadata and controls
72 lines (68 loc) · 1.63 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Difficulty:
* Medium
*
* Desc:
* Given an array of n positive integers and a positive integer s,
* find the minimal length of a contiguous subarray of which the sum ≥ s.
* If there isn't one, return 0 instead.
*
* Example:
* Given the array [2,3,1,2,4,3] and s = 7,
* the subarray [4,3] has the minimal length under the problem constraint.
*
* More practice:
* If you have figured out the O(n) solution,
* try coding another solution of which the time complexity is O(n log n).
*/
/* ==================== solution 1 ==================== */
/**
* @param {number} s
* @param {number[]} nums
* @return {number}
*/
var minSubArrayLen_1 = function(s, nums) {
let sum;
let length = 0;
let result = Infinity;
for (let i = 0; i < nums.length; i += 1) {
sum = nums[i];
length = 1;
if (sum >= s) return length;
for (let j = i + 1; j < nums.length; j += 1) {
sum += nums[j];
length += 1;
if (sum >= s) {
if (length < result) result = length;
break;
}
}
if (sum < s) break;
}
return result === Infinity ? 0 : result;
};
/* ==================== solution 2 ==================== */
/**
* @param {number} s
* @param {number[]} nums
* @return {number}
*/
var minSubArrayLen_2 = function(s, nums) {
if (!nums.length) return 0
let i = 0
let j = 1
let sum = nums[i]
let result = Infinity
while (i < nums.length && j <= nums.length) {
if (sum < s) {
if (j === nums.length) break
sum += nums[j]
j += 1
} else {
result = Math.min(result, j - i)
sum -= nums[i]
i += 1
}
}
return result === Infinity ? 0 : result
}