Maximum Average Subarray II
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的更多相关文章
- leetcode644. Maximum Average Subarray II
leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- Maximum Average Subarray II LT644
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- 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 ...
- 643. Maximum Average Subarray I 最大子数组的平均值
[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
- LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)
643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...
- Maximum Average Subarray
Given an array with positive and negative numbers, find the maximum average subarray which length sh ...
- 【Leetcode_easy】643. Maximum Average Subarray I
problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...
随机推荐
- Ribbon【自定义客户端】
Ribbon的加载策略是懒加载,即第一次请求的时候才加载对应上下文,正是这个原因,很多时候第一次调用显得很慢,甚至会超时,所以,可以通过指定ribbon具体服务名称来开启饿加载,即在工程启动的时候,加 ...
- Tomcat中不能通过访问自己IP,但可以通过localhost/127.0.0.1访问
一.问题如下:局域网内,自己机器部署了一个tomcat应用,在本机上可以通过如下方式访问引用. http://localhost:8080/xxxx http://127.0.0.1:8080/xx ...
- Python 实例代码二
1.实现isNum()函数,参数为一个字符串,如果这个字符串属于整数.浮点数或复数的表示,则返回True,否则返回False def isNum(word): try: word=type(eval( ...
- 基于TCP的编程
前提:本文基于Linux系统下的学习 服务器端 1 创建通讯端口,返回socket设备的文件描述符 sfdsocket(2)#include <sys/types.h> /* See NO ...
- Scala 算法案例
移除第一个负数之后的所有负数 // 构建数组 val a = ArrayBuffer[Int]() a += (1, 2, 3, 4, 5, -1, -3, -5, -9) // 每发现一个第一个负数 ...
- SAS学习笔记52 Excel导入后日期错乱
读入Excel数据到SAS中,很小概率会遇到日期格式错乱,如:将Excel中的日期导入到SAS后就变成一个字符型的数字 在SAS中换算一下就可以更正
- bash 和 powershell 常用命令集锦
Linux Shell # 1. 后台运行命令 nohup python xxx.py & # 查找替换 ## 只在目录中所有的 .py 和 .dart 文件中递归搜索字符"main ...
- dotnetcore 与 hbase 之三——c# for hbase 客户端的使用
说明 在上一篇文章dotnetcore 与 hbase 之二--thrift 客户端的制作中已经可以找到 c# hbase 客户端的使用方法了,为什么这里单独列出一篇文章来讲述呢?最简单的理由就是,本 ...
- SQL Server2008分离数据库
1.右击数据库 2.Tasks 3.点击Detach 4.选取Drop Connections-->点击确定 5.开启本地数据库默认存储路径C:\Program Files\Microsoft ...
- 在我的电脑中删除wps云文档图标
在我的电脑中删除wps云文档图标 右键点击win10左下角选择运行,输入regedit打开注册表后,找到以下注册表路径: HKEY_CURRENT_USER\Software\Microsoft\Wi ...