class Solution { public: double largestSumOfAverages(vector<int>& A, int K) { int n = A.size(); // dp[k][i]: largestSumOfAverages of [0,i] with up to k partitions // dp[0][i]: largestSumOfAverages of [0,i] with 0 partition = (sums[i+1]-sums[0])/(i+1) // dp[k][i]: max(dp[k-1][j] + (sums[i+1]-sums[j+1]) / (i-j), dp[k-1][i]) vector<vector<double>> dp(K, vector<double>(n, 0)); vector<double> sums(n+1, 0); for (int i = 0; i < A.size(); i++) sums[i+1] = sums[i] + A[i]; for (int i = 0; i < n; i++) dp[0][i] = (sums[i+1] - sums[0]) / (i+1); // single partition for (int k = 1; k < K; k++) { for (int i = 0; i < n; i++) { dp[k][i] = dp[k-1][i]; for (int j = 0; j < i; j++) { dp[k][i] = max(dp[k][i], dp[k-1][j] + (sums[i+1]-sums[j+1]) / (i-j)); } } } return dp[K-1][n-1]; } };
转载于:https://www.cnblogs.com/JTechRoad/p/8978410.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/101688.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...