[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 ...
随机推荐
- 别致的语言GO(GO语言初涉)
最近由于各种原因(好吧,其实是犯懒)已经许久没有再写新的博文了!最近正好在学习一门新的语言,所以正好记录一下自己的学习成果!最近利用每天晚上下班回来后的几小时,学习了Google开发的Go语言,算是对 ...
- ThinkPHP框架 AJAX方法返回 AJAX实现分页例子:
在模块控制器Controller文件夹里创建一个 FenyeController.class.php控制器 <?php namespace Admin\Controller; use Think ...
- http://202.194.116.8/webapps/portal/frameset.jsp?tab_id=_2_1&url=%2fwebapps%2fblackboard%2fexecute%2
http://202.194.116.8/webapps/portal/frameset.jsp?tab_id=_2_1&url=%2fwebapps%2fblackboard%2fexecu ...
- js删除Array数组中的某个元素
Array.prototype.indexOf = function (val) { ; i < this.length; i++) { if (this[i] == val) return i ...
- Caused by: java.sql.BatchUpdateException
Caused by: java.sql.BatchUpdateException: Table (%s) has been dropped, altered or renamed.解决方法重启项目
- js点击按钮保存数据到本地
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [No0000BC]ADO.NET中的几个主要对象
ADO 指 ActiveX 数据对象(ActiveX Data Objects). 从一个 ASP 页面内部访问数据库的通常的方法是: 创建一个到数据库的 ADO 连接 打开数据库连接 创建 ADO ...
- Netty入门教程——认识Netty
什么是Netty? Netty 是一个利用 Java 的高级网络的能力,隐藏其背后的复杂性而提供一个易于使用的 API 的客户端/服务器框架. Netty 是一个广泛使用的 Java 网络编程框架(N ...
- Adobe Flex初记
公司项目要用Flex,之前没有接触过,菜鸟只好白手起家,把项目拉下来的同时配置下Flex的环境,以下是一篇参考: http://blog.sina.com.cn/s/blog_4c4a24db0100 ...
- python 遍历list并删除部分元素
python 遍历list并删除部分元素https://blog.csdn.net/afgasdg/article/details/82844403有两个list,list_1 为0-9,list_2 ...