-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution1.js
51 lines (50 loc) · 995 Bytes
/
solution1.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
/**
*
* https://leetcode-cn.com/problems/max-consecutive-ones-iii/
*
*
* 1004. 最大连续1的个数 III
*
*
* Medium
*
* 滑动窗口算法
*
* 108ms 95.83%
* 38mb 72.22%
*/
const longestOnes = (A, K) => {
let oneCount = 0
let zeroCount = 0
let slow = 0
let fast = 0
const max = A.length
let ans = 0
while (fast < max && slow <= fast) {
const fastItem = A[fast]
const slowItem = A[slow]
if (zeroCount <= K) {
// 向前扩张
fast++
if (fastItem === 0) {
zeroCount++
} else {
oneCount++
}
} else {
// 向前缩减
if (slowItem === 0) {
zeroCount--
} else {
oneCount--
}
slow++
}
ans = Math.max(ans, oneCount)
}
// 边界情况处理
return ans + K > max ? max : ans + K
}
console.log(longestOnes([1,1,1,0,0,0,1,1,1,1,0], 2))
console.log(longestOnes([0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], 3))
console.log(longestOnes([0, 0, 0, 1], 4))