Maximum Average Subarray I
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的更多相关文章
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- Maximum Average Subarray
Given an array with positive and negative numbers, find the maximum average subarray which length sh ...
- Maximum Average Subarray II LT644
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- leetcode644. Maximum Average Subarray II
leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...
- 643. Maximum Average Subarray I 最大子数组的平均值
[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)
643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...
- Maximum Average Subarray II
Description Given an array with positive and negative numbers, find the maximum average subarray whi ...
- 【Leetcode_easy】643. Maximum Average Subarray I
problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...
- LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
随机推荐
- python之内置函数(二)与匿名函数、递归函数初识
一.内置函数(二)1.和数据结构相关(24)列表和元祖(2)list:将一个可迭代对象转化成列表(如果是字典,默认将key作为列表的元素).tuple:将一个可迭代对象转化成元组(如果是字典,默认将k ...
- 设置QPushbutton按钮背景、鼠标滑过状态、鼠标点击后状态用法
1.1当要设置QPushbutton按钮背景,字体颜色,鼠标滑过状态,鼠标单击后状态时,可以用QSS来设置,具体的代码如下: QPushButton *allSelect = new QPus ...
- ERROR 1045 (28000): Access denied for user 'xxx'@'localhost' (using password: YES)【奇葩的bug】
# Bug描述 今天周末,在家里学点新技术,虽然公司分配的任务没有完成(滑稽滑稽) 我先创建了一个mysql数据库,用root用户创建一个新用户,毕竟项目中使用root是非常危险的,尤其是我这样的实 ...
- Python 正则处理_re模块
正则表达式 动机 文本处理成为计算机常见工作之一 对文本内容搜索,定位,提取是逻辑比较复杂的工作 为了快速方便的解决上述问题,产生了正则表达式技术 定义 文本的高级匹配模式, 提供搜索, 替换, 本质 ...
- python 实现聊天室
所用模块 asyncore 官方介绍, 源码 英文捉鸡点 这里 源码中可以看到其实本质上就对 select 以及 socket 的进一步封装 简单说明 Python的asyncore模块提供了以异步 ...
- 川普和习G-20会面为缓和中美贸易战提供了很大的机会
川普和习将于这周在Buenos Aires(阿根廷首都)会面,互相商讨虚弱经济全球化的最大威胁. 自从川普在今年七月第一次开始提高中国商品关税之后,对全球的投资者和逐渐削弱的经济活动来说,两位领导人可 ...
- jdbc批处理进行多条数据插入
package cn.linjun.demo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.S ...
- C++回顾day03---<模板>
一:函数模板 建立一个通用函数,其函数类型和形参类型不具体指定,用一个虚拟的类型来代表.这个通用函数就称为函数模板.凡是函数体相同的函数都可以用这个模板来代替,不必定义多个函数,只需要在模板中定义一次 ...
- Linux下 tftp 服务器的安装与使用
安装步骤: 1. 安装xinetd, tftp-hpa tftpd-hpa a. sudo apt-get install xinetd b. sudo apt-get install tftp- ...
- tex中pdf外链
\documentclass{article} \usepackage{hyperref} \begin{document} \href{run:d:/my folder/test.pdf}{This ...