Simple sliding window


  • 0
    F
    public class Solution {
        public double findMaxAverage(int[] nums, int k) {
            int sum = 0;
            for(int i = 0;i<k;i++){
                sum += nums[i];
            }
            int maxAvg = sum;
            int index = 0;
            for(int i = k;i<nums.length;i++){
                sum -= nums[index++];
                sum += nums[i];
                maxAvg = Math.max(maxAvg, sum);
            }
            return (double)maxAvg / k;
        }
    }
    

Log in to reply
 

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