Skip to content

Commit 5cc2f26

Browse files
committed
✅ [643] sliding window
1 parent 7524bd3 commit 5cc2f26

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

643/my_solution.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var findMaxAverage = function (nums, k) {
7+
let l = 0, len = nums.length, max = -Infinity
8+
9+
while ((l + k) <= len) {
10+
let tmpMax = 0;
11+
for (let i = l; i < (l + k); i++) {
12+
tmpMax += nums[i]
13+
}
14+
15+
console.log("tmpMax")
16+
console.log(tmpMax)
17+
18+
max = Math.max(max, tmpMax);
19+
20+
l++;
21+
}
22+
23+
console.log("max")
24+
console.log(max)
25+
26+
return max / k;
27+
};
28+
29+
let x =
30+
// findMaxAverage([1, 12, -5, -6, 50, 3], 4)
31+
findMaxAverage([-1], 1)
32+
33+
34+
console.log("Result")
35+
console.log(x)

643/solution.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var findMaxAverage = function (nums, k) {
7+
let l = 0, len = nums.length, max = -Infinity
8+
9+
while ((l + k) <= len) {
10+
let tmpMax = 0;
11+
for (let i = l; i < (l + k); i++) {
12+
tmpMax += nums[i]
13+
}
14+
15+
max = Math.max(max, tmpMax);
16+
17+
l++;
18+
}
19+
20+
return max / k;
21+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
- [530. Minimum Absolute Difference in BST](./530/)
9090
- [572. Subtree of Another Tree](./572/)
9191
- [605. Can Place Flowers](./605/)
92+
- [643. Maximum Average Subarray I](./643/)
9293
- [647. Palindromic Substrings](./647/)
9394
- [771. Jewels and Stones](./771/)
9495
- [714. Best Time to Buy and Sell Stock with Transaction Fee](./714/)
@@ -162,7 +163,7 @@ Batch create:
162163
NOTE: JS IS HERE
163164
-->
164165
```ssh
165-
chapter=1679 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
166+
chapter=643 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
166167
```
167168
> then you can use `x` for quick debug.
168169

0 commit comments

Comments
 (0)