https://leetcode.com/problems/maximum-average-subarray-i/#/description
找出连续k个数平均值最大的那个平均值,注意注释!
public class Solution {
public double findMaxAverage(int[] nums, int k) {
// Double.MIN_VALUE为double能表示的最小正数
double max = -Double.MAX_VALUE;
double sum = 0;
for (int i = 0; i < nums.length; i++) {
if (i < k) {
sum += nums[i];
} else {
max = Math.max(max, sum);
sum = sum - nums[i - k] + nums[i];
}
}
return Math.max(max, sum) / k;
}
}