Leetcode

Leetcode 643. Maximum Average Su

2018-09-14  本文已影响1人  SnailTyan

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Maximum Average Subarray I

2. Solution

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

Reference

  1. https://leetcode.com/problems/maximum-average-subarray-i/description/
上一篇下一篇

猜你喜欢

热点阅读