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 = 4
Output: 12.75
Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75

Note:

  1. 1 <= k <= n <= 30,000.
  2. Elements of the given array will be in the range [-10,000, 10,000].

题目标签:Array

  题目给了我们一个nums array 和一个 k,让我们找到长度为k 的子数组,它的平均值是最大的。

  这题比较容易想到的方法就是 sliding window, 想象一下有一个长度为k 的窗口在移动,每次加上一个新的num,还要减去一个旧的num。维护更新一个最大的sum。(这里不需要在每一次维护更新的时候 / k,最后 /k 就可以了)  

  最后用最大的sum / k 。

  

  在做这一题的过程中,我发现 Double.MIN_VALUE 居然是大于0的数字。之前用的 Integer.MIN_VALUE 明明是最小的负数。

  来看一下Double.MIN_VALUE 的定义:A constant holding the smallest positive nonzero value of type double, 2-1074.

Java Solution:

Runtime beats 51.30%

完成日期:10/19/2017

关键词:Array

关键点:Sliding Window

 class Solution
{
public double findMaxAverage(int[] nums, int k)
{
double mav = -Double.MAX_VALUE;
double tempMav = 0; for(int i=0; i<nums.length; i++)
{
tempMav += nums[i]; if(i + 1 >= k)
{
mav = Math.max(mav, tempMav);
tempMav -= nums[i + 1 - k];
} } return mav / k;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)的更多相关文章

  1. [Leetcode]643. Maximum Average Subarray I

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

  2. [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)

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

  3. leetcode 643. Maximum Average Subarray I 子数组最大平均数 I

    一.题目大意 https://leetcode.cn/problems/maximum-average-subarray-i/ 给你一个由 n 个元素组成的整数数组 nums 和一个整数 k . 请你 ...

  4. 【Leetcode_easy】643. Maximum Average Subarray I

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

  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. 643. Maximum Average Subarray I 最大子数组的平均值

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

  7. 643. Maximum Average Subarray

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

  8. LeetCode:152_Maximum Product Subarray | 最大乘积连续子数组 | Medium

    题目:Maximum Product Subarray Find the contiguous subarray within an array (containing at least one nu ...

  9. 643. Maximum Average Subarray I

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

随机推荐

  1. Java:java中BufferedReader的read()及readLine()方法的使用心得

    BufferedReader的readLine()方法是阻塞式的, 如果到达流末尾, 就返回null, 但如果client的socket末经关闭就销毁, 则会产生IO异常. 正常的方法就是使用sock ...

  2. SQL Server安装【转载】

    http://blog.csdn.net/sangjinchao/article/details/62044021?locationNum=6&fps=1

  3. Hibernate第八篇【懒加载】

    前言 前面在使用Hibernate的时候就提及过了懒加载,但没有好好地说明具体的说明究竟是怎么回事-本博文主要讲解懒加载 什么是拦截器以及为什么要使用懒加载? 懒加载就是当使用数据的时候才去获取数据. ...

  4. 04面向对象编程-02-原型继承 和 ES6的class继承

    1.原型继承 在上一篇中,我们提到,JS中原型继承的本质,实际上就是 "将构造函数的原型对象,指向由另一个构造函数创建的实例". 这里,我们就原型继承的概念,再进行详细的理解.首先 ...

  5. IT连创业系列:创业者逆境下的思维

    距上篇文章,又半个多月过去了,是时候来一发阶段性的总结了. 可能最近比较懒,也可能是想不到写文的主题,故写文已变成越来越艰难的一个任务. 这个系列的大标题,也改了:它从<一个想法>到< ...

  6. HTML基础入门

    1.什么是HTML 2.HTML文件结构 3.HTML文档 4.HTML标签 1.什么是HTML 首先,HTML是一种语言,是用来描述网页的语言 HTML 指的是超文本标记语言 (Hyper Text ...

  7. apriori关联规则

    挖掘数据集:贩物篮数据 频繁模式:频繁地出现在数据集中的模式,例如项集,子结构,子序列等 挖掘目标:频繁模式,频繁项集,关联规则等 关联规则:牛奶=>鸡蛋[支持度=2%,置信度=60%] ...

  8. html基础知识笔记

    HTML基础 1.1HTML文件的基本结构和W3C标准 1.1.1HTML简介 HTML是一种描述网页的语言,一种超文本标记的语言! 1.1.2HTML文件的基本结构 头部(head) 头部是网页的标 ...

  9. 在CentOS 7 上安装docker

    Docker CE Install yum-utils, which provides the yum-config-manager utility: $ sudo yum install -y yu ...

  10. Android shared_preference操作

    实例化SharedPreferences对象 //test :想要打开的SharedPreferences文件名 //Activity.MODE_PRIVATE:操作模式 MODE_PRIVATE | ...