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 ...
随机推荐
- Centos 6.3 Realtek Audio Driver Compile
/**************************************************************************** * Centos 6.3 Realtek A ...
- PHP 去掉文文文件中的回车与空格
文本文件fff.txt中去除回车与空格: $aa = file_get_contents('./fff.txt'); $bb = str_replace(array("\r\n", ...
- 【学习】JennyHui学英语 - 生词积累
(主要是来自于普特英语) 平常积累 20140820 20140826 VOA慢速英语生词积累 2014-10-08 2014-10-11 2014-10-13 2014-10-14 2014-10- ...
- 感觉有变良好的第一次电面——yahoo北京测试实习生
一个月之前投的岗位.都已经忘了.昨天突然接到电话说今天下午3点电面. 立马又开始忐忑起来,整理了下项目啊,推荐系统相关知识啥的,跑到欧巴桑寝室去电面电面. 3点很准时的电话来了,是个女面试官. 一上来 ...
- YII缓存之数据缓存
1.开启缓存组件 2. ================ 二 先在配置文件components数组中加上: 'cache'=>array( 'class'=>'CFileCache'), ...
- IT售前经验谈
在IT界,成功的完成一个项目需要销售人员.售前人员.项目实施人员(开发人员).售后服务人员等密切协作.本文从售前技术支持人员的角度,对售前技术支持工作的过程进行了描述,根据作者在售前的经验,提出了各环 ...
- Windows Communication Foundation (WCF)和Windows CardSpace的示例程序
微软公司昨天发布了一个Windows Communication Foundation (WCF)和Windows CardSpace的示例程序包,内容极为丰富,从最简单的Hello World到复杂 ...
- Java面试题:栈和队列的实现
面试的时候,栈和队列经常会成对出现来考察.本文包含栈和队列的如下考试内容: (1)栈的创建 (2)队列的创建 (3)两个栈实现一个队列 (4)两个队列实现一个栈 (5)设计含最小函数min()的栈,要 ...
- 用 Python 实现文件查找
用 Python 实现文件查找(BIF实现及队列实现) (1)利用内置函数实现文件查找 1.功能:返回用户输入的文件的绝对路径 2.设计思路: (1)用户输入在哪个盘进行查找 (2)遍历此盘文件,若为 ...
- JAVA 比较两个日期相差的天数
在实际的应用中,我们经常会比较两个日期相差的天数,下面我们通过java方法判断两个日期所差的额天数. 具体内容,请看下面的代码: package com.jd.jr.fclient.test; imp ...