【LeetCode】813. Largest Sum of Averages 解题报告(Python)
【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 <= A.length <= 100.
- 1 <= A[i] <= 10000.
- 1 <= K <= A.length.
- 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)的更多相关文章
- leetcode 813. Largest Sum of Averages
对于一个数组中的数进行分组,取每个组里的平均值进行加和的. 使用动态规划,其中dp[i][k]表示以i为结尾的有k个分组的,那么递推式为: dp[i][k]=dp[j][k-1]+(sum[i]-su ...
- 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 ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
- 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)
[LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
随机推荐
- android listview展示图片
最近学习android开发,感触颇多,和网站开发对比,还是有很大的差距,在这里记录一下. android listview展示图片 在网站开发上,展示图片非常简单,一个HTML img标签就搞定,加上 ...
- Google服务器架构图解简析
无疑是互联网时代最闪亮的明星.截止到今天为止,Google美国主站在Alexa排名已经连续3年第一,Alexa Top100中,各国的Google分站竟然霸占了超过20多个名额,不得不令人感叹Goog ...
- C#多个标题头合并
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.RowType) ...
- day14搭建博客系统项目
day14搭建博客系统项目 1.下载代码包 [root@web02 opt]# git clone https://gitee.com/lylinux/DjangoBlog.git 2.使用pid安装 ...
- 网卡命令ifconfig
• ifconfig • service network restart • ifdown eth0 • ifdown eth0 #linux下run networkexport USER=lizhe ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(终)-配合内存管理来遍历SD卡
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- [学习总结]7、Android AsyncTask完全解析,带你从源码的角度彻底理解
我们都知道,Android UI是线程不安全的,如果想要在子线程里进行UI操作,就需要借助Android的异步消息处理机制.之前我也写过了一篇文章从源码层面分析了Android的异步消息处理机制,感兴 ...
- java foreach循环抛出异常java.util.ConcurrentModificationException
代码如下: for (Iterator<String> iter = list.iterator(); iter.hasNext(); ) { if (Integer.parseInt(i ...
- 【Linux】【Basis】文件
refer to: https://en.wikipedia.org/wiki/POSIX refer to: https://en.wikipedia.org/wiki/Unix_file_type ...
- HTTP协议及常见状态码
超文本传输协议(HTTP)是用于传输诸如HTML的超媒体文档的应用层协议.它被设计用于Web浏览器和Web服务器之间的通信,但它也可以用于其他目的. HTTP遵循经典的客户端-服务端模型,客户端打开一 ...