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 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^-6of the correct answer will be accepted as correct.
Runtime: 4 ms, faster than 99.57% of C++ online submissions for Largest Sum of Averages.
double memo[][];
class Solution {
public:
double largestSumOfAverages(vector<int>& A, int K) {
memset(memo, , sizeof(memo));
int N = A.size();
double cur = 0.0;
for(int i=; i<N; i++) {
cur += A[i];
memo[i+][] = cur / (i+);
}
return search(N, K, A);
}
double search(int n, int k, vector<int>& A) {
if(memo[n][k] > ) return memo[n][k];
if(n < k) return ;
double cur = 0.0;
for (int i = n-; i > ; --i) {
cur += A[i];
memo[n][k] = max(memo[n][k], search(i, k-, A) + cur / (n-i));
}
return memo[n][k];
}
};
LC 813. Largest Sum of Averages的更多相关文章
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 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][k]表示以i为结尾的有k个分组的,那么递推式为: dp[i][k]=dp[j][k-1]+(sum[i]-su ...
- 【leetcode】813. Largest Sum of Averages
题目如下: 解题思路:求最值的题目优先考虑是否可以用动态规划.记dp[i][j]表示在数组A的第j个元素后面加上第i+1 (i从0开始计数)个分隔符后可以得到的最大平均值,那么可以得到递归关系式: d ...
- [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 ...
- [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 ...
- leetcode813 Largest Sum of Averages
""" We partition a row of numbers A into at most K adjacent (non-empty) groups, then ...
- 动态规划-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 + . ...
- 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小
[抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
随机推荐
- SpringBoot定时任务(schedule、quartz)
Scheduled 只适合处理简单的计划任务,不能处理分布式计划任务.优势:是spring框架提供的计划任务,开发简单,执行效率比较高.且在计划任务数量太多的时候,可能出现阻塞,崩溃,延迟启动等问题. ...
- jq国际化i18n
1.(main.js或)ajax.js引入文件 import './jquery.i18n.properties.min' //https://github.com/jquery-i18n-prope ...
- 【SpringBoot】Web开发
一.简介 1.1 引入SpringBoot模块 1.2 SpringBoot对静态资源的映射规则 二.模版引擎 2.1 简介 2.2 引入thymeleaf 2.3 Thymeleaf使用 一.简介 ...
- Redis汇总
开源项目 https://www.cnblogs.com/yswenli/p/9460527.html
- 【Day3】5.Python中的lxml模块
import lxml.etree as le with open('edu.html','r',encoding='utf-8') as f: html = f.read() html_x = le ...
- uuid:全局唯一标识符
uuid1:IEEE 802 MAC地址 import uuid ''' UUID1值使用主机的MAC地址计算.uuid模块使用getnode函数来获取当前系统的MAC值 ''' print(uuid ...
- Hadoop_17_MapRduce_案例2_实现用户手机流量统计(ReduceTask并行度控制)
需求:1.统计每一个用户(手机号)所耗费的总上行流量.下行流量,总流量 1.数据如下:保存为.dat文件(因为以\t切分数据,文件格式必须合适) 1363157985066 13726230503 0 ...
- MySQL No Install zip安装方法
解压 解压安装包到一个目录,比如:D:\Database\mysql-5.1.55 配置 创建配置文件 创建一个my.ini文件,MYSQL目录下有几个my开头的ini文件,是针对不同配置使用,具体可 ...
- 前端自动化构建工具 Webpack—— 2 webpack最基本的使用方式
Webpack可以做什么事情? 1.webpack能够处理JS文件的互相依赖关系: 2.webpacck能够处理JS的兼容问题,把高级的.浏览器不识别的语法,转为 低级的,浏览器能正常识别的语法 we ...
- javascript 常用的一些原生方法
一丶javascript------ reduce() reduce()方法: arr.reduce(function(prev,cur,index,arr){ ... }, init); 参数解释: ...