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. 1 <= k <= n <= 30,000.
  2. 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的更多相关文章

  1. [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  2. Maximum Average Subarray

    Given an array with positive and negative numbers, find the maximum average subarray which length sh ...

  3. Maximum Average Subarray II LT644

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  4. leetcode644. Maximum Average Subarray II

    leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...

  5. 643. Maximum Average Subarray I 最大子数组的平均值

    [抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...

  6. [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  7. LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)

    643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...

  8. Maximum Average Subarray II

    Description Given an array with positive and negative numbers, find the maximum average subarray whi ...

  9. 【Leetcode_easy】643. Maximum Average Subarray I

    problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...

随机推荐

  1. Feign Hystrix

    1.Feign整合Hystrix 添加依赖 编写接口与实现回退 1.1.调用者引入依赖 <!-- Feign --> <dependency> <groupId>o ...

  2. 大数据入门到精通2--spark rdd 获得数据的三种方法

    通过hdfs或者spark用户登录操作系统,执行spark-shell spark-shell 也可以带参数,这样就覆盖了默认得参数 spark-shell --master yarn --num-e ...

  3. 解题3(CoordinateCalculate)

    题目描述 开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动.从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面. 输入: 合 ...

  4. weblogc SessionData.getNextId性能问题

    参考:https://www.cnblogs.com/lemon-flm/p/7396627.html weblogic运行中持续报weblogic.servlet.internal.session. ...

  5. django搭建的站点,通过localhost能访问,但是通过ip不能访问

    问题:使用ip访问不了django站点,只能用127.0.0.1访问     解决方法:启动服务时ip使用0.0.0.0   使用gunicorn启动 gunicorn -w4 -b0.0.0.0:8 ...

  6. Gym - 100989G 二分

    链接:ECJTU 2018 Summer Training 1 - Virtual Judge  https://vjudge.net/contest/236677#problem/G 谷歌翻译: 距 ...

  7. ACM-ICPC 2018 南京赛区网络预赛 L.Magical Girl Haze(分层最短路)

    There are N cities in the country, and M directional roads from u to v(1≤u,v≤n). Every road has a di ...

  8. PTA 7-6 列出连通集(深搜+广搜)

    给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1 ...

  9. [leetcode]304. Range Sum Query 2D - Immutable二维区间求和 - 不变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  10. 20165315 C语言学习情况与Java学习目标

    20165315 C语言学习情况与Java学习目标 一.出色技能的获取经验 我从小便是一个中规中矩的人,在很多方面都是让成绩尽量保持在前百分之二十到三十这个范围内,比如我比较擅长的唱歌和乒乓球,但也不 ...