[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 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 <=
k<=n<= 30,000. - 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)的更多相关文章
- LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [Leetcode]643. Maximum Average Subarray I
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- leetcode 643. Maximum Average Subarray I 子数组最大平均数 I
一.题目大意 https://leetcode.cn/problems/maximum-average-subarray-i/ 给你一个由 n 个元素组成的整数数组 nums 和一个整数 k . 请你 ...
- 【Leetcode_easy】643. Maximum Average Subarray I
problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- 643. Maximum Average Subarray I 最大子数组的平均值
[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
- [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 ...
- [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 ...
- 643. Maximum Average Subarray
Given an array consisting of \(n\) integers, find the contiguous subarray of given length \(k\) that ...
随机推荐
- MyISAM和InnoDB区别 及选择
MySQL默认采用的是MyISAM. MyISAM不支持事务,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提交,这样会影响速度,所以 ...
- MongoDB数据库基础
MongoDB简介 MongoDB是一种文档型的非关系型数据库(NoSQL),举例如下: {“foo”:,"greeting":"Hello,world!"} ...
- GIAC 2017全球互联网架构大会最新日程
12月22日至23日,高可用架构和msup联合主办的GIAC 全球互联网架构大会将于上海光大会展中心举行.GIAC 全球互联网架构大会是高可用架构技术社区推广的面向架构师.技术负责人及高端技术从业人员 ...
- [No0000154]详解为什么32位系统只能用4G内存.
既然是详解, 就从最基础的讲起了. 或者1来存储数据的, 所以Bit实际上可以看成存放1个二进制数字的1个位置.也就是说bit只有2种值, 0 或者 1, 所以1个bit能存放1个布尔类型的值(boo ...
- React中Props 和 State用法
React中Props 和 State用法 1.本质 一句话概括,props 是组件对外的接口,state 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下 ...
- 【编译原理】c++实现词法分析器
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- [Day1]常用Dos命令,Java相关描述及基础
1.常用的DOS命令 (1)返回上一级目录:cd.. (2)返回盘符根目录:cd\ (3)切换当前盘符: 盘符: (4)进入文件夹: cd 文件路径 (5)展示当前目录下的所有内容:D ...
- ORACLE network environment
监听程序 建立网络连接 要建立客户机或中间层连接,Oracle Net要求客户机 下列事项: 运行监听程序的主机 监听程序监视的端口 监听程序使用的协议 监听程序处理的服务名 Hostname/ip ...
- vue打包后出现"Failed to load resource: net::ERR_FILE_NOT_FOUND"错误
创建vue脚手架搭建项目之后,用npm run build经行打包,运行index.html后出现异常: 打开dist/index.html, 诸如这些的,引入是有问题的, 这边的全部是绝对路径,而本 ...
- git 用远程覆盖本地
git fetch --all git reset --hard origin/master