LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)
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 <=
k<=n<= 30,000. - 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 (最大平均值子数组之一)的更多相关文章
- [Leetcode]643. Maximum Average Subarray I
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [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 ...
- leetcode 643. Maximum Average Subarray I 子数组最大平均数 I
一.题目大意 https://leetcode.cn/problems/maximum-average-subarray-i/ 给你一个由 n 个元素组成的整数数组 nums 和一个整数 k . 请你 ...
- 【Leetcode_easy】643. Maximum Average Subarray I
problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- 643. Maximum Average Subarray I 最大子数组的平均值
[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
- 643. Maximum Average Subarray
Given an array consisting of \(n\) integers, find the contiguous subarray of given length \(k\) that ...
- LeetCode:152_Maximum Product Subarray | 最大乘积连续子数组 | Medium
题目:Maximum Product Subarray Find the contiguous subarray within an array (containing at least one nu ...
- 643. Maximum Average Subarray I
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
随机推荐
- Python: 列表注意细节与元组的基本用法
列表注意细节: 1.list.clear():将列表中成员清空(与del list区别开) 2.list.copy():复制一份相同的列表(浅COPY,只复制列表第一层) 3.如果两个列表相等,如li ...
- 监控-CPU使用率
原始脚本来自TG,自己对部分脚本做了调整,分享出来仅供参考,请勿整篇Copy! 使用以下语句获取[CPU使用率] USE [DBA_Monitor] GO /****** 对象: StoredProc ...
- [cocos2dx] lua注册回调到c++
思路 像所有语言一样,绑定回调主要是执行的任务执行到特定情形的时候,调用对用回调方法. 这里也一样.核心思路是,当c代码执行到特定特定情形的时候,调用lua的方法 我这里使用的是用lua_stack直 ...
- oracle 数据库管理--管理表空间和数据文件
一.概念表空间是数据库的逻辑组成部分.从物理上讲,数据库数据存放在数据文件中:从逻辑上讲,数据库数据则是存放在表空间中,表空间由一个或多个数据文件组成. 二.数据库的逻辑结构oracle中逻辑结构包括 ...
- 云 MongoDB 优化让 LBS 服务性能提升十倍
欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 随着国内服务共享化的热潮普及,共享单车,共享雨伞,共享充电宝等各种服务如雨后春笋,随之而来的LBS服务定位问题成为了后端服务的一个挑战.M ...
- NOIP算法总结与复习
NOIP算法总结与复习 (看了看李总的蓝皮书,收获颇多,记下此文,以明志--) (一)数论 1.最大公约数,最小公倍数 2.筛法球素数 3.mod规律公式 4.排列组合数,错排 5.Catalan数 ...
- C#单例测试(懒汉式双锁保证线程安全)
单例模式的概念 单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 关键点: 这个类只有一个实例,这是最基本的 它必须自行创建 ...
- TETELaser Cutting System 不连续吹起的问题
TETELaser Cutting System 不连续吹起的问题 :配置 加工参数-->机器参数-->信号灯和激光器报警:黄灯索引==EX14 红灯索引==EX15 绿灯索引= ...
- VPS修改SSH端口不小心把自己给墙掉的一般解决办法
很多人配置服务器的时候都习惯将 SSH 端口改为其他端口,以规避批量端口扫描,但也有不少人改完端口却忘了去防火墙修改端口,导致 SSH 登陆不上. 昨天我就这么干了 我把SSH端口从22改成了2345 ...
- 80端口被system 占用
1 运行'netstat -ano'发现80端口被pid=4的进程占用 2 打开任务管理器,发现pid=4的进程,其实是system进程,其对应的进程描述是NT kernel & system ...