【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: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...
随机推荐
- 【VS Code】中node.js代码自动补全的方法
原文链接: https://blog.csdn.net/qq_39189819/article/details/91347484
- vue2 路由,运动
- sqlserver 删除表数据
可以使用delete清空表delete from t表名 也可以使用truncate命令 truncate table 表名
- Greenplum 监控segment是否正常
在greenplum运行过程中,Segement很有可能因为压力大出现不可用的情况, 主备Segement发现了切换,或是主备Segement网络断开,数据不同步了.在 默认情况下,如果GreenPl ...
- Cogs 13. 运输问题4(费用流)
运输问题4 ★★☆ 输入文件:maxflowd.in 输出文件:maxflowd.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 一个工厂每天生产若干商品,需运输到销售部门进 ...
- yii2.0 模块的使用
以高级模板为例 1.复制目录 frontend或backend 为一个新目录pro修改advanced\common\config\bootstrap.php 添加一行,加载刚才的路劲Yii::set ...
- chrome的内存限制
推荐阅读:https://www.cnblogs.com/chengxs/p/10919311.html chrome内存限制 存在限制 Chrome限制了所能使用的内存极限(64位为1.4GB,32 ...
- Spring - 环境安装
安装IDEA的非Community版本和Java的包之后就可以用Java来HelloWorld了. 然后去这个链接:https://github.com/spring-guides/gs-rest-s ...
- Redis企业实战的一些坑
附录:Redis企业实战的一些坑 一.前言 小伙伴们对Redis应该不陌生,Redis是系统必备的分布式缓存中间件,主要用来解决高并发下分担DB资源的负载,从而提升系统吞吐量. Redis支持多种数据 ...
- bash常用的快捷键
bash常用快捷键 快捷键 作用 CTRL+A 把光标移动到命令行开头.如果我们输入的命令过长,则在想要把光标移动到命令行开头时使用 CTRL+E 光标移动到命令行行尾 CTRL+C 强制中止当前命令 ...