【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: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...
随机推荐
- idea远程debug调试阿里云ECS
1.首先远程服务器的代码跟本地项目代码应该完全一致,否则会出现debug混乱现象,亲测. 2.config如图: ①命名可以省略②复制这个地址③输入远程ip和自定义且未被占用的端口号xxxx 3.开放 ...
- 有关Django的smallDemo
注: 电脑为Mac,Python解释器为3.5.4 数据库使用的是pymysql模块代替mysqldb 功能: 运行服务器,在login登录界面输入用户名和密码,post到服务器, 通过数据库判断用户 ...
- 系统空闲时间 解决 GetLastInputInfo 负数问题
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices ...
- msyql的子查询,或者叫嵌套查询
INNER和OUTER可以省略
- 百度UEditor编辑器从word粘贴公式
官网地址http://ueditor.baidu.com Git 地址 https://github.com/fex-team/ueditor 参考博客地址 http://blog.ncmem.com ...
- Our Journey of Dalian Ends && Our Journey of Xian Ends 最小费用最大流
2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛Our Journey of Dalian Ends 题意:要求先从大连到上海,再从上海打西安,中途会经过其他城市,每个城市只能去一次,出一次, ...
- Codeforces 1153F Serval and Bonus Problem [积分,期望]
Codeforces 思路 去他的DP,暴力积分多好-- 首先发现\(l\)没有用,所以不管它. 然后考虑期望的线性性,可以知道答案就是 \[ \int_0^1 \left[ \sum_{i=k}^n ...
- hdu4283
You Are the One Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- LeetCode 第 151 场周赛
一.查询无效交易(LeetCode-1169) 1.1 题目描述 1.2 解题思路 根据,它和另一个城市中同名的另一笔交易相隔不超过 60 分钟(包含 60 分钟整) 得出 城市A和其他城市任何一笔交 ...
- Leetcode题目617:合并二叉树(递归-简单)
题目描述: 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新 ...