Maximum Average Subarray
Given an array with positive and negative numbers, find the maximum average subarray which length should be greater or equal to given length k.
Given nums = [1, 12, -5, -6, 50, 3], k = 3
Return 15.667 // (-6 + 50 + 3) / 3 = 15.667
利用队列建立窗口
public class Solution {
/**
* @param nums an array with positive and negative numbers
* @param k an integer
* @return the maximum average
*/
public double maxAverage(int[] nums, int k) {
// Write your code here
if(k<=0||nums==null||nums.length==0) return 0;
double average = Double.NEGATIVE_INFINITY;
double sum = 0;
Queue<Integer> queue = new LinkedList<Integer>();
for(int i=0;i< nums.length;i++){
if(i>=k){
int out = queue.poll();
sum-=out;
}
queue.offer(nums[i]);
sum+=nums[i];
if(i>=k-1){
average = Math.max(average, sum/k);
}
}
return average;
}
}
Maximum Average Subarray的更多相关文章
- [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 ...
- leetcode644. Maximum Average Subarray II
leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...
- 643. Maximum Average Subarray I 最大子数组的平均值
[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)
643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...
- Maximum Average Subarray II
Description Given an array with positive and negative numbers, find the maximum average subarray whi ...
- 【Leetcode_easy】643. Maximum Average Subarray I
problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...
- LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
随机推荐
- 第二章 函数和window对象
1.什么是函数函数相当于Java中的方法,每一个函数可以做一件事情,但是不属于某一个类 2.使用函数的好处:使代码模块化,功能分工明确,方便调用,思路功能清晰 3.函数的分类:(1)系统函数:系统提前 ...
- 使用quartz数据库锁实现定时任务的分布式部署
,1.根据项目引用的quartz依赖版本,确定下载的quartz-distribution安装包,我项目引用的信息如下图所示: 2.解压,在\quartz-2.2.3-distribution\qua ...
- lua 特殊时间格式转换
[1]时间格式转换需求 工作中,因业务需要将时间格式进行转换.需求内容如下: 原格式:17:04:49.475 UTC Mon Mar 04 2019 转换格式:2019-03-04 17:04:4 ...
- ANNOTATION 注解
注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts2有一部分也是 ...
- bzoj2880
打公式好麻烦 QAQ 为了节省时间去复习,原谅我引用一下别人的博客...http://blog.csdn.net/acdreamers/article/details/8542292 #include ...
- shell实现自动部署两台tomcat项目Ⅱ
本次分为3个脚本, scp.sh放进第一台机器(负责传输文件), schenglee.sh放进第一台机器(自动部署), schenglee2.sh放进第二台机器(自动部署) 环境 tomcat1: 1 ...
- Linux相关代码
Linux ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ---- ...
- bzoj 5185 Lifeguards - 动态规划 - 贪心
题目传送门 传送点I 传送点II 题目大意 给定$n$个区间,问恰好删去其中$k$个,剩下的区间的并的最大总长度. 显然被包含的区间一定不优.再加上被包含的区间对计数不友好.直接把它删掉. 注意到题目 ...
- js没有函数重载
上面这道题,要求判断输出的y和z分别为什么 一开始,我选择了2,4 后来发现答案是4,4 意识到js中没有函数重载!!!即使声明了两个同名函数,结果也是后面的函数覆盖了前一个函数. 而且函数声明会提升 ...
- 表达式引擎aviator
Aviator是一个轻量级.高性能的Java表达式执行引擎, 本文内容主要来自于官方文档 简介 包依赖 使用手册 执行表达式 使用变量 exec 方法 调用函数 自定义函数 编译表达式 访问数组和集合 ...