mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 18:07:32 +01:00
22 lines
470 B
Java
22 lines
470 B
Java
|
class Solution {
|
||
|
private long sumFirst(int[] nums, int k) {
|
||
|
long sum = 0;
|
||
|
for (var i = 0; i < k; ++i) {
|
||
|
sum += nums[i];
|
||
|
}
|
||
|
return sum;
|
||
|
}
|
||
|
|
||
|
public double findMaxAverage(int[] nums, int k) {
|
||
|
var runningSum = sumFirst(nums, k);
|
||
|
var foundMax = runningSum;
|
||
|
|
||
|
for (var i = k; i < nums.length; ++i) {
|
||
|
runningSum += nums[i] - nums[i - k];
|
||
|
foundMax = Math.max(foundMax, runningSum);
|
||
|
}
|
||
|
|
||
|
return (double) foundMax / k;
|
||
|
}
|
||
|
}
|