【leetcode】813. Largest Sum of Averages
题目如下:
解题思路:求最值的题目优先考虑是否可以用动态规划。记dp[i][j]表示在数组A的第j个元素后面加上第i+1 (i从0开始计数)个分隔符后可以得到的最大平均值,那么可以得到递归关系式: dp[i][j] = max(dp[i][j],dp[i-1][k]+float(sum(A[k+1:j+1]))/float(j-k)) 。
代码如下:
class Solution(object):
def largestSumOfAverages(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: float
"""
tl = [0] * len(A)
dp = []
for i in range(K):
dp.append(tl[:]) count = 0
for i in range(len(dp)):
for j in range(len(dp[i])-K+i+1):
count += A[j]
if i == 0:
dp[i][j] = float(count)/float(j+1)
continue
for k in range(j):
#print k,j,sum(A[k:j])
dp[i][j] = max(dp[i][j],dp[i-1][k]+float(sum(A[k+1:j+1]))/float(j-k)) return dp[-1][-1]
【leetcode】813. Largest Sum of Averages的更多相关文章
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- LC 813. Largest Sum of Averages
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the su ...
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- 【LeetCode】170. Two Sum III – Data structure design
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- 【LeetCode】#1 Two Sum
[Question] Given an array of integers, return indices of the two numbers such that they add up to a ...
- leetcode 813. Largest Sum of Averages
对于一个数组中的数进行分组,取每个组里的平均值进行加和的. 使用动态规划,其中dp[i][k]表示以i为结尾的有k个分组的,那么递推式为: dp[i][k]=dp[j][k-1]+(sum[i]-su ...
- 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
随机推荐
- leetcode-mid-Linked list- 116. Populating Next Right Pointers in Each Node
mycode 93.97% """ # Definition for a Node. class Node(object): def __init__(self, v ...
- Linux内核调试方法总结之dumpsys
dumpsys [用途]Android系统提供的dumpsys工具可以用来查看系统服务信息与状态. [使用说明] adb shell dumpsys <service> [<opti ...
- .bash_profile vs .bashrc
w http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html
- debugfs linux rm 删除 恢复 Attempt to read block from filesystem resulted in short read while opening filesystem
w 删除具有空字符的文件 反斜杠来转义下一个字符 rm -R Samples\ -\ Copy well@well:/home/etc/project/apilinux/MarketplaceWebS ...
- (转)Jquery之ShowLoading遮罩组件
本文转载自:http://www.cnblogs.com/eczhou/archive/2012/12/18/2822788.html 一.遮罩用途及效果 ShowLoading这个jQuery插件设 ...
- LeetCode 46——全排列
1. 题目 2. 解答 给定一个序列,序列中的任意一个数字都可以作为全排列的最后一位.然后,其余位置元素的确定便是剩余元素的一个全排列,也就是一个子问题. 例子中 [1, 2, 3] 的全排列,最后一 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第4节 ArrayList集合_19-ArrayList练习四_筛选集合
大集合里面循环装了20个int类型的随即数字 下面要自定义方法,这个方法专门负责筛选 遍历偶数的集合 重点是集合当做方法的参数,还能当做集合的返回值
- Samba 1.0服务部署
Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成. SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统.打印机及其他资源. ...
- python程序的模块与包
python的程序是由模块组成的,一个python文件就是一个模块,而模块一般由代码,函数,或者类组成.创建baiduHq.py模块(文件),在该模块中编写变量,函数,类,来说明在一个模块中,变量的输 ...
- Java核心技术
[Java核心技术36讲]1.谈谈你对Java平台的理解 2.Exception和Error有什么区别 3.谈谈final.finally.finalize有什么不同?4.强引用.软引用.弱引用.虚引 ...