forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0643-maximum-average-subarray-i.js
63 lines (53 loc) · 1.56 KB
/
0643-maximum-average-subarray-i.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
52
53
54
55
56
57
58
59
60
61
62
63
/**
* 643. Maximum Average Subarray I
* https://leetcode.com/problems/maximum-average-subarray-i/
* Difficulty: Easy
*
* You are given an integer array nums consisting of n elements, and an integer k.
*
* Find a contiguous subarray whose length is equal to k that has the maximum average
* value and return this value. Any answer with a calculation error less than 10-5
* will be accepted.
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var findMaxAverage = function(nums, k) {
let sum = 0;
for (let i = 0; i < k; i++) {
sum += nums[i];
}
let max = sum;
for (let i = k; i < nums.length; i++) {
sum = sum - nums[i - k] + nums[i];
max = Math.max(max, sum);
}
return max / k;
};
// gpt
// var findMaxAverage = function (nums, k) {
// let maxSum = nums.slice(0, k).reduce((acc, num) => acc + num, 0);
// let windowSum = maxSum;
// for (let end = k; end < nums.length; end++) {
// windowSum += nums[end] - nums[end - k];
// maxSum = Math.max(maxSum, windowSum);
// }
// return maxSum / k;
// };
// var findMaxAverage = function(nums, k) {
// let begin = 0; // Begin
// let window_state = 0; // WindowState
// let result = -Infinity;
// for (let end = 0; end < nums.length; end++) {
// window_state += nums[end];
// // end - begin = window size
// if (end - begin + 1 === k) { // Window condition
// result = Math.max(result, window_state);
// window_state -= nums[begin];
// begin++; // Shrink window
// }
// }
// return result/k;
// };