leetcode 813. Largest Sum of Averages
对于一个数组中的数进行分组,取每个组里的平均值进行加和的。
使用动态规划,其中dp[i][k]表示以i为结尾的有k个分组的,那么递推式为: dp[i][k]=dp[j][k-1]+(sum[i]-sum[j])/(i-j)的,那么当k=1的时候就初始化为组内的平均值的,其中j的初始化为k-2,因为是从0为起始点的。
class Solution {
public:
double largestSumOfAverages(vector<int>& A, int K) {
int n=A.size();
vector<double> sum(n,);
sum[]=A[];
for(int i=;i<n;i++){
sum[i]=sum[i-]+A[i];
}
vector<vector<double>> dp(n,vector<double>(K+,));
for(int k=;k<=K;k++){
for(int i=;i<n;i++){
if(k==){
dp[i][k]=sum[i]/(i+);
}
else if(k-<i){
for(int j=k-;j<i;j++){
dp[i][k]=max(dp[i][k],dp[j][k-]+(sum[i]-sum[j])/(i-j) );
}
}
}
}
/*
for(int i=0;i<n;i++){
for(int j=1;j<=K;j++){
cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
}
}
*/
return dp[n-][K];
}
};
leetcode 813. Largest Sum of Averages的更多相关文章
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 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 ...
- [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 + . ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig的解决
现象: tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig() ...
- js for in循环遍历对象,获取key:value值
var testObj = { 'a':'111', 'b':'222', 'c':'333', 'd':'444'}for(var i in testObj){ console.log(i); // ...
- Java 面试题集锦
都是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我一样参加各大IT校园招聘的同学们,纯考Java基础功底,老手们就不用进来了,免得笑话我们这些未出校门的孩纸们,但 ...
- 1ci
- Java final类&所有构造方法均为private的类(类型说明符&访问控制符)
1. final是类型说明符,表示关闭继承,即final类不能有子类: 但final类可能可以在类外创建对象(即final类的构造方法可以不是private型): 在同一包中时,可以在任何另外一个类中 ...
- saltstack高效运维
saltstack高效运维 salt介绍 saltstack是由thomas Hatch于2011年创建的一个开源项目,设计初衷是为了实现一个快速的远程执行系统. salt强大吗 系统管理员日常会 ...
- MySQL MERGE存储引擎
写这篇文章,主要是因为面试的时候,面试官问我怎样统计所有的分表(假设按天分表)数据,我说了两种方案,第一种是最笨的方法,就是循环查询所有表数据(肯定不能采用):第二种方法是,利用中间件,每天定时把前一 ...
- MySQL 必知必会学习笔记(常用命令二)
CREATE TABLE students(student_id INT UNSIGNED, name VARCHAR(30), sex CHAR(1), birth DATE, PRIMARY KE ...
- Write CSV file for a dataset
import numpy as np import cv2 as cv2 import os import csv dataste_path = 'datasets/pascal-parts/pasc ...
- 在jsp里面 当鼠标元素触发onmouseover时,旁边出现一个浮动且跟随鼠标的div ,移开消失
JSP页面 : <label onmouseover="showLongStrlog(window.event, '<list:seqnum></list:seqnu ...