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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
sliding window 最一般的步骤就是右加左减,直接写就行了
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
sliding window : 框的长度恒定
[关键模板化代码]:
for (int i = k; i < nums.length; i++) {
sum = sum + nums[i] - nums[i - k];
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
644. Maximum Average Subarray II 长度可以更长。贼复杂的二分法,有点无聊。
[代码风格] :
class Solution {
public double findMaxAverage(int[] nums, int k) {
//ini, calculate the first k
int sum = 0;
for (int i = 0; i < k; i++) {
sum += nums[i];
}
int max = sum; //for loop, sliding window
for (int i = k; i < nums.length; i++) {
sum = sum + nums[i] - nums[i - k];
max = Math.max(max, sum);
} //return
return max / 1.0 / k;
}
}
643. Maximum Average Subarray I 最大子数组的平均值的更多相关文章
- 【Leetcode_easy】643. Maximum Average Subarray I
problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- 643. Maximum Average Subarray
Given an array consisting of \(n\) integers, find the contiguous subarray of given length \(k\) that ...
- [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] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [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 ...
随机推荐
- PS基础教程:[8]蒙版使用实例
蒙版是PS中我们最常使用的工具,使用蒙版合成图片可以制作出非常绚丽的效果,并且看上去感觉很真,下面就以一个实例为大家分享一下蒙版的基本使用. 方法 1.在PS中打开准备好的素材,这里主要介绍蒙版的使用 ...
- self = [super init]
Objective-C的推荐init方法写法如下: - (id) init { if(self = [super init]) { //为子类增加属性进行初始化 } return self; } 返回 ...
- SCARA——OpenGL入门学习四(颜色)
OpenGL入门学习[四] 本次学习的是颜色的选择.终于要走出黑白的世界了~~ OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. 无论哪种颜色模式,计算机都必须为每一个像素保存一些数 ...
- [Luogu4169][Violet]天使玩偶/SJY摆棋子
luogu 题意 一个平面上有\(n\)个点,\(m\)次操作,每次新增一个点,或者是询问离某个点最近的点的距离.这里的距离是曼哈顿距离. \(n,m\le3*10^5\) sol 写一发\(CDQ\ ...
- nmon的使用
Linux性能评测工具之一:nmon篇 分类: 敏捷实践2010-06-08 11:27 7458人阅读 评论(0) 收藏 举报 工具linuxfilesystemsaixx86excel 目录( ...
- selenium - 三种元素等待
1.sleep 休眠方法 sleep()由python的time模块提供. 当执行到sleep()方法时,脚本会定时休眠所设置的时长,sleep()方法默认参数是s(秒),sleep(2) 表示休眠2 ...
- flume采集log4j日志到kafka
简单测试项目: 1.新建Java项目结构如下: 测试类FlumeTest代码如下: package com.demo.flume; import org.apache.log4j.Logger; pu ...
- JDK 8 - Lambda Expression 的优点与限制
我们知道 JDK 8 新增了 Lambda Expression 这一特性. JDK 8 为什么要新增这个特性呢? 这个特性给 JDK 8 带来了什么好处? 它可以做什么?不可以做什么? 在这篇文章, ...
- mina2中IoHandler
IoHandler 当我们通过IoSession执行相关操作的时候,如写数据,这些事件会触发Mina框架抽象的IoService实例,从而调用Mina框架底层的相关组件进行处理.这时,配置的IoHan ...
- windows-x64下安装python3.6
1.下载python3安装包,注意要安装windowsx64的exe安装包.python-3.6.2-amd64.exe 2.设置window的环境变量:C:\Users\您设置的windows用户名 ...