Skip to content

Commit 15aa0ea

Browse files
committed
✅ [1004] sliding window WOOHOOO
1 parent 527c447 commit 15aa0ea

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

1004/my_solution.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var longestOnes = function (nums, k) {
7+
let max = 0, tmpMax = 0, tmpK = k, acc = [], zeroIdxes = [], tmpSum = 0, l = 0, r = 0
8+
9+
// for (let i = 0; i < nums.length; i++) {
10+
// if (0 === nums[i]) {
11+
// console.log(`i: ${i}, nums[i]: ${nums[i]}`)
12+
// zeroIdxes.push(i)
13+
// }
14+
// tmpSum += nums[i]
15+
// acc[i] = tmpSum
16+
// }
17+
18+
while (r < nums.length) {
19+
tmpMax++;
20+
21+
console.log(`l: ${l}, r: ${r}, tmpK: ${tmpK}, tmpMax: ${tmpMax}`)
22+
if (nums[r] === 0) {
23+
tmpK--;
24+
25+
if (tmpK < 0) {
26+
let idx = l
27+
28+
while (tmpK < 0) {
29+
console.log(`-> tmpK:${tmpK}, nums[idx]: ${nums[idx]}`)
30+
31+
if (nums[idx] === 0) {
32+
l = idx + 1;
33+
tmpK++;
34+
break;
35+
}
36+
37+
idx++;
38+
}
39+
40+
console.log(`--> tmpMax:${tmpMax}, tmpK: ${tmpK}`)
41+
tmpMax = r - l + 1
42+
console.log(`---> tmpMax:${tmpMax}, tmpK: ${tmpK}`)
43+
}
44+
// else {
45+
// // meet 0 and has credit to deduct
46+
// if (tmpK > 0) tmpK--;
47+
// }
48+
}
49+
50+
max = Math.max(max, tmpMax)
51+
52+
r++;
53+
}
54+
55+
// console.log(zeroIdxes)
56+
// console.log(acc)
57+
// console.log(zeroIdxes.length - k - 1)
58+
59+
// let left = acc[acc.length - 1] - acc[zeroIdxes[zeroIdxes.length - k - 1]]
60+
// console.log("left")
61+
// console.log(left)
62+
console.log("max")
63+
console.log(max)
64+
65+
return max;
66+
};
67+
68+
let x =
69+
// longestOnes([1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], 2) // 6
70+
longestOnes([0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1], 2) // 6
71+
// longestOnes([0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1], 3) // 10
72+
73+
console.log("Res")
74+
console.log(x)

1004/solution.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var longestOnes = function (nums, k) {
7+
let max = 0, tmpMax = 0, l = 0, r = 0
8+
9+
while (r < nums.length) {
10+
tmpMax++;
11+
12+
if (nums[r] === 0) {
13+
k--;
14+
15+
if (k < 0) {
16+
17+
while (k < 0) {
18+
if (nums[l] === 0) {
19+
l++; // the next one
20+
k++;
21+
break;
22+
}
23+
24+
l++;
25+
}
26+
27+
tmpMax = r - l + 1 // the new tmpMax will be r - l + 1 that is the size of the window
28+
}
29+
}
30+
31+
max = Math.max(max, tmpMax)
32+
r++;
33+
}
34+
35+
return max;
36+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
- [832. Flipping an Image](./832/)
9797
- [876. Middle of the Linked List](./876/)
9898
- [997. Find the Town Judge](./997/)
99+
- [1004. Max Consecutive Ones III](./1004/)
99100
- [1027. Longest Arithmetic Subsequence](./1027/)
100101
- [1071. Greatest Common Divisor of Strings](./1071/)
101102
- [1161. Maximum Level Sum of a Binary Tree](./1161/)
@@ -164,7 +165,7 @@ Batch create:
164165
NOTE: JS IS HERE
165166
-->
166167
```ssh
167-
chapter=1456 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
168+
chapter=1004 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
168169
```
169170
> then you can use `x` for quick debug.
170171

0 commit comments

Comments
 (0)