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

  1. Output: 12.75
  2. 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记住检查终止条件 避免死循环

  1. class Solution {
  2. public double findMaxAverage(int[] nums, int k) {
  3. double maxAverage = Integer.MIN_VALUE;
  4. double sum = 0;
  5.  
  6. for(int left = 0, right = 0; right < nums.length; ++right) {
  7. if(right >= k) {
  8. sum -= nums[left];
  9. ++left;
  10. }
  11. sum += nums[right];
  12. if(right >= k-1) {
  13. maxAverage = Math.max(maxAverage, sum/k);
  14. }
  15. }
  16.  
  17. return maxAverage;
  18. }
  19. }

maxAvearge = maxSum/k

  1. class Solution {
  2. public double findMaxAverage(int[] nums, int k) {
  3. double sum = 0;
  4.  
  5. int right = 0;
  6. while(right < k) {
  7. sum += nums[right];
  8. ++right;
  9. }
  10. double maxSum = sum;
  11.  
  12. for(int left = 0; right < nums.length; ++right, ++left) {
  13. sum = sum + nums[right] - nums[left];
  14. maxSum = Math.max(maxSum, sum);
  15. }
  16.  
  17. return maxSum/k;
  18. }
  19. }

instead of using two variables, one variable is enough

  1. class Solution {
  2. public double findMaxAverage(int[] nums, int k) {
  3. double sum = 0;
  4.  
  5. for(int i = 0; i < k; ++i) {
  6. sum += nums[i];
  7. }
  8.  
  9. double maxSum = sum;
  10.  
  11. for(int i = k; i < nums.length; ++i) {
  12. sum = sum + nums[i] - nums[i-k];
  13. maxSum = Math.max(maxSum, sum);
  14. }
  15.  
  16. return maxSum/k;
  17. }
  18. }

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)

  1. class Solution {
  2. public double findMaxAverage(int[] nums, int k) {
  3. int sz = nums.length;
  4. int[] cumu = new int[sz];
  5.  
  6. cumu[0] = nums[0];
  7. for(int i = 1; i < sz; ++i) {
  8. cumu[i] = cumu[i-1] + nums[i];
  9. }
  10.  
  11. double maxSum = cumu[k-1];
  12. for(int i = k; i < sz; ++i) {
  13. maxSum = Math.max(maxSum, cumu[i] - cumu[i-k]);
  14. }
  15.  
  16. return maxSum/k;
  17. }
  18. }

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. MySQL实现中文拼音排序

    MySQL下新建一个表,默认采用utf8字符集,中文不能直接按照拼音进行排序. 例如以下语句: SELECT * FROM `tb_fixedassets` order by C_FANAME 得到的 ...

  2. canvas动画---- 太阳、地球、月球

    <div> <canvas id="canvas" width="500" height="500"></ca ...

  3. 八、AbstractFactory 抽象工厂模式

    设计原理: 代码清单: 抽象 Factory : public abstract class Factory { public static Factory getFactory(String cla ...

  4. day18 logging模块 sys shelve

    昨日回顾 re 正则表达式 匹配字符串 场景 例如:爬虫,密码规则验证,邮箱地址验证,手机号码 学习re主要学习的就是 那一堆特殊符号 hashlib hash是一种算法 lib表示库 该模块包含了一 ...

  5. CentOS 下环境变量

    1. 显示环境变量HOME(不要把一个#号输进去了,#代表用户名) # echo #HOME /home/redbooks 2. 设置一个新的环境变量hello # export HELLO=&quo ...

  6. LibreOJ 6285. 数列分块入门 9

    题目链接:https://loj.ac/problem/6285 其实一看到是离线,我就想用莫队算法来做,对所有询问进行分块,但是左右边界移动的时候,不会同时更新数字最多的数,只是后面线性的扫了一遍, ...

  7. JS判断变量类型

    typeof v 只能用于识别基础类型,不能识别对象 v instanceof MyClass 判断类型 Object.prototype.toString.call(v.p) === "[ ...

  8. vue axios请求/响应拦截器

    // main.js中配置 // 引入 axios import Axios from 'axios' // 这时候如果在其它的组件中,是无法使用 axios 命令的. // 但如果将 axios 改 ...

  9. 联机分析处理ROLAP、MOLAP和HOLAP区别(转)

    OLAP(on-Line Analysis Processing)是使分析人员.管理人员或执行人员能够从多角度对信息进行快速.一致.交互地存取,从而获得对数据的更深入了解的一类软件技术.OLAP的目标 ...

  10. js常用的数组,,字符串,,Math..正则方法

    数组 slice[start,end] 返回从原数组中指定开始下标到结束下标之间的项目组成新数组(不会影响原数组) splice() 1.删除功能:2个参数 , 起始位置 , 删除的项目 2.插入功能 ...