// var findMaxAverage = function(nums, k) {
// let max = -10000;
// for (let i =0; i < nums.length - k + 1; i++) {
// let currSum = 0;
// for (let j = 0; j < k; j++) {
// currSum += nums[i+j];
// }
// max = max > (currSum / k) ? max : (currSum / k);
// }
// return max;
// };
var findMaxAverage = function(nums, k) {
var sum = 0, max = 0;
for(var i=0;i<k;i++){
sum += nums[i];
}
max = sum;
for(var i=k,len=nums.length;i<len;i++){
sum += nums[i] - nums[i-k]; // 使用第i个去取代第i-k个元素 只用将两者的差值加入到和中
max = Math.max(max,sum);
}
return max/k;
};
// let nums = [1,12,-5,-6,50,3], k = 4;
let nums = [5], k = 1;
console.log(findMaxAverage(nums, k));
console