-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path1004.js
More file actions
34 lines (32 loc) · 775 Bytes
/
Copy path1004.js
File metadata and controls
34 lines (32 loc) · 775 Bytes
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
const longestOnes = (arr, k) => {
let max = 0;
for (let i = 0; i < arr.length; ++i) {
let cur = 0;
for (let j = i, zero = 0; j < arr.length; ++j) {
if (arr[j] === 0 && ++zero > k) break;
++cur;
}
cur > max && (max = cur);
}
return max;
};
const longestOnes = (arr, k) => {
let max = 0;
for (let left = -1, right = 0; right < arr.length; ++right) {
if (arr[right] === 1 || --k >= 0) {
right - left > max && (max = right - left);
} else {
while (arr[++left] !== 0);
++k;
}
}
return max;
};
const longestOnes = (arr, k) => {
let left = -1;
for (let right = 0; right < arr.length; ++right) {
arr[right] === 0 && --k;
k < 0 && arr[++left] === 0 && ++k;
}
return arr.length - left - 1;
};