leetcode 813. Largest Sum of Averages
对于一个数组中的数进行分组,取每个组里的平均值进行加和的。
使用动态规划,其中dp[i][k]表示以i为结尾的有k个分组的,那么递推式为: dp[i][k]=dp[j][k-1]+(sum[i]-sum[j])/(i-j)的,那么当k=1的时候就初始化为组内的平均值的,其中j的初始化为k-2,因为是从0为起始点的。
class Solution {
public:
double largestSumOfAverages(vector<int>& A, int K) {
int n=A.size();
vector<double> sum(n,);
sum[]=A[];
for(int i=;i<n;i++){
sum[i]=sum[i-]+A[i];
}
vector<vector<double>> dp(n,vector<double>(K+,));
for(int k=;k<=K;k++){
for(int i=;i<n;i++){
if(k==){
dp[i][k]=sum[i]/(i+);
}
else if(k-<i){
for(int j=k-;j<i;j++){
dp[i][k]=max(dp[i][k],dp[j][k-]+(sum[i]-sum[j])/(i-j) );
}
}
}
}
/*
for(int i=0;i<n;i++){
for(int j=1;j<=K;j++){
cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
}
}
*/
return dp[n-][K];
}
};
leetcode 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][j]表示在数组A的第j个元素后面加上第i+1 (i从0开始计数)个分隔符后可以得到的最大平均值,那么可以得到递归关系式: d ...
- 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 ...
- [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 ...
随机推荐
- Code Reading: ORB-SLAM回环检测源码阅读+注释
之前研究过一些回环检测的内容,首先要看的自然是用词袋回环的鼻祖和正当继承人(没有冒犯VINS和LDSO的意思)ORB-SLAM.下面是我的代码注释.因为代码都是自己手打的,不是在源码上注释的,所以一些 ...
- java接口特性
java接口特性 (1)接口可以被实例化 ,常作为类型使用 (2)实现类必须实现接口的所有方法 ,抽象类除外 (3)实现类可以实现多个几口 java中的多继承 (4)接口中的变量都是静态变量
- vi编辑器和系统分区
作业一:1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cat /etc/passwd /etc/group > /1.txt2) 将用户信息数据库文件和用户 ...
- 关于Ubuntu使用笔记
Ubuntu vm tools 安装 sudo apt install open-vm-tools-desktop 在安装程序时Ubuntu会将安装目录锁定,安装结束后会解除锁定,中断安装后无法再安装 ...
- Linux关机总结
立刻关机 root@ubuntu17:~# shutdown -h now 100分钟后关机 root@ubuntu17:~# shutdown -h + Shutdown scheduled -- ...
- 【webpack学习笔记】a04-建立开发环境
开发环境就是在开发过程中为了方便配置的环境,生产环境就是开发完成即将上线的情况. 好了,说了句废话,切入正题. 在开发时,打包后的文件压缩成一团,报错调试的时候傻眼了有木有?每次做出修改需要到浏览器查 ...
- ubuntu 16.04 安装 opencv +contrib (3.2.0) + python 3.5
环境: - ubuntu 16.04 - OpenCV + contrib 3.2.0 (文中附下载链接) - Python 3.5 基于其他环境的配置应该大同小异. 没时间解释了,直接上车. 更新下 ...
- windows环境下,spring boot服务使用docker打包成镜像并推送到云服务器私有仓库
最近在淘宝上学习springcloud教程,其中有几节课是讲解讲本地springboot服务打包成镜像并推送到云服务器私有仓库,但是教程里面用的事Mac环境,我的是Windows环境,而且课程里面没有 ...
- jquery添加节点时能有点击事件
<script> var n=0; $(".dj").on('click',function(){ ...
- 后台管理系统之“图片上传” --vue
图片上传(基于vue) 相信上传图片是所有系统必备的功能吧,工作中的第一个管理系统就在上传图片的功能上卡顿了一整天. 当时用的elementUI组件,但是由于样式和设计图样式差别较大再加上原生相较好理 ...