Given an array consisting of n integers, find the contiguous subarray whose length is greater than or equal to k that has the maximum average value. And you need to output the maximum average value.

Example 1:

Input: [1,12,-5,-6,50,3], k = 4
Output: 12.75
Explanation:
when length is 5, maximum average value is 10.8,
when length is 6, maximum average value is 9.16667.
Thus return 12.75.

Note:

  1. 1 <= k <= n <= 10,000.
  2. Elements of the given array will be in range [-10,000, 10,000].
  3. The answer with the calculation error less than 10-5 will be accepted.

求一个数组中长度大于等于K的子数组的平均值的最大值。

我的AC解,这道题没什么思路,我只能暴力求解了,虽然AC但时间肯定长。

Runtime : 1240ms  Beats:5.63%

class Solution {
public:
double findMaxAverage(vector<int>& nums, int k) {
vector<int> B(nums.size()+);
B[] = ;
for(int i=; i<B.size();i++){
B[i] = B[i-] + nums[i-];
}
double sum = (double)INT_MIN; if(nums.size() < k) return ;
for(int i=k; i<B.size(); i++){
for(int j=; j+k<=i; j++){
if(B[i] - B[j] > sum * (i - j)){
sum = (double)(B[i]-B[j])/(i-j);
}
}
}
return sum;
}
};

这是网上的解法,利用的是二分查找,但是这里的二分查找的判断条件不一样,是以平均值作为判断条件。

我们要知道,一个数组的子数组的平均值介于这个数组最大值和最小值之间。因此可以将最大值和最小值的平均数mid作为判断依据,如果存在某一个长度大于K的子数组的平均数大于mid,那就说明

需要从mid和最大值之间继续寻找。寻找次数是O(log(max + min)),每一次寻找都是遍历一遍数组O(n)。

那如何通过遍历一遍数组来寻找是否存在一个长度大于K的子数组的平均数大于mid呢?先看代码,

Runtime: 72ms, Beats: 90.14%

整个程序结构right代表最大值,left代表最小值,整个程序大结构是一个while循环,判断条件是right和left差值小于0.00001,意思就是找到了题目要求的值。

mid是最大值和最小值的平均数。

利用sum求得数组累加和与mid的差值。因为我们想要比较长度大于K的所有数组和mid的大小关系,其实只需要比较长度大于K的所有数组中的最大值与mid的关系。

而这个最大值和mid的关系可以用sum > minsum来表示,sum > minsum <=> sum - minsum > 0 <=> Σnums[i] - i * mid - minsum > 0 这个式子减号前一部分的意义好理解,

就是前i个数组元素的和与i*mid的差,其实就是前i个数组元素的平均数与mid的差,minsum是第0个到第i-k个累加和中最小的一个和0的最小值,minsum初始值为0,

所以碰到任何在0到i-k之间大于0的累加和全部为0,因为此时能让sum最大的情况是我把前i个元素都包括进来。

如果碰到minsum小于0,假设这个minsum的index是第j个,也就是说第0到j个的累加和是小于0的,那这个时候,第j到i的累加和是要大于第0到i的累加和的,因为我们去掉了一些

小于0的元素。取最小就是为了让sum最大。所以我们能得到长度大于K的所有数组的最大值与mid的关系。一旦得到了这个关系我们就知道他高于还是低于平均值mid,进而再

用二分查找的思想迭代,得到最终的长度大于K的所有数组的最大值。

后续:

1. 如果要求长度大于K的所有数组的最小值怎么办?

修改二分查找的时候right和left的赋值规则就行。很简单了。

LC 644. Maximum Average Subarray II 【lock,hard】的更多相关文章

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

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

  2. LC 272. Closest Binary Search Tree Value II 【lock,hard】

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

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

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

  4. leetcode644. Maximum Average Subarray II

    leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...

  5. Maximum Average Subarray II LT644

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

  6. Maximum Average Subarray II

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

  7. LC 244. Shortest Word Distance II 【lock, Medium】

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  8. LC 774. Minimize Max Distance to Gas Station 【lock,hard】

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  9. LC 425. Word Squares 【lock,hard】

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

随机推荐

  1. linux tty终端个 pts伪终端 telnetd伪终端

    转:http://blog.sina.com.cn/s/blog_735da7ae0102v2p7.html 终端tty.虚拟控制台.FrameBuffer的切换过程详解 Framebuffer Dr ...

  2. Makefile 编译静态库文件及链接静态库

    本文为原创文章,转载需指明该文链接 1.代码目录结构如下: comm/ comm/inc/apue.h  3 atexit.c Makefile  5 staticlib/lib/ staticlib ...

  3. Python制作的射击游戏

    如果其他朋友也有不错的原创或译文,可以尝试推荐给伯乐在线.] 你有没有想过电脑游戏是怎样制作出来的?其实它没有你想象的那样复杂! 在这个教程里,你要学做一个叫<兔子和獾>的塔防游戏,兔子作 ...

  4. IPC之util.c源码解读

    // SPDX-License-Identifier: GPL-2.0 /* * linux/ipc/util.c * Copyright (C) 1992 Krishna Balasubramani ...

  5. Docker容器内服务自启

    创建容器时需要配置--privileged和容器启动后执行的命令为/sbin/init/. docker run -d -it --name example -p 3308:3306 -p 2080: ...

  6. css改变背景透明度

    透明往往能产生不错的网页视觉效果,先奉上兼容主流浏览器的CSS透明代码:.transparent{filter:alpha(opacity=90); -moz-opacity:0.9; -khtml- ...

  7. Linux之apt-get软件管理

    apt-get 用Linux apt-get命令的第一步就是引入必需的软件库,Debian的软件库也就是所有Debian软件包的集合,它们存在互联网上的一些公共站点上.把它们的地址加入,apt-get ...

  8. MyBatis笔记-03-CRUD

    3.CRUD 1.namespace namespace中的包名要和Dao/mapper接口的包名保持一致 2.select 选择查询语句: id:就是对应的namespace中的方法名: resul ...

  9. ios 打包下

    一.打包真机方式 二.编译打包 三.配置打包信息 以下为打的包:

  10. CSS引入外部字体方法,附可用demo

    有时候我们做的页面需要用到一些更好看的字体又不想用图片代替,图片会影响加载速度则使用外部字体来显示但是直接通过font-family又不一定全部都行这就需要我们在css中进行定义并且引入字体文件路径然 ...