A general C/C++ solution


  • 0

    Conception: Keep a k length slide window.

    double findMaxAverage(vector<int>& nums, int k) 
    {
    	double max = -10000.0;
    	double sum = 0.0;
    	int last = 0;
    	for (int i = 0; i < nums.size(); ++i)
    	{
    		sum += nums[i];
    		if (i < k - 1)
    			continue;
    		double t = sum / k;
    		max = t>max ? t : max;
    		sum -= nums[last++];
    	}
    	return max;
    }
    

Log in to reply
 

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