643. Maximum Average Subarray I

来源:互联网 发布:mac系统如何倒退 编辑:程序博客网 时间:2024/06/06 02:06

题目:

Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value.

Example 1:

Input: [1,12,-5,-6,50,3], k = 4Output: 12.75Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75

Note:

  1. 1 <= k <= n <= 30,000.
  2. Elements of the given array will be in the range [-10,000, 10,000].
思路:

本题一开始简单直接的思路是利用两层for循环,但是结果会time limit,那是每层循环都会有重复计算项,那么本题建立一个长度为k的窗口,从左至右依次滑动一个数字,每次的和就是加上最新的一个数,减去最左边的数。

代码:

class Solution {public:    double findMaxAverage(vector<int>& nums, int k) {        double res = -10000*k;        double sumnum = accumulate(nums.begin(),nums.begin()+k,0);        res = res<sumnum?sumnum:res;        for(int i=k;i<nums.size();i++)        {            sumnum = sumnum+nums[i]-nums[i-k];            res = res<sumnum?sumnum:res;        }        return res/k*1.0;    }};


原创粉丝点击