对于一个数组中的数进行分组,取每个组里的平均值进行加和的。

使用动态规划,其中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的更多相关文章

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

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

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

  6. [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 ...

  7. leetcode813 Largest Sum of Averages

    """ We partition a row of numbers A into at most K adjacent (non-empty) groups, then ...

  8. 动态规划-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 + . ...

  9. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. 数据仓库之Data Vault模型总结

    一,Data Vault模型有几个主要的组件,这里先总结一下: 1.Hub组件,是一个数据表,用于记录在业务应用中常用到的业务实体键值,如员工ID,发票号.客户编号.车辆号等. 表内包括几个关键字段: ...

  2. git命令简介

    git作为版本控制器,多分支功能能够很好的协同开发.其中分支中分为主分支和辅助分支 主分支包括:master分支和develop分支,不多做解释 辅助分支包括一下三种分支,其中 需求分支(Featur ...

  3. react项目搭建及webpack配置

    1,配置webpack npm install -g webpack                       webpack的cli环境 npm install -g webpack-dev-se ...

  4. flask基础--第二篇

    1.Flask中的HTTPResponse,Redirect, render #导入render_template和redirect from flask import Flask,render_te ...

  5. unity中使用www的库读取数据里面的数据

    //使用www的库,读取数据里面的数据 string factoryControllerUrl = "http://IP地址:8086/DatabaseServerCode/GangShan ...

  6. JS 跳出多重循环

    今天学到了如何跳出多重循环

  7. bzoj1668

    题解: 简单dp 注意最后往后面多取几个 求个最小值 代码: #include<bits/stdc++.h> using namespace std; ; int n,m,f[N],a[N ...

  8. python学习------文件处理

    文件操作 一 介绍 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所周 ...

  9. 不使用接口的 limit 控制分页的容量

    1.html中v-for 此时的v-for对象并不是在后台获取的数组list,而是计算属性的函数名pageList <div v-for="item in pageList" ...

  10. java 初学 英语单词 记录在此 希望全部记住

    Java英文单词Java基础常见英语词汇(共70个)                                                                          ...