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].

自己方法1 Brute Force:

窗口k = 4, 从数组左到右滑动, 代码省略.

\(O(n*k)\) time, \(O(1)\) extra space.

自个方法2 改进版的 Brute Force:

想法如下:

k = 4 的情形

1	12	-5	-6	50	3
^ ^ 1 12 -5 -6 50 3
^ ^
明显的, 第一行的 sum + 50 - 1 就是第二行的 sum
A[i] A[i-k]

\(O(n)\) time, \(O(1)\) extra space.

double findMaxAverage(vector<int>& A, int k) {
int i, sum = 0, max = INT_MIN;
for (i = 0; i < k; i++) sum += A[i];
max = max > sum ? max : sum;
for (i = k; i < A.size(); i++) {
sum = sum + A[i] - A[i - k];
max = max > sum ? max : sum;
}
return 1.0 * max / k;
}

643. Maximum Average Subarray的更多相关文章

  1. 【Leetcode_easy】643. Maximum Average Subarray I

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

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

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

  3. LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  4. [Leetcode]643. Maximum Average Subarray I

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  5. [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  6. 643. Maximum Average Subarray I

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  7. leetcode 643. Maximum Average Subarray I 子数组最大平均数 I

    一.题目大意 https://leetcode.cn/problems/maximum-average-subarray-i/ 给你一个由 n 个元素组成的整数数组 nums 和一个整数 k . 请你 ...

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

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

  9. Maximum Average Subarray II LT644

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

随机推荐

  1. windows7.0旗舰版安装后控制面板自带的Microsoft程序

    1.不要卸载,否则会出现安装其他软件时缺少动态链接库

  2. Spark快速入门

    Spark 快速入门   本教程快速介绍了Spark的使用. 首先我们介绍了通过Spark 交互式shell调用API( Python或者scala代码),然后演示如何使用Java, Scala或者P ...

  3. OAuth2.0学习(1-2)OAuth2.0的一个企业级应用场景 - 新浪开放平台微博OAuth2.0认证

    http://open.weibo.com/wiki/%E9%A6%96%E9%A1%B5 开发者可以先浏览OAuth2.0的接口文档,熟悉OAuth2.0的接口及参数的含义,然后我们根据应用场景各自 ...

  4. vue2.0+koa2+mongodb实现注册登录

    前言 前段时间和公司一个由技术转产品的同事探讨他的职业道路,对我说了一句深以为然的话: "不要把自己禁锢在某一个领域,技术到产品的转变,首先就是思维上的转变.你一直做前端,数据的交互你只知道 ...

  5. NHibernate优点和缺点:

    NHibernate优点: 1.完全的ORM框架. NHibernate对数据库结构提供了较为完整的封装,它将数据库模式映射为较完全的对象模型,支持封装,继续机制,功能较强大,比一般的ORM灵活性高. ...

  6. Python之黏包的解决

    黏包的解决方案 发生黏包主要是因为接收者不知道发送者发送内容的长度,因为tcp协议是根据数据流的,计算机操作系统有缓存机制, 所以当出现连续发送或连续接收的时候,发送的长度和接收的长度不匹配的情况下就 ...

  7. java集合小知识的复习

    *Map接口 Map<k,v>接口中接收两个泛型,key和value的两个数据类型 Map中的集合中的元素都是成对存在的每个元素由键与值两部分组成,通过键可以找对所对应的值.值可以重复,键 ...

  8. Hibernate(十二):HQL查询(一)

    概述 Hibernate提供了以下几种检索对象的方式 1)导航对象图检索方式:根据已经加载的对象导航到其他对象: 2)OID检索方式:按照对象的OID来检索对象: 3)HQL检索方式:使用面向对象的H ...

  9. flask 操作mysql的两种方式-sqlalchemy操作

    flask 操作mysql的两种方式-sqlalchemy操作 二.ORM sqlalchemy操作 #coding=utf-8 # model.py from app import db class ...

  10. [ABP]浅谈模块系统与 ABP 框架初始化

    在 ABP 框架当中所有库以及项目都是以模块的形式存在,所有模块都是继承自AbpModule 这个抽象基类,每个模块都拥有四个生命周期.分别是: PreInitialze(); Initialize( ...