【Leetcode_easy】643. Maximum Average Subarray I
problem
643. Maximum Average Subarray I
题意:一定长度的子数组的最大平均值。
solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度的子数组之和,迭代更新得到最大值。
注意:1)累加数组;2)数值类型;
class Solution {
public:
double findMaxAverage(vector<int>& nums, int k) {
vector<int> sum(nums.size(), );
//vector<int> sum = nums;
for(int i=; i<nums.size(); ++i)//err.
{
if(i==) sum[] = nums[i];
else sum[i] = sum[i-] + nums[i];//err.
}
double mx = sum[k-]; for(int i=; i<nums.size()-k; ++i)
{
mx = max(mx, (double)sum[i+k]-sum[i]);//err.
}
/*
for(int i=k; i<nums.size(); ++i)
{
mx = max(mx, (double)sum[i]-sum[i-k]);
}
*/
return mx/k;
}
};
solution2:
子数组的长度k是确定的,所以其实没有必要建立整个累加数组,而是先算出前k个数字的和,然后就像维护一个滑动窗口一样,将窗口向右移动一位,即加上一个右边的数字,减去一个左边的数字,就等同于加上右边数字减去左边数字的差值,然后每次更新结果res即可。
注意:如何求解k个连续数值之和,这里使用accumulate函数。
class Solution {
public:
double findMaxAverage(vector<int>& nums, int k) {
double sum = accumulate(nums.begin(), nums.begin()+k, );//
double res = sum;
for(int i=k; i<nums.size(); ++i)
{
sum += nums[i] - nums[i-k];
res = max(res, sum);
}
return res/k;
}
};
参考
1. Leetcode_easy_643. Maximum Average Subarray I;
2. Grandyang;
完
【Leetcode_easy】643. Maximum Average Subarray I的更多相关文章
- 643. Maximum Average Subarray I 最大子数组的平均值
[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
- LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- 643. Maximum Average Subarray
Given an array consisting of \(n\) integers, find the contiguous subarray of given length \(k\) that ...
- [Leetcode]643. Maximum Average Subarray I
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- 【LeetCode】1120. Maximum Average Subtree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...
- 【LeetCode】152. Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【Leetcode_easy】628. Maximum Product of Three Numbers
problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...
随机推荐
- docker常用命令以及搭建
1. 进入容器中 docker exec -it <容器的名字> /bin/bash 2. 查看镜像 docker images 3. 查看所有容器 docker ps -a 4. 运行容 ...
- 并发编程大师系列之:Synchronized的类锁和对象锁
说到并发编程,感觉跟大多数人一样,谈之色变,说它简单把,其实很有内容,说难吧,用起来也挺容易,最近我硬着头皮,决心要把并发编程好好的搞一遍.以前,面试的时候,面试官问,并发编程会吗?嗯,接触过,就加一 ...
- 14 Vue列表渲染
列表渲染 用 v-for 把一个数组对应为一组元素(for循环) 我们可以用 v-for 指令基于一个数组来渲染一个列表. v-for 指令需要使用 item in items 形式的特殊语法, 其中 ...
- Python 使用装饰器装饰类
1.装饰器装饰函数 了解过或学过装饰器的同学都知道,Python 中的装饰器是可以装饰函数的,如: # 定义一个装饰器 def decorator(func): def inner(*args,**k ...
- 使用集合方式注入IoC
使用集合方式注入Ioc 1.创建类 //集合 private String[] arrays; //list集合 private List<Integer> lists; //map集合 ...
- javascript权威指南第14章 表单脚本示例代码
HTML部分 <!DOCTYPE html> <html> <head> <title></title> </head> < ...
- [NOI2019]序列(模拟费用流)
题意: 有两个长度为n的序列,要求从每个序列中选k个,并且满足至少有l个位置都被选,问总和最大是多少. \(1\leq l\leq k\leq n\leq 2*10^5\). 首先,记录当前考虑到的位 ...
- php常量和变量之变量引用
变量引用 变量引用很多老师喜欢来用C语言的指针来去讲解.我们作为有这么多年开发和教学经验的人来说——大多数学习PHP的人来说根本不了解C语言. 使用C语言一指针来讲解变量引用,我们觉得画蛇填足.并且, ...
- 32 | 为什么还有kill不掉的语句?
在 MySQL 中有两个 kill 命令:一个是 kill query + 线程 id,表示终止这个线程中正在执行的语句:一个是 kill connection + 线程 id,这里 connecti ...
- Greenplum 调优--数据倾斜排查(一)
对于分布式数据库来说,QUERY的运行效率取决于最慢的那个节点. 当数据出现倾斜时,某些节点的运算量可能比其他节点大.除了带来运行慢的问题,还有其他的问题,例如导致OOM,或者DISK FULL等问题 ...