LC 644. Maximum Average Subarray II 【lock,hard】
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 <=
k<=n<= 10,000. - Elements of the given array will be in range [-10,000, 10,000].
- 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】的更多相关文章
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- 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 ...
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- leetcode644. Maximum Average Subarray II
leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...
- Maximum Average Subarray II LT644
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- Maximum Average Subarray II
Description Given an array with positive and negative numbers, find the maximum average subarray whi ...
- 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 ...
- 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 ...
- LC 425. Word Squares 【lock,hard】
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
随机推荐
- phpstudycomposer thinkPHP5.1 使用
1.首先把php变成全局变量 2.打开phpstudy composer 的安装目录 E:\phpstudy\PHPTutorial\tools\composer 把里面的文件全部删除(或者备份一下) ...
- SQL语句复习【专题九】
SQL语句复习[专题九] 视图:View视图的概念:视图是从若干基本表或其他视图构造出来的表.在创建一个视图时,只是存放的视图的定义,也即是动态检索数据的查询语句,而并不存放视图对应的数据在用户使用视 ...
- 四、Attribute(2)授权角色过滤器
一.授权过滤器 1.新建一个mvc 项目 2.首先创建一个过滤器 MyAuthorizeAttribute 继承AuthorizeAttribute,并重写 AuthorizeCore public ...
- kubernetes管理机密信息
一.启动应用安全信息的保护: Secret介绍: 应用启动过程中可能需要一些敏感信息,比如访问数据库的用户名密码或者秘钥.将这些信息直接保存在容器镜像中显然不妥,Kubernetes 提供的解决方案是 ...
- JMeter 问题整理
1. 远程连接出现错误: java.net.connectexception: connection refused: connect -解决办法: 编辑jmeter.bat(windows OS), ...
- 在RecyclerView中集成QQ汽泡一
上次已经实现了QQ汽泡的自定义View的效果[http://www.cnblogs.com/webor2006/p/7726174.html],接着再将它应用到列表当中,这样才算得上跟QQ的效果匹配, ...
- Angular与Vue
最近在考虑对前端js框架的选择 根据前人的总结,就总结一下 Angular与Vue 的特点与区别 速度/性能 虽然 Angular 和 Vue 都提供了很高的性能,但由于 Vue 的虚拟 DOM 实现 ...
- java虚拟机的基本结构如图
1 java虚拟机的基本结构如图: 1)类加载子系统负责从文件系统或者网络中加载Class信息,加载的类信息存放于一块称为方法区的内存空间.除了类的信息外,方法区中可能还会存放运行时常量池信息,包括字 ...
- BZOJ3589 动态树[树剖/暴力/容斥]
操作0,显然直接线段树解决. 操作1,瓶颈在于重叠的链只算一次.在线段树上来看,如果一个区间被覆盖了,那么只算这个区间,子树里面也就不管了. 考虑对节点打标记来表示是否覆盖.但是,如果统一打完之后,并 ...
- 服务消费(LoadBalancerClient、Ribbon、Feign)
转自:https://www.jianshu.com/p/562045489d9d 4.1使用LoadBalancerClient 在Spring Cloud Commons中提供了大量的与服务治理相 ...