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. 多线程面试题系列(7):经典线程同步 互斥量Mutex

    前面介绍了关键段CS.事件Event在经典线程同步问题中的使用.本篇介绍用互斥量Mutex来解决这个问题. 互斥量也是一个内核对象,它用来确保一个线程独占一个资源的访问.互斥量与关键段的行为非常相似, ...

  2. geotrellis使用(三十三)关于Geotrellis读取Geotiff的两个细节

    前言 在上两篇文章中我介绍了如何直接将Geotiff(一个或者多个)发布为TMS服务.这中间其实我遇到了一个问题,并且这个问题伴随Geotrellis的几乎所有使用案例,下面我进行详细讲述. 一.问题 ...

  3. 我的Spring学习记录(二)

    本篇就简单的说一下Bean的装配和AOP 本篇的项目是在上一篇我的Spring学习记录(一) 中项目的基础上进行开发的 1. 使用setter方法和构造方法装配Bean 1.1 前期准备 使用sett ...

  4. 翻译连载 |《你不知道的JS》姊妹篇 |《JavaScript 轻量级函数式编程》- 引言&前言

    原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 译者团队(排名不分先后):阿希.blueken.brucec ...

  5. JS--微信浏览器复制到剪贴板实现

    由于太忙很久没写博客了,如有错误遗漏,请指出,感谢! 首先这里要注意,是微信浏览器下的解决方案,其他浏览器请自行测试. 先说复制到剪贴板主要有什么使用场景: 优惠券优惠码,需要用户复制 淘宝商品,需要 ...

  6. 九月 26, 2017 10:18:14 上午 com.sun.jersey.server.impl.application.RootResourceUriRules <init> 严重: The ResourceConfig instance does not contain any root resource classes.

    Tomcat启动错误:九月 26, 2017 10:18:14 上午 com.sun.jersey.server.impl.application.RootResourceUriRules <i ...

  7. S2_SQL_第五章

    UNIQUE|FULLTEXT|SPATIAL:分别表示唯一索引,全文索引和空间索引,为可选参数index_name;指定索引名称table_name;指定创建索引表名colymn_name;指定需要 ...

  8. Ansible(二) - 配置及命令简介

    Ⅰ. Ansible Inventory Hosts文件配置 # mkdir /etc/ansible # touch /etc/ansible/hosts # cat /etc/hosts 127. ...

  9. MySQL Windows版安装详解

    一.下载MySQL MySQL官网https://dev.mysql.com提供了Windows下的安装版msi和解压版zip,其中均包含32和64位版本,mis版本与SqlServer安装基本一致N ...

  10. 使用Dapper操作Mysql数据库

    首先我想说明一下:相比最原始的ADO.NET,一般都认为封装过一层的ORM性能上会有损耗,但其实在使用中你会发现,当你需要把数据库对象转化为实体模型时,很多所谓的DbHelper其实封装的很低效,反而 ...