644. Maximum Average Subarray II
来源:互联网 发布:海美迪q5四核 优化补丁 编辑:程序博客网 时间:2024/06/13 05:36
先看个简单的
Given an array consisting of n
integers, find the contiguous subarray of given length 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 = 4Output: 12.75Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
Note:
- 1 <=
k
<=n
<= 30,000. - Elements of the given array will be in the range [-10,000, 10,000].
package l643;public class Solution { public double findMaxAverage(int[] nums, int k) { long sum = 0, max = 0; for(int i=0; i<k; i++)sum+=nums[i]; max = sum; for(int i=k; i<nums.length; i++) { sum += nums[i]; sum -= nums[i-k]; if(sum > max) max = sum; } return (max+0.0)/k; }}
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 = 4Output: 12.75Explanation: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.
思路:有2个trick
1. 只要满足一定误差,所以用二分,直接二分最后的平均值
2. 在判断某个平均值是否满足时,转换为求连续子数组的和大于0(这个求平均值的套路可以记一下)
/* * 只要10-5 精度就好 */public class Solution { public double findMaxAverage(int[] nums, int k) { int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; for(int i : nums) { min = Math.min(min, i); max = Math.max(max, i); } double lo = min, hi = max; while(hi-lo > 1e-6) { double mid = (lo+hi)/2.0; if(ok(nums, k ,mid)) lo = mid; else hi = mid; } return lo; }private boolean ok(int[] nums, int k, double mid) {// 数组每个数减去mid,转换为:连续子数组累加大于0double[] t = new double[nums.length];for(int i=0; i<nums.length; i++)t[i] = nums[i] - mid;double sum = 0;for(int i=0; i<k; i++)sum += t[i];if(sum >= 0)return true;double min = 0, sum2 = 0;for(int i=k; i<t.length; i++) {sum2 += t[i-k];sum += t[i];min = Math.min(min, sum2);if(sum - min >= 0)return true;}return false;}}
阅读全文
0 0
- 644. Maximum Average Subarray II
- 644. Maximum Average Subarray II
- Leetcode 644. Maximum Average Subarray II 最大平均区间2 解题报告
- [LintCode]Maximum Average Subarray
- Maximum Average Subarray I
- Lintcode 617:Maximum Average Subarray
- 643. Maximum Average Subarray I
- 643. Maximum Average Subarray I
- 643. Maximum Average Subarray I
- 643. Maximum Average Subarray I
- leetcode[Maximum Average Subarray I]
- 643. Maximum Average Subarray I
- 643. Maximum Average Subarray I
- 643. Maximum Average Subarray I
- 643. Maximum Average Subarray I
- 643 Maximum Average Subarray I
- leetCode-Maximum Average Subarray I
- Lintcode - Maximum Subarray II
- HDU 1166 线段树 解题报告
- 如何限制程序互斥运行?
- Codeforces 740 B Alyona and flowers(前缀和)
- Python编程_Lesson007_循环讲解和函数
- jzoj. 1300. 卫星照片
- 644. Maximum Average Subarray II
- 树链剖分学习笔记
- SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
- LeetCode 636 : Exclusive Time of Functions(python)
- webstorm 激活 2017.7.16测试可用
- 单例模式(Singleton Pattern)。
- 拷贝之 SCP
- BLE简介及连接过程
- 每天坚持写日记第一篇