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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/largest-sum-of-averages/description/

题目描述:

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.

Note:

  1. 1 <= A.length <= 100.
  2. 1 <= A[i] <= 10000.
  3. 1 <= K <= A.length.
  4. Answers within 10^-6 of the correct answer will be accepted as correct.

题目大意

把一个数组分成K个区间,使得各个平均数的和最大化。

解题方法

典型的dp求解的题目。不过dfs方式去做速度还挺快。看到A的大小是100,那么时间复杂度应该在O(n^3)。

我们定义一个函数LSA,这个函数的含义是,把A这个数组的前n个元素分成k个组得到的最大平均数和。使用m_二维数组完成记忆化,使得搜索速度变快。用sum_保存前i项数组的和,使得可以快速求平均数。

所以递归的方式:

把A这个数组的前i个元素分成k个组得到的最大平均数和 = max(把A这个数组的前j个元素分成k-1个组得到的最大平均数和 + 数组A从j+1到i个元素的平均值)。

如果读不懂上面这句话,可以直观的理解成k个组的最大平均数和 是怎么组成的?它是由前k-1个组与后面一个组放在一起组成,所以求怎么划分的情况下,前部分和后部分的求平均数的和最大。

代码如下:

class Solution:
def largestSumOfAverages(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: float
"""
n = len(A)
m_ = [[0 for i in range(n + 1)] for j in range(K + 1)]
sum_ = [0] * (n + 1)
for i in range(1, n + 1):
sum_[i] = sum_[i - 1] + A[i - 1]
return self.LSA(A, sum_, m_, n, K) # Largest sum of averages for first n elements in A partioned into K groups
def LSA(self, A, sum_, m_, n, k):
if m_[k][n] > 0: return m_[k][n]
if k == 1: return sum_[n] / n
for i in range(k - 1, n):
m_[k][n] = max(m_[k][n], self.LSA(A, sum_, m_, i, k - 1) + (sum_[n] - sum_[i]) / (n - i))
return m_[k][n]

还可以用dp求解,待续。

参考资料:

https://www.youtube.com/watch?v=IPdShoUE9z8

日期

2018 年 9 月 14 日 ———— 脚踏实地,不要迷茫了

【LeetCode】813. Largest Sum of Averages 解题报告(Python)的更多相关文章

  1. leetcode 813. Largest Sum of Averages

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

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

  3. 【leetcode】813. Largest Sum of Averages

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

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

  5. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  6. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  7. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  8. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

  9. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

随机推荐

  1. Python爬虫3大解析库使用导航

    1. Xpath解析库 2. BeautifulSoup解析库 3. PyQuery解析库

  2. 【翻译】.NET 6 中的 dotnet monitor

    原文:Announcing dotnet monitor in .NET 6 我们在 2020 年 6 月首次推出了dotnet monitor 作为实验工具,并在去年(2020年)努力将其转变为生产 ...

  3. Azure Key Vault(二)- 入门简介

    一,引言 在介绍 Azure Key Vault 之前,先简单介绍一下 HSM(硬件安全模块). -------------------- 我是分割线 -------------------- 1,什 ...

  4. 一文搞懂指标采集利器 Telegraf

    作者| 姜闻名 来源|尔达 Erda 公众号 ​ 导读:为了让大家更好的了解 MSP 中 APM 系统的设计实现,我们决定编写一个<详聊微服务观测>系列文章,深入 APM 系统的产品.架构 ...

  5. 一起手写吧!sleep函数!

    Async/Await 版本 function sleep(delay) { return new Promise(reslove => { setTimeout(reslove, delay) ...

  6. 【leetcode】451. Sort Characters By Frequency

    Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...

  7. webservice--cxf和spring结合

    服务端: 实体: package entity; import java.util.Date; /*** 实体 */ public class Pojo { //温度 private String d ...

  8. centos 7 重新获取IP地址

    1.安装软件包 dhclient # yum install dhclient 2.释放现有IP # dhclient -r 3.重新获取 # dhclient 4.查看获取到到IP # ip a

  9. Dos窗口下中文乱码问题

    最近用Datax工具进行数据同步时,在DOS窗口下出现了中文乱码问题,导致一些错误只能到Log中查看,在网上找了一些方法,记录使用成功的方法. Dos命令:chcp 通过cmd进入Dos命令窗口,执行 ...

  10. Linux基础命令---httpd守护进程

    httpd httpd是apache超文本传输协议的主程序,它被设计成一个独立运行的守护进程.httpd会建立一个线程池来处理http请求. 此命令的适用范围:RedHat.RHEL.Ubuntu.C ...