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. SpringCloud的Hystrix(一) 一个消费者内的两个服务监控

    一.概念与定义 1.服务雪崩 在微服务架构中,整个系统按业务拆分出一个个服务,这些服务之间可以相互调用(RPC),为了保证服务的高可用,单个服务通常会集群部署. 但是由于网络原因或自身原因,服务并不能 ...

  2. 超简单的jQuery前台分页,不需导包

    今天我们介绍一个不需要导分页包的,非常容易上手的分页+模糊查询功能.接下来先介绍分页功能: 首先第一步,你要有个要去分页的列表.我这里敲了个简单的图书管理,作为展示的基础,它的列表为异步提交,由两部分 ...

  3. Window7下安装Jmeter

    解压Jmeter,存放位置为D:\apache-jmeter-2.11 用户变量——>新建变量名JMETER_HOME,变量值为存放目录 系统变量——>添加;%JMETER_HOME%/l ...

  4. mongodb 索引的基本命令

    mongodb的索引: 在数据量超大的时候,能够极大的增快查询速率,但是会降低更新效率.建立索引: db.集合.ensureIndex({属性:1}) //1代表升序 -1代表降序 db.集合.ens ...

  5. jdk的server模式修改无效(关于client和server模式)

    本机为64位操作系统,64位jdk,win10. 修改C:\Program Files\Java\jre8\lib\amd64\jvm.cfg无效. 我的文件的内容为: 原因参考如下: http:// ...

  6. hdu1568 Fibonacci---前4位

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1568 题目大意: 求斐波那契数列第i项的前四位.i<1e8 思路: 由于数据范围大,不可能打表 ...

  7. FTP下载文件

    linux命令方式下载 step1: >>ftp ip port 根据提示输入用户名 根据提示输入用户密码 >>cd 目录(重要:一定要进入文件所在的目录) >>g ...

  8. 10_Python函数方法加深_Python编程之路

    上节课已经简单的跟大家讲了如何定义一个方法,但是并没有深入去讲,这一节我们继续来学习定义方法中需要注意的几点 默认参数 前面我们讲到定义一个方法时是可以传递参数的,除了这个功能,实际上python在定 ...

  9. [LeetCode] Sliding Window Median 滑动窗口中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  10. python2.7-巡风源码阅读

    推荐个脚本示例网站:https://www.programcreek.com/python/example/404/thread.start_new_thread,里面可以搜索函数在代码中的写法,只有 ...