【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. day32 HTML

    day32 HTML 什么是前端 只要是跟用户打交道的界面都可以称之为前端 # eg:电脑界面, 手机界面,平板界面, 什么是后端? eg:python, java,php,go, 不跟用户直接打交道 ...

  2. ES5中改变this指向的三种方法

    ES5中提供了三种改变函数中this指针指向的方法,分别如下 1.call() var obj = {username:"孙悟空"}; //没有任何修饰的调用函数,函数中的this ...

  3. 内存管理——array new,array delete

    1.array new array new就是申请一个数组空间,所以在delete的时候一定不能忘记在delete前加[] delete加上[]符号以后,就相当于告诉系统"我这里是数组对象, ...

  4. Output of C++ Program | Set 14

    Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...

  5. 神器Tampermonkey的安装使用

    Tampermonkey是一款基于浏览器的神奇插件,在国内称为油猴,开发者可以在上面开发满足自己需求的各类浏览器应用脚本.不过经过全球各地无数开发者数年的积累现在其官网已经有一大把的优秀的现成脚本,完 ...

  6. matplotlib画直线图的基本用法

    一  figure使用 1 import numpy as np 2 import matplotlib.pyplot as plt 3 4 # 从-3到中取50个数 5 x = np.linspac ...

  7. pipeline配置java项目

    pipeline { agent { label 'slave' } options { timestamps() disableConcurrentBuilds() buildDiscarder( ...

  8. Jenkins配置代码化

    目录 一.简介 二.init.groovy 脚本命令行调试 一.简介 Jenkins用久了,会有一种莫名的紧张感.因为没人清楚Jenkins都配置了什么,以至于没人敢动它. 但凡使用界面进行配置的都会 ...

  9. 按照eslint的规则格式化代码

    1.下载eslint. 2.首选项->设置,然后搜索eslint,点击在setting.json中设置.设置内容如下: "editor.codeActionsOnSave": ...

  10. CPU中断数查看与网卡中断绑核

    CPU中断数查看 多核CPU每个核心CPU发生中断的数量查看 # mpstat -I SUM -P ALL 1 3 Linux 5.4.0-40-generic (verify-new-511kern ...