Maximum Average Subarray I LT643
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].
Idea 1: window with width k. Assume we already know the sum of element from index left to index right(right = left + k -1), now how to extend the solution from the index left + 1 to index right + 1? Use two pointers moving from left to right, add the element on the right end and take away the element on the left end.
Time complexity: O(n), one pass
Space complexity: O(1)
写loop记住检查终止条件 避免死循环
- class Solution {
- public double findMaxAverage(int[] nums, int k) {
- double maxAverage = Integer.MIN_VALUE;
- double sum = 0;
- for(int left = 0, right = 0; right < nums.length; ++right) {
- if(right >= k) {
- sum -= nums[left];
- ++left;
- }
- sum += nums[right];
- if(right >= k-1) {
- maxAverage = Math.max(maxAverage, sum/k);
- }
- }
- return maxAverage;
- }
- }
maxAvearge = maxSum/k
- class Solution {
- public double findMaxAverage(int[] nums, int k) {
- double sum = 0;
- int right = 0;
- while(right < k) {
- sum += nums[right];
- ++right;
- }
- double maxSum = sum;
- for(int left = 0; right < nums.length; ++right, ++left) {
- sum = sum + nums[right] - nums[left];
- maxSum = Math.max(maxSum, sum);
- }
- return maxSum/k;
- }
- }
instead of using two variables, one variable is enough
- class Solution {
- public double findMaxAverage(int[] nums, int k) {
- double sum = 0;
- for(int i = 0; i < k; ++i) {
- sum += nums[i];
- }
- double maxSum = sum;
- for(int i = k; i < nums.length; ++i) {
- sum = sum + nums[i] - nums[i-k];
- maxSum = Math.max(maxSum, sum);
- }
- return maxSum/k;
- }
- }
Idea 1.a Use cumulative sum, the sum of subarray = cumu[i] - cumu[i-k].
Time complexity: O(n), two passes
Space complexity: O(n)
- class Solution {
- public double findMaxAverage(int[] nums, int k) {
- int sz = nums.length;
- int[] cumu = new int[sz];
- cumu[0] = nums[0];
- for(int i = 1; i < sz; ++i) {
- cumu[i] = cumu[i-1] + nums[i];
- }
- double maxSum = cumu[k-1];
- for(int i = k; i < sz; ++i) {
- maxSum = Math.max(maxSum, cumu[i] - cumu[i-k]);
- }
- return maxSum/k;
- }
- }
Maximum Average Subarray I LT643的更多相关文章
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- Maximum Average Subarray
Given an array with positive and negative numbers, find the maximum average subarray which length sh ...
- Maximum Average Subarray II LT644
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- 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 ...
- [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
Description Given an array with positive and negative numbers, find the maximum average subarray whi ...
- 【Leetcode_easy】643. Maximum Average Subarray I
problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...
随机推荐
- MySQL实现中文拼音排序
MySQL下新建一个表,默认采用utf8字符集,中文不能直接按照拼音进行排序. 例如以下语句: SELECT * FROM `tb_fixedassets` order by C_FANAME 得到的 ...
- canvas动画---- 太阳、地球、月球
<div> <canvas id="canvas" width="500" height="500"></ca ...
- 八、AbstractFactory 抽象工厂模式
设计原理: 代码清单: 抽象 Factory : public abstract class Factory { public static Factory getFactory(String cla ...
- day18 logging模块 sys shelve
昨日回顾 re 正则表达式 匹配字符串 场景 例如:爬虫,密码规则验证,邮箱地址验证,手机号码 学习re主要学习的就是 那一堆特殊符号 hashlib hash是一种算法 lib表示库 该模块包含了一 ...
- CentOS 下环境变量
1. 显示环境变量HOME(不要把一个#号输进去了,#代表用户名) # echo #HOME /home/redbooks 2. 设置一个新的环境变量hello # export HELLO=&quo ...
- LibreOJ 6285. 数列分块入门 9
题目链接:https://loj.ac/problem/6285 其实一看到是离线,我就想用莫队算法来做,对所有询问进行分块,但是左右边界移动的时候,不会同时更新数字最多的数,只是后面线性的扫了一遍, ...
- JS判断变量类型
typeof v 只能用于识别基础类型,不能识别对象 v instanceof MyClass 判断类型 Object.prototype.toString.call(v.p) === "[ ...
- vue axios请求/响应拦截器
// main.js中配置 // 引入 axios import Axios from 'axios' // 这时候如果在其它的组件中,是无法使用 axios 命令的. // 但如果将 axios 改 ...
- 联机分析处理ROLAP、MOLAP和HOLAP区别(转)
OLAP(on-Line Analysis Processing)是使分析人员.管理人员或执行人员能够从多角度对信息进行快速.一致.交互地存取,从而获得对数据的更深入了解的一类软件技术.OLAP的目标 ...
- js常用的数组,,字符串,,Math..正则方法
数组 slice[start,end] 返回从原数组中指定开始下标到结束下标之间的项目组成新数组(不会影响原数组) splice() 1.删除功能:2个参数 , 起始位置 , 删除的项目 2.插入功能 ...