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. Quartz.Net—配置化

    Schedule配置 线程数量 如果一个Schedule中有很多任务,这样默认的10个线程就不够用了. 有很多种方法配置线程的个数. 工厂构造函数 webfonfig quartzconfig 环境变 ...

  2. 在CentOS7 安装 Redis数据库

    环境说明: 名称 版本 CentOS CentOS Linux release 7.4.1708 (Core) VMware Fusion 专业版 10.1.1 (7520154) SSH Shell ...

  3. QT json数据的应用(cJSON)

    json数据可保存小量的数据在本地的json文件中.QT有两种方式操作:(1).cJSON (2).QT的操作json数据的类. 应用:将监控预案数据保存在本地中. 1.首先根据预案结构创建一个jso ...

  4. 剑指offer64:滑动窗口的最大值

    1 题目描述 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4 ...

  5. 二十一、RTC驱动

    一.RTC设备驱动分析 内核的rtc驱动位于内核drivers/rtc目录下,里面包含各个平台的RTC驱动.读者可在此目录下任意选择一个单板驱动文件进行分析,我选择的是rtc-davinci.c文件. ...

  6. 怎样解决Script error报错问题

    如果脚本网址与网页网址不在同一个域(比如使用了 CDN), 那如果这个脚本执行报错了, 就会报:Script error. 由于同源策略, 浏览器禁止向外部脚本泄漏信息, 因此不会提供完整的报错信息, ...

  7. C# 阿拉伯数字转换为中文数字/中文数字转换为阿拉伯数字

    项目中经常会格式化数据,转换数字的使用情况比较多,记录一下数字转换的方法! 如果需要转换为繁体中文,将数组里的汉字换成繁体中文即可. 1.阿拉伯数字转换为中文数字 /// <summary> ...

  8. Vivado cordic IP求模求角教程

    前言 当需要对复数求模的时候,用FPGA怎么求呢?怎么开根号? 方法1:先求幅值平方和,再使用cordic IP开根号.(蠢办法) 方法2:直接用cordic求取模值. 此处只介绍方法2,资源占用更少 ...

  9. 安装HANA Rules Framework(HRF)

    1. 收集文档 1.1  SAP HANA Rules Framework by the SAP HANA Academy link 1.2  HANA Rules Framework (HRF) b ...

  10. Vue使用ref 属性来获取DOM

    注意,在父组件中可以使用this.$refs.属性名  获取任何元素的属性和方法,子组件不可以获取父组件中的 <!DOCTYPE html> <html lang="en& ...