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 ...
随机推荐
- ADO.net 数据库连接new SqlConnection、Open、Close、Dispose
今天踩了一个坑,用了一个static对象,存储了Connection,导致了并发量大时(35/s)出现单个连接的数据库请求堆积,以及并发Open导致的异常,最终使服务下线. 排查中发现有些概念不清的地 ...
- sql分页查询(2005以后的数据库)和access分页查询
sql分页查询: select * from ( select ROW_NUMBER() over(order by 排序条件) as rowNumber,* from [表名] where 条件 ) ...
- 基于Linux解决登录ssh客户端失败问题—sshd error: could not load host key
当你ssh远程登录时,会发现ssh登录界面刚打开就会闪退,在查看主机日志消息中,就会看到如下错误: 然而问题的根源也就是这三个文件,无法正常加载ssh主机密钥. 而我们只需要将有问题的文件删除,然后重 ...
- (备忘)Nginx中文手册(技术指南第二版)
Nginx 常见应用技术指南[Nginx Tips] 第二版 目 录 一. Nginx 基础知识二. Nginx 安装及调试三. Nginx Rewrite四. Nginx Redirect五. Ng ...
- 使用ViewPager实现导航
1.使用PageAdapter适配器 2.使用FragmentPageAdapter适配器 监听事件: OnPageChangeListener 百度云:链接: http://pan.baidu.co ...
- Yii2 DatePicker时间选择器的使用
bootstrap-Date Time Picker 详细配置请参考http://www.bootcss.com/p/bootstrap-datetimepicker/ use kartik\widg ...
- 牛客小白月赛12 H 华华和月月种树 (离线dfs序+线段树)
链接:https://ac.nowcoder.com/acm/contest/392/H 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 131072K,其他语言2621 ...
- CSS基础学习-4.CSS属性_背景、颜色、边框
- NoSQL数据库技术实战-第1章 NoSQL与大数据简介 NoSQL产生的原因
NoSQL产生的原因: 关系型数据库不擅长的操作,是NoSQL应运而生的原因: 大量的数据写入操作书上写的是“大量数据的写入操作“,我理解的应该是“大量的数据写入操作”,因为大量的数据写入操作才会引起 ...
- [Python之路] 实现简易HTTP服务器与MINI WEB框架(利用WSGI实现服务器与框架解耦)
本文描述如果简单实现自定义Web服务器与自定义简易框架,并且不断进行版本迭代,从而清晰的展现服务器与Web框架之间是如何结合.如何配合工作的.以及WSGI是什么. 本文帖的代码有点多,但基本每次迭代修 ...