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

思路就是sliding windows, A[i] = A[i-1] - nums[i-1] + nums[i+k-1], init: A[0] = sum(nums[:k])

Code:  T: O(n)  S; O(1)  using rolling arry to  O(1)

class Solution(object):
def findMaxAverage(self, nums, k):
ans, n = sum(nums[:k]), len(nums)
dp = [ans] + [0]
for i in range(1, n-k+1):
dp[i%2] = dp[i%2-1] - nums[i-1] + nums[i+k-1]
ans = max(ans, dp[i%2])
return ans*1.0/k

[LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)的更多相关文章

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

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

  2. [Leetcode]643. Maximum Average Subarray I

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

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

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

  4. 【Leetcode_easy】643. Maximum Average Subarray I

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

  5. [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二

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

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

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

  7. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. 643. Maximum Average Subarray

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

随机推荐

  1. python 中 try ...except

    捕捉异常 try: 下的代码段 即为 需要捕捉异常的代码段: except:  捕获某一模块的异常,须带异常模块名称,可带原因参数:except 下代码为该异常发生时,所执行的代码:一个try可对应多 ...

  2. Spark RDD Action 简单用例(二)

    foreach(f: T => Unit) 对RDD的所有元素应用f函数进行处理,f无返回值./** * Applies a function f to all elements of this ...

  3. cuteftp9破解及安装、使用

    一.破解: 参考:https://jingyan.baidu.com/article/ca00d56c4e43b2e99febcf70.html 1. 首先下载cuteftp替换文件及cuteftp9 ...

  4. [No0000132]正确使用密码加盐散列[译]

    如果你是一个 web 开发工程师,可能你已经建立了一个用户账户系统.一个用户账户系统最重要的部分是如何保护密码.用户账户数据库经常被黑,如果你的网站曾经被攻击过,你绝对必须做点什么来保护你的用户的密码 ...

  5. 关于初识Java整理

  6. AlphaRacks 2018年黑五 VPS $3.99/年

    发现这么久了这些链接还是能购买.算是捡了便宜了. 搭建shadowsocks非常合算. 我买了6.99美元的那个. VPS OVZ构架 1核/125MB/5GB/800GB流量/1 IPv4/OVZ/ ...

  7. ASP_NET实现界面无刷新的DropdownList两级联动效果

    所谓DropdownList联动,也就是在选一个DropdownList的时候使另外一个DropdownList的内容更新(如选省份时显示所属城市),按常规的方法那就是在第一个DropdownList ...

  8. CSP 通信顺序进程

    communicating sequential processes CSP 通信顺序进程 C.A.R.Hoare 1979 CSP是一种用来描述并行系统交互模式的形式语言,最早由C.A.R.Hoar ...

  9. JS实现倒计时(天数,时,分,秒)

    <!DOCTYPE html> <html> <head>   <meta charset="utf-8" >   <titl ...

  10. 洛谷P4198 楼房重建 单调栈+线段树

    正解:单调栈+线段树 解题报告: 传送门! 首先考虑不修改的话就是个单调栈板子题昂,这个就是 然后这题的话,,,我怎么记得之前考试好像有次考到了类似的题目昂,,,?反正我总觉着这方法似曾相识的样子,, ...