"""
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
"""
"""
这题目的思想是基于一个事实:一个数组分割之后肯定是份数越多得到的平均值就越大。
我们假设dp[i][k]表示为数组A【0~i】分割k份得到的最大平均值。
因为最后一个分割点可以出现在任何部分(实际上也得出现在大于k-1个位置之后),所以
dp[i][k]=max(dp[i][k], dp[j][k-1]+sum(j~i)/(i-j)) 其中j在j之前的地方j<i,
因此可以把这个递推式理解为重新选择最后一个分割点的最大处。
当然,要迅速完成sum段求和还需要一个小函数。
传送门:https://blog.csdn.net/weixin_37373020/article/details/81543069
"""
class Solution:
def largestSumOfAverages(self, A, K):
sums = [0]
for a in A:
sums.append(sums[-1]+a) #sums[i] = A[0]+A[1]+...A[i-1]
def gap_sum(i, j):
return sums[j]-sums[i]
dp = [[0 for x in range(K+1)] for y in range(len(A)+1)]
for i in range(1, len(A)+1):
dp[i][1] = gap_sum(0, i)/i
for k in range(2, K+1):
for i in range(k, len(A)+1):
for j in range(i):
dp[i][k] = max(dp[i][k], dp[j][k-1]+gap_sum(j, i)/(i-j))
return dp[len(A)][K]

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

  6. leetcode 813. Largest Sum of Averages

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

  7. 【leetcode】813. Largest Sum of Averages

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

  8. 动态规划-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 + . ...

  9. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. ANSYS-APDL施加扭转载荷CERIG命令

    目录 1. 要求 2. ANSYS有限元分析 2.1 APDL建模 2.2 APDL施加载荷 2.3 APDL查看结果 3. 举一反三 1. 要求 一块0.8m*0.4m*0.04m厚的钢板,在板的两 ...

  2. 【原】jenkins知识点_凭据(一)

    一:凭据 1.目的: 与第三方网站或应用程序进行交互,如代码仓库.云存储系统和服务等 2.操作路径: Jenkins-凭据-系统-全局凭据 3.权限 Jenkins 中保存的凭证可以用于: 任何适用于 ...

  3. springboot例子

    @Mapperpublic interface FinancingMapper { @Insert("<script>" + "insert into fin ...

  4. 基于 Chrome 浏览器的扩展插件来进行的安装Postman

    我会给你一个安装包,见附件.你应该下载下来,解压缩到你喜欢的位置. 打开 Chrome 浏览器的「扩展程序」 点击「加载已解压的扩展程序...」按钮,找到你刚刚下载的安装包的位置,点击确定. 你去看看 ...

  5. js学习:函数

    概述 函数的声明 JavaScript 有三种声明函数的方法 function 命令 function命令声明的代码区块,就是一个函数.function命令后面是函数名,函数名后面是一对圆括号,里面是 ...

  6. PAT A1151 LCA in Binary Tree

    利用树的前序和中序递归判定最小公共祖先~ 直接根据两个序列递归处理~ #include<bits/stdc++.h> using namespace std; ; int N,M; int ...

  7. A - Bi-shoe and Phi-shoe 素数打表

    A - Bi-shoe and Phi-shoe Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi- ...

  8. Java 代码中如何调用 第三方Api

    在代码中调用第三方API 获取数据 package com.example.demo.utils; import com.alibaba.fastjson.JSONObject; import lom ...

  9. DuiLib中FlashDemo的例子经验杂粹1

    转载:https://www.jianshu.com/p/3e958ae9e5ab 最近用duilib做个东西,经常卡壳 ,而且以前学的现在又忘.现在觉得应该好好做笔记,以前老是觉得博客是很郑重的东西 ...

  10. windows通过zip安装mysql5.7.26的一个坑

    需要将my.ini的 红框的/不能写成\ 注意编码格式问题 然后 mysqld --initialize-insecure mysqld --install net start mysql