动态规划-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 + ... + ai) / (i - k)} k = [j - 2, i - 1]
public double largestSumOfAverages(int[] A, int K) {
double[][] dp = new double[A.length][K + 1];
int curSum = 0;
for (int i = 0; i < A.length; i++) {
curSum += A[i];
dp[i][1] = curSum * 1.0 / (i + 1);
}
for (int k = 2; k <= K; k++) {
for (int i = k - 1; i < A.length; i++) {
for (int j = k - 2; j < i; j++) {
curSum = 0;
int idx = j + 1;
while (idx <= i) curSum += A[idx++];
dp[i][k] = Math.max(dp[i][k], dp[j][k - 1] + curSum * 1.0 / (i - j));
}
}
}
return dp[A.length - 1][K];
}
动态规划-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 ...
- leetcode 813. Largest Sum of Averages
对于一个数组中的数进行分组,取每个组里的平均值进行加和的. 使用动态规划,其中dp[i][k]表示以i为结尾的有k个分组的,那么递推式为: dp[i][k]=dp[j][k-1]+(sum[i]-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][j]表示在数组A的第j个元素后面加上第i+1 (i从0开始计数)个分隔符后可以得到的最大平均值,那么可以得到递归关系式: d ...
- leetcode813 Largest Sum of Averages
""" We partition a row of numbers A into at most K adjacent (non-empty) groups, then ...
- 动态规划——Split Array Largest Sum
题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...
随机推荐
- yii2 restful api——app接口编程实例
<?php namespace common\components; use common\models\Cart; use common\models\User; use Yii; use y ...
- iPhone手机获取uuid 安装测试app
iPhone手机获取uuid 安装测试app UDID是一种iOS设备的特殊识别码.除序号之外,每台ios装置都另有一组独一无二的号码,我们就称之为识别码( Unique Device Identif ...
- 360在线网站安全检测,web安全测试AppScan扫描工具,XSS常用的攻击手法
360在线网站安全检测,web安全测试AppScan扫描工具,XSS常用的攻击手法 如何做好网站的安全性测试 360网站安全检测 - 在线安全检测,网站漏洞修复,网站后门检测http://websca ...
- Linux中Postfix邮件安装Maildrop(八)
Postfix使用maildrop投递邮件 Maildrop是本地邮件投递代理(MDA), 支持过滤(/etc/maildroprc).投递和磁盘限额(Quota)功能. Maildrop是一个使用C ...
- CentOS 7 安装OpenCV
CentOS 7 安装OpenCV步骤如下: 1.在CentOS 7命令行中直接在线安装: yum install numpy opencv* 2.安装完成后进行全盘搜索:find / -n ...
- cnats 使用
1. 准备 yum install cmakeyum install gcc gcc-c++yum install ncurses ncurses-develyum install openssl o ...
- 使用wireshark分析tcp/ip报文之报文头
以太网报文的结构如下: 其中,以太网的帧头: 14 Bytes:MAC目的地址48bit(6B),MAC源地址48bit(6B),Type域2B,一共14B. IP头部: TCP头部: http:// ...
- String和int互相转换,String转float
String-->int int a=Integer.parseIn(str); int-->String String s= a+""; String-->fl ...
- Delphi XE5 for Android (四)
在Delphi中窗体与窗体之间的交互与调用非常简单,在FMX中这个优势得到了充分体现,先建立一个主窗体和一个需要调用的窗体: 在主窗体上放的按钮事件如下: - private { Pr ...
- mbr看图