leetcode644. Maximum Average Subarray II

题意:

给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值。您需要输出最大平均值。

思路:

先计算前k个的avg和sum,然后每次往后加一个数,每次加一个数的同时用一个tempsum( = sum)依次减少前置的数,以此来模拟区间向后移动的计算。与直接的暴力相比,少了向后加又前减的一些步骤,减少了许多重复计算,但是这应该不是最优的算法,更快的算法暂时还没想到。

ac代码:

C++

class Solution {
public:
double findMaxAverage(vector<int>& nums, int k) {
double maxavg = 0;
double sum = 0;
int len = k;
for(int i = 0; i < k; i++)
{
sum += nums[i];
}
maxavg = sum/k;
double avg = maxavg; for(int i = k; i < nums.size(); i++)
{
len++;
sum += nums[i];
avg = sum/len; double tempsum = sum;
double tempavg = avg;
for(int j = len - 1; j >= k; j--)
{
tempsum -= nums[i - j];
tempavg = tempsum / j;
if(tempavg >= avg)
{
avg = tempavg;
sum = tempsum;
len = j;
}
}
if(avg > maxavg)
maxavg = avg;
}
return maxavg;
}
};

python


leetcode644. Maximum Average Subarray II的更多相关文章

  1. [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  2. Maximum Average Subarray II LT644

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  3. [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  4. Maximum Average Subarray II

    Description Given an array with positive and negative numbers, find the maximum average subarray whi ...

  5. LC 644. Maximum Average Subarray II 【lock,hard】

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

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

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

  7. LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)

    643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...

  8. Maximum Average Subarray

    Given an array with positive and negative numbers, find the maximum average subarray which length sh ...

  9. 【Leetcode_easy】643. Maximum Average Subarray I

    problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...

随机推荐

  1. Mysql储存过程3:if语句

    --if/else语句 if 条件 then SQL语句 else SQL语句elseifSQL语句 end if; create procedure test1( number int ) begi ...

  2. python selenium登陆网易云音乐

    from selenium import webdriver import time driver=webdriver.Chrome() driver.get("http://music.1 ...

  3. ie7浏览器兼容问题

    win10 下如何调试Ie 网上有很多ie的测试工具,包括ms自己出的有,但是如果是win10系统,压根不需要这些玩意. win10 浏览器edge虽然是重写过的,但是win10并没有完全抛弃ie,可 ...

  4. Python 库汇总中文版

    这又是一个 Awesome XXX 系列的资源整理,由 vinta 发起和维护.内容包括:Web框架.网络爬虫.网络内容提取.模板引擎.数据库.数据可视化.图片处理.文本处理.自然语言处理.机器学习. ...

  5. 当while read line 遇到 ssh

    问题:while read line 中使用ssh只能读取一行? #!/bin/sh while read line do echo $line ssh root@$line "echo 1 ...

  6. geoserver-manager发布style失败

    当参数给定没有错误时,最有可能的原因就是: sld文件格式应该以UTF-8无BOM格式编码(自己生成的sld文件多数情况下是以UTF-8格式编码).

  7. 常用的Oracle的doc命令

    常用的Oracle的doc命令 1.连接数据库 普通用户连接数据库: conn scott/tiger --(默认的用户名/密码).conn 即"connection"连接数据库的 ...

  8. SilverLight高亮显示文本

    文笔不好,就长话短说,就是想实现这样的效果,比如在成都二环路南一段一号附一号凤舞九天网吧 ,搜索 二环路 九天网吧 然后结果中高亮显示. <local:TextBlockHighLight Te ...

  9. ASP .NET CORE MVC 部署Windows 系统上 IIS具体步骤---.Net Core 部署到 IIS位系统中的步骤

    一.IIS 配置 启用 Web 服务器 (IIS) 角色并建立角色服务. 1.Windows Ddesktop 桌面操作系统(win7及更高版本) 导航到“控制面板” > “程序” > “ ...

  10. 详解Oracle的unlimited tablespace系统权限

    1. 系统权限unlimited tablespace是隐含在dba, resource角色中的一个系统权限. 当用户得到dba或resource的角色时, unlimited tablespace系 ...