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. Linux知识积累 (9) 创建用户、分配权限和更改所有者

    一.useradd和adduser 1.useradd命令: 用于Linux中创建的新的系统用户. useradd可用来建立用户帐号.帐号建好之后,再用passwd设定帐号的密码. 而可用userde ...

  2. apacheds的客户端

    Apache DS管理的JAVA实现 LdapConnection connection = new LdapNetworkConnection("localhost", 1038 ...

  3. Spark入门(1-1)什么是spark,spark和hadoop

    一.Spark是什么? Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎,可用来构建大型的.低延迟的数据分析应用程序. Spark是UC Berkeley AMP lab (加 ...

  4. ASP.NET Web API编程——序列化与内容协商

    1 多媒体格式化器 多媒体类型又叫MIME类型,指示了数据的格式.在HTTP协议中多媒体类型描述了消息体的格式.一个多媒体类型包括两个字符串:类型和子类型. 例如: text/html.image/p ...

  5. 翻译:JVM虚拟机规范1.7中的运行时常量池部分(二)

    本篇为JVM虚拟机规范1.7中的运行时常量池部分系列的第二篇. 4.4.4. The CONSTANT_Integer_info and CONSTANT_Float_info Structures ...

  6. VCS使用学习笔记(1)——Verilog相关的仿真知识

    本文主要学习Verilog的仿真特性,以及仿真器对Verilog的处理,算是对Verilog知识的增量学习.本文内容与我的另一篇博文(http://www.cnblogs.com/IClearner/ ...

  7. Vue框架下的node.js安装教程

    Vue框架下的node.js安装教程 python服务器.php  ->aphche.java ->tomcat.   iis -->它是一个可以运行JAVASCRIPTR 的运行环 ...

  8. Github学习笔记-不定时更新

    最近在搞一个外包项目,有个非常厉害的师兄带我,他写后端,我负责前端部分,项目合作需要用到github,这也是早晚要接触,记录下项目过程中对github/git使用的学习笔记. 1.在网上看了一些教程之 ...

  9. JS面向对象使用面向对象进行开发

      面向对象基础一之初体验使用面向对象进行开发 对 JS 中的面向对象的基础进行讲述, 初体验使用面向对象进行开发 主要内容是 面向对象的概念及特性 用面向对象的方式解决简单的标签创建实例 一些基础的 ...

  10. IdentityServer4-介绍大纲(译文)

    简介 IdentityServer4是一个基于ASP.NET CORE2使用OAuth2.0协议和OpenID Connect的框架 特性如下: Authentiaction作为一个Service 集 ...