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. Java学习笔记【七、时间、日期、数字】

    参考:http://www.runoob.com/java/java-date-time.html Date类 构造: Date() 使用当前的日期时间 Date(long millisec) 197 ...

  2. ubuntu16.04环境LNMP实现PHP5.6和PHP7.2

    最近因为公司突然间说要升级php7,所以做个记录 PPA 方式安装 php7.2 : sudo apt-get install software-properties-common 添加 php7 的 ...

  3. 【2017-04-20】Ado.Net与面向对象结合架构中的数据访问层(实体类,数据访问类)

    开发项目三层架构:界面层.业务逻辑层.数据访问层 今天学习一下数据访问层,分为实体类和数据访问类 所有的类放在App_Code这个文件夹下边.养成一个好的习惯. 一.实体类 数据库中的表映射为一个类, ...

  4. LNMP安装与配置之CentOS7的安装。

    LNMP与LAMP是我们常见的两种网站服务器架构.LNMP代表的就是Linux系统下Nginx+MySQL+PHP/Python,LAMP代表的则是Linux系统下Apache+MySQL+PHP/P ...

  5. PAT Basic 1056 组合数的和 (15 分)

    给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字.要求所有可能组合出来的 2 位数字的和.例如给定 2.5.8,则可以组合出:25.28.52.58.82.85 ...

  6. python操作kafka实践

    1.先看最简单的场景,生产者生产消息,消费者接收消息,下面是生产者的简单代码. ------------------------------------------------------------ ...

  7. socket链接的关闭连接与close和shutdown的区别

    TCP主动关闭连接 appl: close(), --> FIN FIN_WAIT_1 //主动关闭socket方,调用close关闭socket,发FIN <-- ACK FIN_WAI ...

  8. index首页加载数据库数据方法

    https://blog.csdn.net/qq_33198758/article/details/82987805 在做网站的时候,会遇到需要首页加载数据库数据的情况.而web.xml配置的首页: ...

  9. java 内部类(简单使用)

    什么是内部类 1.内部类是指在一个外部类的内部再定义一个类. 2.内部类作为外部类的一个成员,依附于外部类而存在. 3.内部类可为静态,可用protected和private修饰(而外部类只能使用pu ...

  10. PHP类知识----clone方法上机实验

    <?php class mycoach { public function __construct($name,$age) { $this->name = $name; $this-> ...