Maximum Average Subarray I

2017/12/1 posted in  leetcode

解决方法:滑动窗口方案

class Solution {
public:
    double findMaxAverage(vector<int>& nums, int k) {
        double r = INT_MIN;
        double sum = 0;
        for (int i = 0; i<nums.size(); i++) {
            if (i<k) {
                sum+=nums[i];
            }else{
                r = max(sum, r);
                sum +=nums[i]-nums[i-k];
            }
        }
        r = max(sum, r);
        return r/double(k);
    }
};