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 ...
随机推荐
- mysql----------mysql5.7如何配置主从数据库
主库: 1.配置文件里面加入以下两行 server-id=1 log-bin=MySQL-bin 2.创建账户 grant replication client,replication slave o ...
- lua 特殊时间格式转换
[1]时间格式转换需求 工作中,因业务需要将时间格式进行转换.需求内容如下: 原格式:17:04:49.475 UTC Mon Mar 04 2019 转换格式:2019-03-04 17:04:4 ...
- Servlet+纯java+MySQL实现课程信息的增删改查
Dbutil: package com.zh.util; import java.sql.Connection; import java.sql.DriverManager; import java. ...
- easyUI 创建详情页dialog
使用easyui dialog先下载jQuery easyui 的压缩包 下载地址http://www.jeasyui.com/download/v155.php 解压后放在项目WebContent ...
- 关于django1.8版本的静态文件配置
环境:Python3.5.4,django1.8.1. 在页面使用js时,总是提示404找不到js文件. 于是,看看了settings文件 好像也没什么毛病.导入的方式也换了很多种,总是不行,于是只好 ...
- miui获取完整root
1.先解锁BL锁 需要在miui官网申请,下载相关软件,申请后下载软件,提示需要过xx小时才能解锁(我是72小时) 2.解开BL锁后,在系统设置里开启root权限 3.开启root权限后,发现/sys ...
- index read-only
系统重启后,Eleastisearch6.5.0在给 Eleastisearch 更新索引的时候报了一个错误:ClusterBlockException[blocked by: [FORBIDDEN/ ...
- windows C++删除非空文件夹
//add by zhuxy 递归删除文件夹 BOOL myDeleteDirectory(CString directory_path) //删除一个文件夹下的所有内容 { BOOL ret=TRU ...
- eclipse Oxygen2 4.7.2版本安装activiti插件,并兼容svn插件
附录,插件下载:链接:https://pan.baidu.com/s/138ChoXao1fALBzdOhJjdQg 密码:06fx 安装方法: 解压eclipse安装包,将eclipse-activ ...
- Springboot解决war包放到Tomcat服务器上404的特殊情况
Springboot解决war包放到Tomcat服务器上404的特殊情况 原文链接:https://www.cnblogs.com/blog5277/p/9330577.html 原文作者:博客园-- ...