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. javascript学习笔记(四):DOM操作HTML

    当网页被加载时,浏览器会创建页面的文档对象模型Document Object Model,简称DOM Dom操作html 1:改变页面中所有HTML元素 2:改变页面中所有HTML属性 3:改变页面中 ...

  2. Django之crm

    crm注册 crm注册Form from django import forms from crm import models from django.core.exceptions import V ...

  3. layer数据表格换行

    在使用layer数据表格的时候,默认是不可以换行的.这样显示 改动后 数据格式为   aa<br>bb就会显示为换行 比如我们的字符串是    a<br>b 这样的字符串浏览器 ...

  4. 【工具引入】uiautomatorviewer 查找元素后自动生成代码

    缘起 公司部门调整PC部门和无线部门合并,原本负责主站PC端自动化的同事需要马上上手安卓,IOS自动化.对于初次接触移动端的测试者来说,跨度还是有点大的.加之人员有些变动,不得不搞个工具降低学习成本, ...

  5. PAT L2-016 愿天下有情人都是失散多年的兄妹(深搜)

    呵呵.大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人.父母.祖父母.曾祖父母.高祖父母)则不可通婚.本题就请你帮助一对有情人判断一下,他们究竟是否可以成婚? 输入格式: 输入 ...

  6. python + selenium 学习笔记 -摘要

    一.浏览器操作相关 from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() # 窗口最大 ...

  7. java NIO Buffer 详解(1)

    1.java.io  最为核心的概念是流(stream),面向流的编程,要么输入流要么输出流,二者不可兼具: 2.java.nio 中拥有3个核心概念: Selector Channel, Buffe ...

  8. MVC 学习(一)Linq to Entities 简单Demo

    Linq定义了一组标准查询符号,标准查询符允许查询作用于所有基于IEnumerable<T>接口源. 我们看看LINQ的总体架构.如下图所示 EF4.1 数据操作及持久化,常见的是Data ...

  9. Aspose.Words五 MergeField

    通过MegerField来循环,将数据保存到dataset的table中,dataset通过关联datarelation字段来指定主从表关系.模板中通过标签TableStart和TableEnd来框定 ...

  10. idea spring-boot gradle mybatis 搭建开发环境

    使用工具idea 2017.2开发,gradle构建项目,使用的技术有spring-boot.mybatis 1.新建项目 说明:1.src为源码路径,开发主要在src下 2.src/main/jav ...