forked from rubythonode/javascript-problems-and-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum-average-subarray-i.js
More file actions
41 lines (36 loc) · 836 Bytes
/
maximum-average-subarray-i.js
File metadata and controls
41 lines (36 loc) · 836 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
35
36
37
38
39
40
41
/**
* Maximum Average Subarray I
*
* Given an array consisting of n integers, find the contiguous subarray of given length k
* that has the maximum average value. And you need to output the maximum average value.
*
* Example 1:
*
* Input: [1,12,-5,-6,50,3], k = 4
* Output: 12.75
*
* Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
*
* Note:
*
* - 1 <= k <= n <= 30,000.
* - Elements of the given array will be in the range [-10,000, 10,000].
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
const findMaxAverage = (nums, k) => {
let sum = 0;
let max = -Infinity;
for (let i = 0; i < nums.length; i++) {
sum += nums[i];
if (i >= k - 1) {
max = Math.max(max, sum / k);
sum -= nums[i - k + 1];
}
}
return max;
};
export { findMaxAverage };