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 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的更多相关文章
- 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】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- [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 ...
- [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 ...
- 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 813. Largest Sum of Averages
对于一个数组中的数进行分组,取每个组里的平均值进行加和的. 使用动态规划,其中dp[i][k]表示以i为结尾的有k个分组的,那么递推式为: dp[i][k]=dp[j][k-1]+(sum[i]-su ...
- 【leetcode】813. Largest Sum of Averages
题目如下: 解题思路:求最值的题目优先考虑是否可以用动态规划.记dp[i][j]表示在数组A的第j个元素后面加上第i+1 (i从0开始计数)个分隔符后可以得到的最大平均值,那么可以得到递归关系式: d ...
- 动态规划-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 + . ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- Cisco AP-Flexconnect配置结果
一个部署Flexconnect AP(印度)注册到远端WLC(上海)的例子:1.连接AP的交换机接口的配置: nterface GigabitEthernet0/4switchport access ...
- Update(Stage4):Spark Streaming原理_运行过程_高级特性
Spark Streaming 导读 介绍 入门 原理 操作 Table of Contents 1. Spark Streaming 介绍 2. Spark Streaming 入门 2. 原理 3 ...
- pip-9.0.1更新到pip-10.0.1遇到的问题
使用 pip 安装第三方库时,报错: You are using pip version 9.0.3, however version 10.0.1 is available. You should ...
- jsp格式化日期
1.先引入JSTL库 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> ...
- 集成PHP应用和SOLR
集成 PHP 应用和 Solr 搜索引擎 您的评价: 收藏该经验 你为什么需要搜索引擎?单纯数据库不够用?如果你只是创建小网站,数据库就够了.但当你创建中性或大型应用的时 ...
- Python 使用pillow 操作图像
文档:https://pillow.readthedocs.io/en/stable/index.html 计算机图像基础 颜色和RGBA值 计算机程序通常将图像中的颜色表示为 RGBA 值.RGBA ...
- 如何优雅的使用python中的代码注释
在编写代码时,确保您的代码易于被其他人理解时很重要的,给变量,函数起合适的名字以及合理的组织代码都是很好的方法. 使用代码注释时增加代码可读性的另一种方便简单且重要的方法! 1.为什么代码注释如此重要 ...
- SSM日常报错
mybatis配置时出现org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 解决方法: po ...
- luogu P4013 数字梯形问题
三倍经验,三个条件,分别对应了常见的3种模型,第一种是限制每个点只能一次且无交点,我们可以把这个点拆成一个出点一个入点,capacity为1,这样就限制了只选择一次,第二种是可以有交点,但不能有交边, ...
- C++11并发编程4------线程间共享数据
举个例子: 刚参加工作的你,只能租房住,嫌房租贵就和别人合租了,两个人住一起只有一个洗手间,每天早上起床的时候,如果你室友在洗手间,你就只能等着,如果你强行进去,那画面就不可描述了.同样的问题,如果多 ...