【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 ...
随机推荐
- SpringBoot系列:五、SpringBoot使用Actuator
Actuator为springboot提供了运行状态监控的功能 通过集成它我们可以试试获取到应用程序的运行信息 首先,在pom.xml中引入起步依赖 <dependency> <gr ...
- js将html转换为纯文本
document.body.textContent//firefox浏览器 document.body.innerText//适用ie webkit浏览器 document.body.innerHTM ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第6节 static静态_13_静态static关键字修饰成员方法
static关键字 用来修饰方法的情况 任何使用这个静态方法呢? 可以直接通过对象名称的方式进行调用. 更好的写法是通过类名称进行调用. 分别定义成员变量和静态变量 静态方法里面不能用this
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_3_Map接口中的常用方法
这个方法比较特殊,它的返回值是V他也就是Vlaue get remove containsKey: put value没有重复的所以v1返回的是null key值有重复,所以会返回被替换的值,范冰冰1 ...
- spring,springMVC的优点和区别
spring 是是一个开源框架,是为了解决企业应用程序开发,功能如下◆目的:解决企业应用开发的复杂性◆功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能◆范围:任何Java应用简单 ...
- mooc-IDEA 项目/文件之间跳转--002
二.IntelliJ IDEA -项目之间跳转 1.Next Project Window :跳转到下一个项目 [ ctrl+alt+) ] 2.Previous Project Window:跳转到 ...
- 前端 CSS 一些标签默认有padding
一个html body标签 默认有 margin外边距属性 比如ul标签,有默认的padding-left值. 那么我们一般在做站的时候,是要清除页面标签中默认的padding和margin.以便于我 ...
- python中pycharm中.py文件调用一个.py文件的函数
在相同文件夹内调用函数: file1.py def add(x,y): print('和为:%d'%(x+y)) file2.py import A A.add(1,2)
- jenkins 启动报错
daemon: fatal: refusing to execute unsafe program: /usr/java/jdk1.8.0/bin/java (/usr/java/jdk1.8.0/b ...
- CentOS7 修复MBR引导
为了达到实验目的,首先破坏MBR引导bootloader 重启系统发现系统进不去了,这正是我们想要的 重启进入系统救援模式,输入以下命令重建MBR引导bootloader 重启,可以正常引导进入系统