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 ...
随机推荐
- Docker私有仓库实例
C:\Users\think\.m2\settings.xml文件配置: <?xml version="1.0" encoding="UTF-8"?> ...
- 在CentOS 7 上设置返回上一级目录的快捷键为 Backspace
参考这里. 编辑文件: $ vi ~/.config/nautilus/accels 找到这一行: ; (gtk_accel_path "<Actions>/ShellActi ...
- linux php7.2安装扩展memcached
wget http://pecl.php.net/get/igbinary-2.0.8.tgz tar -xzvf igbinary-2.0.8.tgz cd igbinary-2.0.8 /usr/ ...
- 微信公众号手机无法直接下载APK文件是怎么回事
现在微信分享的功能很多,从分享的链接下载apk安卓包是很正常的,但是微信不让下载apk包,只能通过浏览器来下载,但是这要给用户一个提示吧,不然用户不知道 下面我们来实现,引导用户通过浏览器来下载apk ...
- C++学习笔记:多态篇之虚析构函数
动态多态中存在的问题:可能会产生内存泄漏! 以下通过一个例子向大家说明问什么会产生内存泄漏: class Shape//形状类 { public: Shape(); virtual double ca ...
- day44-Celery异步分布式
celery异步分布式Celery是一个python开发的异步分布式任务调度模块.Celery本身并不提供消息服务,使用第三方服务,也就是borker来传递任务,目前支持rebbimq,redis, ...
- python format(格式化)
自 python 2.6 开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱.#语法它通过{ ...
- 1.1 Java并发编程的一些概念
并发编程的一些概念 同步和异步 同步: 同步方法必须等到方法调用返回后,才能继续后继的行为.也就是说,同步方法执行时,如果没有返回,则后面的方法是执行不到的.同步方法调用,调用过程中可能出现阻塞和等待 ...
- javeEE第五周
一.定义 AndXML”(异步Javascript和XML),是指一种创建交互式网页应用的网页开发技术.AJAX = 异步JavaScript和XML(通用标记语言的子集),是一种用于创建快速动态网页 ...
- 小程序获取openid 出现null,{"errcode":40163,"errmsg":"code been used, hints: [ req_id: WNUzlA0105th41 ]"}
//根据微信提供的接口,请求得到openid和session_id public class UserInfoUtils { private String getKeyURL="https: ...