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. java和c#中String

    java中: c#中: 1.拼接字符串 sql语句中 in() str="'001','002','003'";至于产生string就这样  str1="'001'&qu ...

  2. MAT eclipse内存分析工具

      启动的时候提示: Failed to load the JNIshared library 解决办法: 查看配置文件:MemoryAnalyzer.ini --launcher.librarypl ...

  3. python 函数的定义及传参

    函数是一个通用的程序结构部件,是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 定义一个简单的函数: >>> def test(a): #创建一个函数,函数名是test ...

  4. Python程序打包—pyinstaller

    简介:PyInstaller是一个十分有用的第三方库,通过对源文件打包,Python程序可以在没有安装 Python的环境中运行,也可以作为一个独立文件方便传递和管理. PyInstaller的官方网 ...

  5. as3.0橡皮擦功能

    //主容器 var main:Sprite = new Sprite(); main.mouseEnabled = false; addChild(main) //临时容器(所有操作都将先画在临时容器 ...

  6. APP内的H5页面测试方法, 移动端的浏览器(例如UC浏览器)测试方法

    前言: 用appium做UI自动化,测试APP里面的H5和测试手机浏览器打开的H5的操作流程上是有所区别的.比如要测试APP内嵌的H5需要先操作appium启动APP,然后通过context切到web ...

  7. HDU-1002.大数相加(字符串模拟)

    本题大意:给出两个1000位以内的大数a 和b,让你计算a + b的值. 本题思路:字符串模拟就能过,会Java的大佬应该不会点进来...... 参考代码: #include <cstdio&g ...

  8. TOJ 3589 likaer的最长点距

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3589 时间限制(普通/Jav a) ...

  9. 207. Course Schedule(Graph; BFS)

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  10. TDD - 登录成功和失败

    /** * Created by Administrator on 2017-04-06. */ @RunWith(SpringJUnit4ClassRunner.class)@SpringBootT ...