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. Sqlmap与burpsuite动态更新某些参数

    有如下注入点: http://localhost/id=1&order_nu=1 情况说明: id为注入点,  每一次注入时, order_nu不能跟上次的一样(假说这个order_nu为一个 ...

  2. windows安装linux虚拟机、修改apt源

    记录一下windows安装虚拟机以及初始配置的一些坑. 安装VMware Workstation 直接百度搜索VMware,选择合适的版本下载: 按照一般软件的安装步骤安装VMware Worksta ...

  3. Linux内存高,触发oom-killer问题解决

    最近遇到两起Linux的内存问题,其一是触发了oom-killer导致系统挂 1. 首先确认该系统的版本是32位 ? #uname -a Linux alarm 2.6.9-67.ELsmp #1 S ...

  4. git 还原到指定版本号

      git clone git branch -r --contains 88b92060224e96ef209565fa75c816eb9b0fae8e git checkout origin/re ...

  5. Vim的分屏功能

    本篇文章主要教你如何使用 Vim 分屏功能. 分屏启动Vim 使用大写的O参数来垂直分屏. vim -On file1 file2 ... 使用小写的o参数来水平分屏. vim -on file1 f ...

  6. [ python ] 练习作业 - 2

    1.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者. lic = [0, 1, 2, 3, 4, 5] def func(l): return l[1::2 ...

  7. ZOJ 1610 Count the Colors(区间染色)

    题目大意:多组数据,每组给一个n(1=<n<=8000),下面有n行,每行有l,r,color(1=<color<=8000),表示将l~r颜色变为color,最后求各种颜色( ...

  8. Lunix含Ubuntu使用总结

    错误 鼠标闪烁解决 系统设置->显示—>未知显示器->关闭->应用->选择当前配置 提示sudo: unable to resolve host ,亦即无法解析主机. 原 ...

  9. python collection系列

    collection系列 不常用功能,需要进行模块功能导入: import collection Counter 常用方法测试: #!/usr/local/env python3 ''' Author ...

  10. JavaScript快速找出字符串并返回其下标

    var box = "this is javascript"; for (var i = -1, arr = []; (i = box.indexOf("s", ...