"""
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
"""
"""
这题目的思想是基于一个事实:一个数组分割之后肯定是份数越多得到的平均值就越大。
我们假设dp[i][k]表示为数组A【0~i】分割k份得到的最大平均值。
因为最后一个分割点可以出现在任何部分(实际上也得出现在大于k-1个位置之后),所以
dp[i][k]=max(dp[i][k], dp[j][k-1]+sum(j~i)/(i-j)) 其中j在j之前的地方j<i,
因此可以把这个递推式理解为重新选择最后一个分割点的最大处。
当然,要迅速完成sum段求和还需要一个小函数。
传送门:https://blog.csdn.net/weixin_37373020/article/details/81543069
"""
class Solution:
def largestSumOfAverages(self, A, K):
sums = [0]
for a in A:
sums.append(sums[-1]+a) #sums[i] = A[0]+A[1]+...A[i-1]
def gap_sum(i, j):
return sums[j]-sums[i]
dp = [[0 for x in range(K+1)] for y in range(len(A)+1)]
for i in range(1, len(A)+1):
dp[i][1] = gap_sum(0, i)/i
for k in range(2, K+1):
for i in range(k, len(A)+1):
for j in range(i):
dp[i][k] = max(dp[i][k], dp[j][k-1]+gap_sum(j, i)/(i-j))
return dp[len(A)][K]

leetcode813 Largest Sum of Averages的更多相关文章

  1. 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 ...

  2. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  3. [Swift]LeetCode813. 最大平均值和的分组 | 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 ...

  4. [LeetCode] 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 ...

  5. 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 ...

  6. leetcode 813. Largest Sum of Averages

    对于一个数组中的数进行分组,取每个组里的平均值进行加和的. 使用动态规划,其中dp[i][k]表示以i为结尾的有k个分组的,那么递推式为: dp[i][k]=dp[j][k-1]+(sum[i]-su ...

  7. 【leetcode】813. Largest Sum of Averages

    题目如下: 解题思路:求最值的题目优先考虑是否可以用动态规划.记dp[i][j]表示在数组A的第j个元素后面加上第i+1 (i从0开始计数)个分隔符后可以得到的最大平均值,那么可以得到递归关系式: d ...

  8. 动态规划-Largest Sum of Averages

    2018-07-12 23:21:53 问题描述: 问题求解: dp[i][j] : 以ai结尾的分j个部分得到的最大值 dp[i][j] = max{dp[k][j - 1] + (ak+1 + . ...

  9. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. Latin-1字符集

    ISO Latin-1字符集是Unicode字符集的一个子集,对应于IE4+中Unicode字符指令表的前256个条目.下面表格中详细提供了每个字符及字符的十进制编码和HTML已命名实体.其中Unic ...

  2. Python学习第二十六课——PyMySql(python 链接数据库)

    Python 链接数据库: 需要先安装pymysql 包 可以设置中安装,也可以pip install pymysql 安装 加载驱动: import pymysql # 需要先安装pymysql 包 ...

  3. win10 桌面快捷键技术

    win 10 的 快捷键技术,使用还是挺流畅舒适的: Windows10技术新增键盘快捷键汇总: 1.贴靠窗口:Win +左/右> Win +上/下>窗口可以变为1/4大小放置在屏幕4个角 ...

  4. pandas help

    1. read_csv read_csv方法定义: pandas.read_csv(filepath_or_buffer, sep=', ', delimiter=None, header='infe ...

  5. netty集成springboot

    一 前言 springboot 如何集成netty实现mapper调用不为null的问题让好多读者都头疼过,知识追寻者发了一点时间做了个基本入门集成应用给读者们指明条正确的集成方式,我相信,只要你有n ...

  6. Python编程使用PyQT制作视频播放器

    最近研究了Python的两个GUI包,Tkinter和PyQT.这两个GUI包的底层分别是Tcl/Tk和QT.相比之下,我觉得PyQT使用起来更加方便,功能也相对丰富.这一篇用PyQT实现一个视频播放 ...

  7. 「快学springboot」SpringBoot整合freeMark模板引擎

    前言 虽然现在流行前后端分离开发和部署,但是有时候还是需要用到服务端渲染页面的.比如:需要考虑到SEO优化等问题的时候,FreeMark其实还是很有作用的.本人的博客本来是用React开发的,但是后来 ...

  8. [网络转载 ]LoadRunner技巧之THML与URL两种录制模式分析

    loadrunner自带网站的访问 Html_based script模式 Action() { web_url("WebTours", "URL=http://127. ...

  9. 笔记-Python-性能优化

    笔记-Python-性能优化 1.      开始 1.1.    python性能差么? 做一个判断前,先问是不是. python运行效率低是事实. 1.2.    为什么? 原因: Python是 ...

  10. Windows远程“要求的函数不受支持”解决办法

    解决方法: 开启组策略中远程桌面链接安全层.1.开始-运行-gpedit.msc,进入组策略编辑器:2.找到左侧边栏计算机配置-管理模板-Windows组件-远程桌面服务-远程桌面会话主机-安全项:3 ...