https://leetcode.com/problems/maximum-average-subarray-i/#/description

求指定长度为 k 的子数组的最大均值,基本思路是用长度为k 的滑动窗口扫一遍原数组,然后记录最大子数组的MaxSum,最后返回MaxSum/k

可以优化的部分是滑动窗口内部求sum 的过程。思路是事先求出一个cache 数组,数组里每一项是原数组对应位置的累加和。有了cache 以后滑动窗口在每次滑动过程中,窗口内部的累加就不用重复计算了,可以用窗口末尾和窗口前的cache 值相减得到。

class Solution {
public:
double findMaxAverage(vector<int>& nums, int k) {
vector<int> cache{};
int acc = ;
for (int i = ; i < nums.size(); i++) {
acc += nums[i];
cache.push_back(acc);
} int start = ;
int maxSum = INT_MIN;
while (start + k - < nums.size()) {
int sum = cache.at(start + k) - cache.at(start);
if (sum > maxSum) maxSum = sum;
start++;
}
return ((double)maxSum / (double)k); }
};

这里用一个小trick 来处理窗口从0 开始的时候,因为要减去窗口之前的累加和,所以cache 第一个元素为0,后面的元素错位对齐原数组。

Maximum Average Subarray I的更多相关文章

  1. [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  2. Maximum Average Subarray

    Given an array with positive and negative numbers, find the maximum average subarray which length sh ...

  3. Maximum Average Subarray II LT644

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  4. leetcode644. Maximum Average Subarray II

    leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...

  5. 643. Maximum Average Subarray I 最大子数组的平均值

    [抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...

  6. [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  7. LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)

    643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...

  8. Maximum Average Subarray II

    Description Given an array with positive and negative numbers, find the maximum average subarray whi ...

  9. 【Leetcode_easy】643. Maximum Average Subarray I

    problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...

  10. LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

随机推荐

  1. JarvisOJ Misc shell流量分析

    分析一下shell流量,得到flag 看着一大推的数据记录头都大了,并没有什么wireshark的使用经验,开始胡搞 首先用notepad++打开,搜索flag字样找到了一个类似于python脚本的东 ...

  2. python发送smtp 邮件 图片

    #-*- coding: utf-8 -*- # python2 import os import time import random import smtplib from time import ...

  3. P4610 [COCI2011-2012#7] KAMPANJA

    题目背景 临近选举,总统要在城市1和城市2举行演讲.他乘汽车完成巡回演讲,从1出发,途中要经过城市2,最后必须回到城市1.特勤局对总统要经过的所有城市监控.为了使得费用最小,必须使得监控的城市最少.求 ...

  4. LoadRunner开发ftp协议接口之上传文件脚本

    Action() { //建立一个ftp对象 FTP ftp1=0; //建立FTP连接并登录 ftp_logon_ex(&ftp1,"ftpLogon", "U ...

  5. 洛谷 P2257 【YY的GCD】

    这道题还是和上一道[ZAP]有那么一点点的相似哈 题目大意 给定N, M,求1<=x<=N, 1<=y<=M且\(gcd(x, y)\)为质数的(x, y)有多少对 如果对莫比 ...

  6. codeforces-1139 (div2)

    A.如果第i个数字是偶数,总贡献就加上i #include <map> #include <set> #include <ctime> #include <c ...

  7. windows linux 文件编码转换

    查看文件编码在Linux中查看文件编码可以通过以下几种方式:1.在Vim中可以直接查看文件编码:set fileencoding即可显示文件编码格式.如果你只是想查看其它编码格式的文件或者想解决用Vi ...

  8. 分布式监控系统开发【day38】:报警策略队列处理(五)

    一.目录结构 二.报警策略队列处理 1.入口MonitorServer import os import sys if __name__ == "__main__": os.env ...

  9. django - 总结 - cnblog

    1.头像预览 -------方法1-------- 点击头像------>点击input img和input重合; img在label,input-->display:none $(&qu ...

  10. JS转换HTML转义符,编码及解码

    JS转换HTML转义符 //去掉html标签 function removeHtmlTab(tab) { return tab.replace(/<[^<>]+?>/g,'') ...