Description

Given an array with positive and negative numbers, find the maximum average subarray which length should be greater or equal to given length k.

It's guaranteed that the size of the array is greater or equal to k.

Example

Example 1:

Input:
[1,12,-5,-6,50,3]
3
Output:
15.667
Explanation:
(-6 + 50 + 3) / 3 = 15.667

Example 2:

Input:
[5]
1
Output:
5.000

思路:二分答案
二分出 average 之后,把数组中的每个数都减去 average,然后的任务就是去求这个数组中,是否有长度 >= k 的 subarray,他的和超过 0。
这一步用类似 Maximum Subarray 的解法来做就好了
public class Solution {
/**
* @param nums: an array with positive and negative numbers
* @param k: an integer
* @return: the maximum average
*/ private boolean canFind(int[] nums, int k, double avg) {
double rightSum = 0, leftSum = 0, minLeftSum = 0;
for (int i = 0; i < k; i++) {
rightSum += nums[i] - avg;
} for (int i = k; i <= nums.length; i++) {
if (rightSum - minLeftSum >= 0) {
return true;
}
if (i < nums.length) {
rightSum += nums[i] - avg;
leftSum += nums[i - k] - avg;
minLeftSum = Math.min(minLeftSum, leftSum);
}
}
return false;
}
public double maxAverage(int[] nums, int k) {
double start, end, mid;
start = end = nums[0];
for (int i = 0; i < nums.length; i++) {
start = Math.min(nums[i], start);
end = Math.max(nums[i], end);
}
while (start + 1e-5 < end) {
mid = (start + end) / 2;
if (canFind(nums, k, mid)) {
start = mid;
} else {
end = mid;
}
}
return start;
}
}

  

Maximum Average Subarray II的更多相关文章

  1. leetcode644. Maximum Average Subarray II

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

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

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

  3. Maximum Average Subarray II LT644

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

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

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

  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. LeetCode 941. 有效的山脉数组(Valid Mountain Array)

    941. 有效的山脉数组 941. Valid Mountain Array 题目描述 给定一个整数数组 A,如果它是有效的山脉数组就返回 true,否则返回 false. 让我们回顾一下,如果 A ...

  2. [转帖]Linux下主机间文件传输命令

    Linux下主机间文件传输命令 https://yq.aliyun.com/articles/53631?spm=a2c4e.11155435.0.0.580ce8ef4Q9uzs   SCP命令: ...

  3. [SQL] - 报表查询效率优化

    背景 系统将数据对象JSON序列化后存放到数据库字段中.Report 模块需要获取实时数据对象数值,当前在SQL中进行数值判断的耗时长,效率低. 分析 当前执行效率低主要是程序结构设计的不合理. SQ ...

  4. Java基础---Java 数组

    数组概念: 数组就是存储数据长度固定的容器,保证多个数据的数据类型要一致. 数组的定义 格式:数组定义格式详解: 数组存储的数据类型: 创建的数组容器可以存储什么数据类型. [] : 表示数组. 数组 ...

  5. closed channel

    func Test_chanel(t *testing.T) { c := make(chan int, 1) go func() { time.Sleep(time.Second * 3) clos ...

  6. SpringCloud使用Consul作为分布式配置中心

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_36027670/article/de ...

  7. 将double转化成string,并保持N位小数

    double dumpSize = (1024000000.1415926535897932384 * 1.0) / 1024 / 1024; string tempStr = to_string(d ...

  8. git 修改注释

    原文:https://www.jianshu.com/p/098d85a58bf1 修改最后一条注释: git commit --amend 如果已经推送到远程,强制push到远程仓库: git pu ...

  9. List、dictionary、hashtable、ArrayList集合

    集合的引用命名空间在 system.Collections下 1.为什么引入集合 因为数组长度是固定的,为了建立一个动态的"数组",所以引入了集合. 2.为什么引入ArrayLis ...

  10. C# 不是序列化xml 转实体Model【原家独创】

    public static T XmlConvertModel<T>(string xmlStr) where T : class, new()        {            T ...