6-line JavaScript solution


  • 0
    var findMaxAverage = function(nums, k) {
        let max = -Infinity;
        for (let i = 0, sum = 0; i < nums.length; i++) {
            sum += nums[i] - (nums[i - k] || 0);
            if (i >= k - 1) max = Math.max(max, sum);
        }
        return max / k;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.