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. Spring Boot Maven Plugin(二):run目标

    简介 Spring Boot Maven Plugin插件提供spring boot在maven中的支持.允许你打包可运行的jar包或war包. 插件提供了几个maven目标和Spring Boot ...

  2. 自定义BaseServlet利用反射

    比较完美一点的BaseServlet package com.yangwei.mvc.servlet; import java.io.IOException; import java.lang.ref ...

  3. JavaScript自动化构建工具入门----grunt、gulp、webpack

    蛮荒时代的程序员: 做项目的时候,会有大量的js 大量的css   需要合并压缩,大量时间需要用到合并压缩 在前端开发中会出现很多重复性无意义的劳动  自动化时代的程序员: 希望一切都可以自动完成  ...

  4. 轻松把你的项目升级到PWA

    什么是PWA PWA(Progressive Web Apps,渐进式网页应用)是Google在2015年推出的项目,致力于通过web app获得类似native app体验的网站. 优点 1.无需客 ...

  5. JAVA数据流再传递

    有一个filter类,在请求进入的时候读取了URL信息,并且读取了requestBod中的参数信息,那么在请求到达实际的控制层时,入参信息是拿不到的,对这种情况就需要数据流做再传递处理. 处理原理:使 ...

  6. String类的一些常见的比较方法(4)

    1:boolean equals(Object obj); //比较字符穿的内容是否相同 区分大小写的 2:boolean equalsIgnoreCase(String str); //比较字符穿的 ...

  7. JS中关于数组的内容

      前  言 LIUWE 在网站制作过程中,数组可以说是起着举足轻重的地位.今天就给大家介绍一下数组的一些相关内容.例如:如何声明一个数组和在网站制作过程中我们常用的一些数组的方法.介绍的不好还请多多 ...

  8. Linux下利用expect,不用交互模式,直接登陆远程主机

    Linux环境下只有在机器20.200.254.18上ssh dataconv@20.200.31.23才能连接到23的机器,而且还需要输入密码(每次都需要输入地址,密码很烦),所以利用expect写 ...

  9. VS2015 + EF6连接MYSQL

    ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,不仅支持SQL Server,还支持MySQL.Ora ...

  10. 每周分享之 二 http协议(2)

    本次分享http协议,共分为三部分,这是第二部分,主要讲解请求与响应的字段,以及状态码. 以http/1.1版本的一个完整的请求与响应作为例子 http请求信息由三部分组成 1.请求方法(GET/PO ...