动态规划-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 ...
随机推荐
- LeetCode-MinimumDepthOfBinaryTree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- ide vscode安装
在linux系统中安装VSCode(Visual Studio Code) 在linux系统中安装VSCode(Visual Studio Code) 1.从官网下载压缩包(话说下载下来解压就直接 ...
- EditPlus 5.0 中文版已经发布(3月26日更新)
注意:新版本不再支持旧的注册码! 新特性: - Ctrl+Alt+Up/Down 键可添加多个插入点以及进行列选择 - Alt+鼠标点击可添加多个插入点 - 连续执行“选择单词”命令可将多个选中项添加 ...
- mysql日志文件目录
默认情况下mysql的二进制日志文件保存在默认的数据目录data下,如:/usr/local/mysql/data 修改日志保存目录(/backup/mysqlbinlog/mysql-bin)的话: ...
- 优化 MySQL: 3 个简单的小调整
我并不期望成为一个专家级的 DBA,但是,在我优化 MySQL 时,我推崇 80/20 原则,明确说就是通过简单的调整一些配置,你可以压榨出高达 80% 的性能提升.尤其是在服务器资源越来越便宜的当下 ...
- Centos下安装git高版本2.1.2
安装依赖软件 # yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc # yum in ...
- P4289 [HAOI2008]移动玩具(bfs)
P4289 [HAOI2008]移动玩具 双向bfs+状态压缩+记忆化搜索 双向bfs用于对bfs的优化,每次找到可扩展节点少的一边进行一次bfs,找到的第一个互相接触的点即为最短路径 矩阵范围仅4* ...
- Git 基础笔记整理1
Git 官网:http://git-scm.com/ git教程1:http://www.yiibai.com/git/home.html git教程2 :http://www.liaoxuefeng ...
- Python3基础 list str转成list
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- fhq treap抄袭笔记
目录 碎碎念 点一下 注意!!! 模板 fhq treap 碎碎念 我咋感觉合并这么像左偏树呢 ps:难道你们的treap都是小头堆的吗 fhq真的是神人 现在看以前学的splay是有点恶心,尤其是压 ...