Java


  • 0
    J

    public double findMaxAverage(int[] nums, int k) {
    double maxAverage = 0;
    if (nums.length < k) {
    return maxAverage;
    } else {
    for (int j = 0; j < k ; j++) {
    maxAverage += nums[j];
    }
    double average = maxAverage;
    for (int i = k; i < nums.length; i++) {
    average += nums[i];
    average -= nums[i-k];
    maxAverage = Math.max(average, maxAverage);
    }
    }
    return maxAverage/k;
    }


Log in to reply
 

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