题目如下:

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

代码如下:

class Solution(object):
def largestSumOfAverages(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: float
"""
tl = [0] * len(A)
dp = []
for i in range(K):
dp.append(tl[:]) count = 0
for i in range(len(dp)):
for j in range(len(dp[i])-K+i+1):
count += A[j]
if i == 0:
dp[i][j] = float(count)/float(j+1)
continue
for k in range(j):
#print k,j,sum(A[k:j])
dp[i][j] = max(dp[i][j],dp[i-1][k]+float(sum(A[k+1:j+1]))/float(j-k)) return dp[-1][-1]

【leetcode】813. Largest Sum of Averages的更多相关文章

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

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

  2. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  3. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

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

  5. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

  6. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  7. 【LeetCode】#1 Two Sum

    [Question] Given an array of integers, return indices of the two numbers such that they add up to a ...

  8. leetcode 813. Largest Sum of Averages

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

  9. 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)

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

随机推荐

  1. The MEAN stack is a modern replacement for the LAMP (Linux, Apache, MySQL, PHP/Python) stack

    w https://www.mongodb.com/blog/post/building-your-first-application-mongodb-creating-rest-api-using- ...

  2. python生成requirements.txt 导出项目依赖

    使用pip freeze $ pip freeze > requirements.txt 这种方式是把整个环境中的包都列出来了,如果是虚拟环境可以使用. 通常情况下我们只需要导出当前项目的req ...

  3. EntityFrameworkCore.MySql

    1.点击“工具”->“NuGet包管理器”->“程序包管理器控制台” 分别安装以下几个包 Mysql 版本: Install-Package MySql.Data.EntityFramew ...

  4. ssm项目的创建思路

    ·DAO层: 代码:pojo.映射文件.接口文件——Mybatis逆向工程自动生成 配置:ApplicationContext-dao.xml——数据源.连接池.会话工厂.mapper包扫描 ·Ser ...

  5. 剑指offer--day11

    1.1 题目:字符串的排列:输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba ...

  6. 浅谈vue单页面seo问题

    最近做项目的时候,被要求做seo,由于项目已经开发完毕,且只需首页做seo,所以考虑再三,决定用prerender-spa-plugin结合vue-meta-info来实现首页的seo.如果你的页面是 ...

  7. SVN检出新项目

    1.新建文件夹SourseCode -->打开SourseCode文件夹,右键空白处 ---> 选择SVN Checkout --选择URL of repository,选择Checkou ...

  8. 计算距离的SQL语句

    一,BEGINset @num=6378.138*2*ASIN(SQRT(POW(SIN((lat1*PI()/180-lat2*PI()/180)/2),2)+COS(lat1*PI()/180)* ...

  9. mysql修改库、表、字段 字符集,中文排序

    查看字段编码: show full columns from t2;show variables like '%character%';show variables like 'collation_% ...

  10. Spring Boot 中的 Tomcat 是如何启动的?

    作者:木木匠 https://my.oschina.net/luozhou/blog/3088908 我们知道 Spring Boot 给我们带来了一个全新的开发体验,让我们可以直接把 Web 程序打 ...