Very Simple Sliding window JAVA - Beats 82%


  • 0
    C
    class Solution {
        public double findMaxAverage(int[] nums, int k) {
            double sum = 0.0;
            double max = 0.0;
            double finalSum = 0.0;
            for(int i = 0; i < k; i++){
                sum += nums[i];
            }
            max = sum;
            for(int i = k; i < nums.length; i++){
                sum = sum - nums[i - k] + nums[i];
                if(max < sum){
                    max = sum;
                }
            }
            finalSum = max / k;
            return finalSum;
        }
    }
    
    

Log in to reply
 

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