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 ...
随机推荐
- $《第一行代码:Android》读书笔记——第2章 Activity
(一)创建活动 1.创建活动类 创建没有Activity的项目,发现src文件夹是空的,手动创建一个包com.jyj.demo1,在包中添加一个名为MainActivity的class,该MainAc ...
- vuex的 例子
最近在学习vuejs,一直有听说vuex,用来实现多组件共享的一种状态管理模式,但是网上都说,不要为了用vuex而用vuex,大概意思就是尽量少用vuex,一些小项目可以用bus来实现组件之间的传值问 ...
- qnx 黑莓系统
qnx 黑莓图形系统框架 http://www.360doc.com/content/07/0807/10/38907_658192.shtml http://blog.csdn.net/keyboa ...
- Eclipse SVN插件设置
项目开发中,开发人员用SVN来管理代码,在和服务器同步时,需要避免上传不必要的一些编译文件,如.class,.log,target等文件,这里需要设置同步选项. 打开Eclipse ---> W ...
- Go sqlx库
sqlx is a library which provides a set of extensions on go's standard database/sql library. sqlx sup ...
- JAVA 使用qq邮箱发送邮件
引入一个架包: gradle( "com.sun.mail:javax.mail:1.5.6", ) 代码如下: private static final String QQ_EM ...
- PhotoShopCs5启动 需要使用Adobe Application Manager 启动试用版
解决办法:下载Application Manager 7.0 地址:http://download.adobe.com/pub/adobe/creativesuite/cs/win/Applicati ...
- linux基础(8)-颜色显示
echo显示内容-带颜色显示 格式:echo -e "\033[字体背景颜色;文字颜色m字符串 \033[0m" 实例:echo -e "\n\n \t\t \033[4 ...
- 初识Django---视图
MTV模型 一.Django的MTV分别代表: Model(模型):负责业务对象与数据库的对象 Template(模板):负责如何把页面展示给用户 View(视图 ...
- Java中使用注释
在编写程序时,经常需要添加一些注释,用以描述某段代码的作用. 一般来说,对于一份规范的程序源代码而言,注释应该占到源代码的 1/3 以上.因此,注释是程序源代码的重要组成部分,一定要加以重视哦! Ja ...