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. 180425、cookie工具类

    package com.thinkgem.jeesite.common.utils; import java.io.UnsupportedEncodingException; import java. ...

  2. textarea 固定大小,滚动条,限制拖动,文字对齐

    取值:$("#ID").val(); 控制大小:加width,height限制(style="width:100px;height:200px;");或row, ...

  3. TOP100summit【分享实录-网易】构建云直播分发网络

    本篇文章内容来自2016年TOP100summit网易视频云.网易杭州研究院服务端技术专家邵峰的案例分享.编辑:Cynthia 邵峰:网易视频云.网易杭州研究院服务端技术专家浙江大学计算机专业博士毕业 ...

  4. VIVE pro和hololens购买调研

    VIVE pro专业版是一款虚拟现实头盔 VIVE Pro专业版头显 ¥6,488 仅含头显,不含定位器及操控手柄送VIVEPORT会员服务2个月 订购VIVE Pro专业版头显,加 ¥2,400可得 ...

  5. 阿里云mysql远程登录报ERROR 2027(HY000)

    mysql远程登录的命令是: mysql -h数据库地址 -u用户名 -p 但是用这个命令在登录阿里云的mysql时,会报ERROR 2027 (HY000): Malformed packet

  6. hive拉链表

    前言 本文将会谈一谈在数据仓库中拉链表相关的内容,包括它的原理.设计.以及在我们大数据场景下的实现方式. 全文由下面几个部分组成:先分享一下拉链表的用途.什么是拉链表.通过一些小的使用场景来对拉链表做 ...

  7. iOS RSA非对称加密测试流程

    非对称加密需要两把钥匙:公钥和秘钥. 单向加密:一般情况下服务器会持有秘钥和公钥,那该怎么使用呢?以注册场景为例,最初服务器持有公钥和密钥. 用户注册时不是直接发送用户名,密码,验证码等明文信息给服务 ...

  8. 异常 java.net.ConnectException: Connection refused: no further information

    java.net.ConnectException: Connection refused: no further information at sun.nio.ch.SocketChannelImp ...

  9. TCP/IP具体解释--TCP首部的TimeStamp时间戳选项

    TCP应该是以太网协议族中被应用最为广泛的协议之中的一个,这里就聊一聊TCP协议中的TimeStamp选项.这个选项是由RFC 1323引入的,该C建议提交于1992年.到今天已经足足有20个年头.只 ...

  10. 【SQL】where 后不可以接聚合函数,都哪些是聚合函数?

    where 后不可以接聚合函数,比如函数:SUM(count),AVG(count),MIN(count),MAX(count)