813. Largest Sum of Averages
We partition a row of numbers
A
into at mostK
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.
Note:
1 <= A.length <= 100
.1 <= A[i] <= 10000
.1 <= K <= A.length
.- Answers within
10^-6
of the correct answer will be accepted as correct.
Approach #1: DFS + Memory. [Java]
class Solution {
public double largestSumOfAverages(int[] A, int K) {
int n = A.length;
double[][] memo = new double[K+1][n+1];
double[] sum = new double[n+1];
for (int i = 1; i <= n; ++i)
sum[i] += sum[i-1] + A[i-1];
return LSA(n, K, memo, sum); } public double LSA(int n, int k, double[][] memo, double[] sum) {
if (memo[k][n] > 0) return memo[k][n];
if (k == 1) return sum[n] / n;
for (int i = k - 1; i < n; ++i) {
memo[k][n] = Math.max(memo[k][n], LSA(i, k-1, memo, sum) + (sum[n] - sum[i]) / (n - i));
}
return memo[k][n];
}
}
Approach #2: DP. [C++]
class Solution {
public:
double largestSumOfAverages(vector<int>& A, int K) {
int n = A.size();
vector<vector<double>> dp(K+1, vector<double>(n+1, 0.0));
vector<double> sum(n+1, 0.0);
for (int i = 1; i <= n; ++i) {
sum[i] = sum[i-1] + A[i-1];
dp[1][i] = static_cast<double>(sum[i]) / i;
} for (int k = 2; k <= K; ++k)
for (int i = k; i <= n; ++i)
for (int j = k-1; j < i; ++j)
dp[k][i] = max(dp[k][i], dp[k-1][j] + (sum[i] - sum[j]) / (i - j)); return dp[K][n];
}
};
Refernce:
http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-813-largest-sum-of-averages/
813. Largest Sum of Averages的更多相关文章
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 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
对于一个数组中的数进行分组,取每个组里的平均值进行加和的. 使用动态规划,其中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 ...
- [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 ...
- leetcode813 Largest Sum of Averages
""" We partition a row of numbers A into at most K adjacent (non-empty) groups, then ...
- 动态规划-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 ...
随机推荐
- OAuth2.0 Owin 授权问题
http://www.cnblogs.com/dudu/p/4569857.html OAuth2.0 一.什么是OAuth OAuth是一个关于授权(Authorization)的开放网络标准,目前 ...
- 【z】Storm - the world's best IDE framework for .NET
http://www.codeproject.com/Articles/42799/Storm-the-world-s-best-IDE-framework-for-NET Storm - the w ...
- pkg_config找不到库
解决方法 假设libavutil默认安装时libavutil.pc安装到了 /usr/lib64/lib/pkgconfig/ 中,而 echo $PKG_CONFIG_PATH 结果中没有这个路径, ...
- 交互神器-最好用的Mac原型设计工具
市场上有着大量的开发和设计工具支持在Mac上安装使用,今天给大家强烈推荐一款Mac上的原型设计工具-Mockplus,原型工具在产品开发设计中是必不可少的,无论是现在非常火的小程序设计,还是网页设计, ...
- 什么是tcp协议?
这是世界上最顶尖的tcp讲解技术...
- springmvc 整合数据验证框架 jsr
1.maven <dependency> <groupId>javax.validation</groupId> <artifactId>validat ...
- Hadoop 1: NCDC 数据准备
本文介绍Hadoop- The Definitive Guide一书中的NCDC数据准备,为后面的学习构建大数据环境; 环境 3节点 Hadoop 2.7.3 集群; java version &qu ...
- 编译 link
--generating dsym file change the appropriate one from 'DWARF with dSYM file' to just 'DWARF',This s ...
- 【JDBC】jdbc原理总结
1 什么是JDBC JDBC(Java DataBase Connectivity)就是Java数据库连接,说白了就是用Java语言来操作数据库.原来我们操作数据库是在控制台使用SQL语句来操作数据库 ...
- sys.argv和getopt.getopt()的用法
1.sys.argv Python中sys.argv是命令行参数从程序外部传值的的一种途径,它是一个列表,列表元素是我们想传进去的的新参数,所以可以用索引sys.argv[]来获得想要的值.因为一个写 ...