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的更多相关文章

  1. 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 ...

  2. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  3. [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 ...

  4. [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 ...

  5. leetcode 813. Largest Sum of Averages

    对于一个数组中的数进行分组,取每个组里的平均值进行加和的. 使用动态规划,其中dp[i][k]表示以i为结尾的有k个分组的,那么递推式为: dp[i][k]=dp[j][k-1]+(sum[i]-su ...

  6. 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 ...

  7. 【leetcode】813. Largest Sum of Averages

    题目如下: 解题思路:求最值的题目优先考虑是否可以用动态规划.记dp[i][j]表示在数组A的第j个元素后面加上第i+1 (i从0开始计数)个分隔符后可以得到的最大平均值,那么可以得到递归关系式: d ...

  8. leetcode813 Largest Sum of Averages

    """ We partition a row of numbers A into at most K adjacent (non-empty) groups, then ...

  9. 动态规划——Split Array Largest Sum

    题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...

随机推荐

  1. 使用feof()判断文件结束时会多输出内容的原因

    这是原来的代码: #include <stdio.h>int main(){    FILE * fp;    int ch;    fp = fopen("d:\\aaaaa\ ...

  2. sql查询一列 重复的数据

    select * from 表 where num  in(select num  from 表 group by num having count(num)>1)

  3. 认识GMT和UTC时间-附带地理知识

    GMT-格林尼治标准时 GMT 的全名是格林威治标准时间或格林威治平时 (Greenwich Mean Time),这个时间系统的概念在 1884 年确立,由英国伦敦的格林威治皇家天文台计算并维护,并 ...

  4. 复习总结《一》MFC消息映射

    长时间人容易遗忘,从新捡起!特做下记录 MFC消息映射 1.在MFC中消息映射主要牵扯到三个宏分别为: DECLARE_MESSAGE_MAP() BEGIN_MESSAGE_MAP(theClass ...

  5. 【转】Redis之发布 订阅模式

    本例包括 jedis_demo:入口类 jedis_control:jedis控制器(jedis的连接池) jedis_pub_sub_listener:订阅的监听器 singleton_agent: ...

  6. python 简单的爬虫

    import urllib.request import re import ssl # 处理https请求 import time import os # 创建目录用 def get_html(ur ...

  7. maven intall在target文件夹中自动生成的war包部署服务器时缺斤少两

    1.问题描述,本地改动特别大或者升级系统操作,打war包部署服务器上程序时候,页面或者后台总是报错,原因就是比本地少东西. 2.问题排查解决:maven clean然后maven intall在tar ...

  8. 发布QT exe

    https://blog.csdn.net/u014453443/article/details/85837138

  9. Linux基础命令---cmp

    cmp 用字节的方式,比较两个文件是否存在差异,但是不保存运算结果.Cmp指令只会根据结果设置相关的标志位,这个指令之后往往会跟着一个条件跳转指令. 此命令的适用范围:RedHat.RHEL.Ubun ...

  10. mybatis项目启动报错 The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".

    启动项目报错 2018-02-26 17:09:51,535 ERROR [org.springframework.web.context.ContextLoader] - Context initi ...