Given an array with positive and negative numbers, find the maximum average subarray which length should be greater or equal to given length k.

Example

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的更多相关文章

  1. [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  2. Maximum Average Subarray II LT644

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  3. leetcode644. Maximum Average Subarray II

    leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...

  4. 643. Maximum Average Subarray I 最大子数组的平均值

    [抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...

  5. [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  6. LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)

    643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...

  7. Maximum Average Subarray II

    Description Given an array with positive and negative numbers, find the maximum average subarray whi ...

  8. 【Leetcode_easy】643. Maximum Average Subarray I

    problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...

  9. LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

随机推荐

  1. meterpreter 渗透用法

    获取凭证 hashdump模块(post)可以从SAM数据库中导出本地用户账号,credential_collector脚本(post/windows/gather/credentials)也可以从目 ...

  2. (TIP 2018)Technology details of FFDNet

    前言 论文地址:见researchgate, 方法继续更新. 解决的问题: 1.discriminative learning methods 用于denoising 任务学习到的是一个对于每种 噪声 ...

  3. 牛客网 查找第K小数

    题目链接:https://www.nowcoder.com/practice/204dfa6fcbc8478f993d23f693189ffd?tpId=40&tqId=21522&t ...

  4. python学习教程,史上最全面的python学习路线图

    Python 是Web 开发.游戏脚本.计算机视觉.物联网管理和机器人开发的主流语言之一,随着Python用户可以预期的增长,它还有机会在多个领域里登顶.Python学习路线分享给你. 阶段一是Pyt ...

  5. java面试题汇总(有的题无视即可,没什么实际用途)

    相关概念 面向对象的三个特征 封装,继承,多态,这个应该是人人皆知,有时候也会加上抽象. 多态的好处 允许不同类对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不同的行为方式(发送消 ...

  6. detours express版本的使用

    原文最早发表于百度空间2012-03-21 一.编译lib 1)拷贝它的src文件夹和system.mak文件到VS的VCVARS32.BAT所在的目录下 2)在命令提示符中运行VCVARS32.BA ...

  7. Linux 组管理、权限

    权限说明 1. 组涉及到两个配置文件,组文件/etc/group,组密码管理员/etc/gshadow/,GID500往后的算普通组. 2.主组与附属组,当创建一个用户,没有制定,用户会默认创建一个与 ...

  8. Java的类继承

    知识点1.继承作用:提高代码的重用性,继承之后子类可以继承父类中的属性和方法减少重复代码条件:子类和父类要满足is a的逻辑关系,才能使用继承.如:苹果 is a水果语法:使用extends 连接子类 ...

  9. c# 7.1 Async Main方法

    安装 .net framework sdk 7.1 新建一个 .net framework 7.1 的程序 在程序的工程文件的第一个 PropertyGroup  节点下加入以下子属性   <L ...

  10. Linux 命令行下导入导出 .sql 文件

    一.导出数据库用的是 mysqldump 命令 1.导出数据和表结构 /usr/bin/mysqldump -u 用户名 -p 数据库名 > 数据库名.sql 敲回车键后会提示输入密码 注意 m ...