-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path209.长度最小的子数组.js
56 lines (56 loc) · 1.25 KB
/
209.长度最小的子数组.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
50
51
52
53
54
55
56
/*
* @lc app=leetcode.cn id=209 lang=javascript
*
* [209] 长度最小的子数组
*
* https://leetcode-cn.com/problems/minimum-size-subarray-sum/description/
*
* algorithms
* Medium (39.37%)
* Likes: 116
* Dislikes: 0
* Total Accepted: 13.6K
* Total Submissions: 34.5K
* Testcase Example: '7\n[2,3,1,2,4,3]'
*
* 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。
*
* 示例:
*
* 输入: s = 7, nums = [2,3,1,2,4,3]
* 输出: 2
* 解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组。
*
*
* 进阶:
*
* 如果你已经完成了O(n) 时间复杂度的解法, 请尝试 O(n log n) 时间复杂度的解法。
*
*/
/**
* @param {number} s
* @param {number[]} nums
* @return {number}
*/
var minSubArrayLen = function(s, nums) {
let lp = 0,
rp = 0,
sum = 0,
n = 0;
while (lp < nums.length) {
sum += nums[rp];
if (sum >= s) {
const n0 = rp - lp + 1;
n = n > 0 ? Math.min(n0, n) : n0;
sum = 0;
rp = ++lp;
continue;
}
rp++;
if (rp > nums.length - 1) {
sum = 0;
rp = ++lp;
}
}
return n;
};