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的更多相关文章

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

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

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

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

  3. 643. Maximum Average Subarray

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

  4. [Leetcode]643. Maximum Average Subarray I

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

  5. [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 ...

  6. 【LeetCode】1120. Maximum Average Subtree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  7. 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...

  8. 【LeetCode】152. Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  9. 【Leetcode_easy】628. Maximum Product of Three Numbers

    problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...

随机推荐

  1. RedisTemplate和StringRedisTemplate的区别

    今天springboot项目中用redis的时候,遇到了一个问题,用RedisTemplate这个类向redis中存储数据的时候,明明数据存进去了,也可以取出来,但是rdm就是看不到key的值,网上的 ...

  2. Selenium常用API的使用java语言之15-警告框处理

    在 WebDriver中处理JavaScript所生成的alert.confirm以及prompt十分简单,具体做法是使用switch_to_alert()方法定位到alert/confirm/pro ...

  3. Moq练习

    本文参考 http://www.cnblogs.com/haogj/archive/2011/06/24/2088788.html Moq适合于TDD的项目,小项目初期应该不太适合使用,有些浪费时间了 ...

  4. ZooInspector使用

    一.工具 ZooInspector作用: 可以利用该工具图形化浏览ZK中的文件及文件夹 下载地址: https://issues.apache.org/jira/secure/attachment/1 ...

  5. 14 | count(*)这么慢,我该怎么办?

    在开发系统的时候,你可能经常需要计算一个表的行数,比如一个交易系统的所有变更记录总数.这时候你可能会想,一条select count(*) from t 语句不就解决了吗? 但是,你会发现随着系统中记 ...

  6. antd 表格隔行变色

    rowClassName={(record, index) => { let className = 'light-row'; if (index % 2 === 1) className = ...

  7. Android工程的合并

    http://www.xuanyusong.com/archives/3395 1.游戏包名( 类似 com.xx.xxx ) Android应用程序只能有一个包名,如果两个游戏包名一样,那么后者安装 ...

  8. python获取当前天气情况

    利用 Python 从互联网公开服务中获取天气预报信息.天气信息来源网站:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx实现以下 ...

  9. OpenJudge 1.5.27:级数求和

    描述 已知:Sn= 1+1/2+1/3+…+1/n.显然对于任意一个整数K,当n足够大的时候,Sn大于K. 现给出一个整数K(1<=k<=15),要求计算出一个最小的n:使得Sn>K ...

  10. Java核心复习——CompletableFuture

    介绍 JDK1.8引入CompletableFuture类. 使用方法 public class CompletableFutureTest { private static ExecutorServ ...