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 ...
随机推荐
- nfs共享存储
1.下载软件包 yum install nfs-utils nfs-utils-lib -y 2.编辑/etc/exports文件: 1.创建目录:mkdir -p /home/glance2.编辑e ...
- active scaffold
模板文件路径:/.rvm/gems/ruby-2.2.3/gems/active_scaffold-3.5.3/app/views/active_scaffold_overrides
- event driven model
http://www.jdon.com/eda.html http://blog.csdn.net/gykimo/article/details/9182287 事件代表过去发生的事件,事件既是技术架 ...
- qt_hal_verion
/opt/EmbedSky/B2/linux-3.0.35/drivers/mxc/gpu-viv/hal/kernel/inc/gc_hal_version.h 文件中的具体版本 export DI ...
- 八、golang文本操作、json协议
一.终端读写 操作终端相关文件语句常量,go所有的都是接口 os.Stdin:标准输入 os.Stdout:标准输入,只要实现输出这个接口,打印到终端, os.Stderr:标准错误输出 os.Ope ...
- mysql删除重复记录
Solution 1: Add Unique Index on your table: ALTER IGNORE TABLE `TableA` ADD UNIQUE INDEX (`member_id ...
- 字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)?
字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)? 根据我的了解,编码中有三个核心概念:1. 字符集(Character Set),可以说是一个抽象概念,字符的合集2. ...
- MVC 中 System.Web.Optimization 找不到引用
在MVC4的开发中,如果创建的项目为空MVC项目,那么在App_Start目录下没有BundleConfig.cs项的内容,在手动添加时在整个库中都找不到:System.Web.Optimizatio ...
- javascript的宿主环境
关于javascript语言,最有意思的是它必须要在一个宿主环境中运行,其中受欢迎的宿主环境当然就是浏览器了,但这并不是我们唯一的选择, javascript完全可以运行在服务器端,桌面以及富媒体环境 ...
- Installing StackTach
为StackTach创建database,默认使用MySql,也可以 在settings.py 文件中配置其他的. create stack db mysql -u root -p mysql> ...