LeetCode Maximum Average Subarray I
原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/
题目:
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].
题解:
维护长度为k的sliding window的最大sum.
Time Complexity: O(nums.length). Space: O(1).
AC Java:
class Solution {
public double findMaxAverage(int[] nums, int k) {
if(nums == null || nums.length < k){
throw new IllegalArgumentException("Illegal input array length smaller than k.");
} long sum = 0;
for(int i = 0; i<k; i++){
sum += nums[i];
} long max = sum;
for(int i = k; i<nums.length; i++){
sum += nums[i]-nums[i-k];
max = Math.max(max, sum);
}
return max*1.0/k;
}
}
LeetCode Maximum Average Subarray I的更多相关文章
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- [LeetCode] Maximum Average Subarray I 子数组的最大平均值
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)
643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...
- Maximum Average Subarray II LT644
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- Maximum Average Subarray
Given an array with positive and negative numbers, find the maximum average subarray which length sh ...
- leetcode644. Maximum Average Subarray II
leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...
- 643. Maximum Average Subarray I 最大子数组的平均值
[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
随机推荐
- 【TopCoder】SRM160 DIV1总结
做了两道题之后才发现做的是DIV1,不是DIV2,DIV1的第二道题是DIV1的第三道题,果断决定第3题就不看了=.= 250分题:给定一个时间起点8:00 AM DAY 1,再给出一组时间终点,格式 ...
- CSS3 画点好玩的东西
虽然项目赶工还是挺忙的,但闲了总要找点乐子嘛,毕竟秃顶和猝死两座大山夹逼着编程员们. 好吧,其实是无聊起来我自己都怕,于是就做了点小玩意. .heart { position: relative; t ...
- MongoDB命令语法小用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MongoDB; ...
- PHP辅攻_[学习资料收集]PHP连接SQLServer2005方法
PHP连接SQLServer2005 1.修改php.ini将extension=php_mssql.dll的注释删除保存. 修改php.in将mssql.secure_connection = Of ...
- 进程控制块PCB结构体 task_struct 描述
进程控制块,英文名(Processing Control Block),简称 PCB . 进程控制块是系统为了管理进程设置的一个专门的数据结构,主要表示进程状态. 每一个进程都对应一个PCB来维护进程 ...
- 克隆VMware虚拟机
虚拟机使用过程需要用到多个进行实验.重新安装时间又太长,通过vmware虚拟机自带软件能够很好的快速克隆出完全相同的系统. 环境:关闭VMware,不要在开启状态哦~ 步骤: 选中需要被克隆的系统 菜 ...
- Prometheus Monitoring System & Time Series Database
什么是 TSDB (Time Series Database): 我们可以简单的理解为.一个优化后用来处理时间序列数据的软件,并且数据中的数组是由时间进行索引的. 时间序列数据库的特点: 大部分时间都 ...
- poj 1273 ---&&--- hdu 1532 最大流模板
最近在换代码布局,因为发现代码布局也可以引起一个人的兴趣这个方法是算法Edmonds-Karp 最短增广路算法,不知道的话可以百度一下,基于Ford-Fulkerson算法的基础上延伸的 其实我不是很 ...
- RHEL7 LAMP
准备篇: RHEL 7.0系统安装配置图解教程:http://www.osyunwei.com/archives/7702.html 一.使用系统镜像文件配置本地yum源 1.使用WinSCP.exe ...
- 2.virtualenv安装和配置以及在PyCharm中如何使用虚拟环境
virtualenv优点 使不同应用开发环境相互独立 环境升级不影响其它应用,也不会影响全局的python环境 它可以防止系统中出现包管理混乱和版本的冲突 1.使用virtualenv pip ins ...