Skip to content

Commit 7a2917e

Browse files
authored
Create 1004-max-consecutive-ones-iii.js
1 parent 78faf32 commit 7a2917e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

1004-max-consecutive-ones-iii.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} A
3+
* @param {number} K
4+
* @return {number}
5+
*/
6+
const longestOnes = function (A, K) {
7+
let i = 0
8+
let j = 0
9+
const len = A.length
10+
while (j < len) {
11+
if (A[j] === 0) K--
12+
if (K < 0) {
13+
if (A[i] === 0) K++
14+
i++
15+
}
16+
j++
17+
}
18+
return j - i
19+
}

0 commit comments

Comments
 (0)