Problem
题目大意:把一个数组分成k个段,求每段平均值和的最大值。
https://leetcode.com/problems/largest-sum-of-averages/description/
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^-6
of the correct answer will be accepted as correct.
Idea
DP, use dp[k][i] to denote the largest average sum of partitioning first i elements into k groups.
Init
dp[1][i] = sum(a[0] ~ a[i – 1]) / i, for i in 1, 2, … , n.
Transition
dp[k][i] = max(dp[k – 1][j] + sum(a[j] ~ a[i – 1]) / (i – j)) for j in k – 1,…,i-1.
that is find the best j such that maximize dp[k][i]
largest sum of partitioning first j elements (a[0] ~ a[j – 1]) into k – 1 groups (already computed)
+ average of a[j] ~ a[i – 1] (partition a[j] ~ a[i – 1] into 1 group).
Answer
dp[K][n]
Solution 1: DP
Time complexity: O(kn^2)
Space complexity: O(kn)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Author: Huahua // Running time: 9 ms class Solution { public: double largestSumOfAverages(vector<int>& A, int K) { const int n = A.size(); vector<vector<double>> dp(K + 1, vector<double>(n + 1, 0.0)); vector<double> sums(n + 1, 0.0); for (int i = 1; i <= n; ++i) { sums[i] = sums[i - 1] + A[i - 1]; dp[1][i] = static_cast<double>(sums[i]) / i; } for (int k = 2; k <= K; ++k) for (int i = k; i <= n; ++i) for (int j = k - 1; j < i; ++j) dp[k][i] = max(dp[k][i], dp[k - 1][j] + (sums[i] - sums[j]) / (i - j)); return dp[K][n]; } }; |
C++ O(n) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Author: Huahua // Running time: 8 ms class Solution { public: double largestSumOfAverages(vector<int>& A, int K) { const int n = A.size(); vector<double> dp(n + 1, 0.0); vector<double> sums(n + 1, 0.0); for (int i = 1; i <= n; ++i) { sums[i] = sums[i - 1] + A[i - 1]; dp[i] = static_cast<double>(sums[i]) / i; } for (int k = 2; k <= K; ++k) { vector<double> tmp(n + 1, 0.0); for (int i = k; i <= n; ++i) for (int j = k - 1; j < i; ++j) tmp[i] = max(tmp[i], dp[j] + (sums[i] - sums[j]) / (i - j)); dp.swap(tmp); } return dp[n]; } }; |
Solution 2: DFS + memoriation
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Author: Huahua // Running time: 10 ms class Solution { public: double largestSumOfAverages(vector<int>& A, int K) { const int n = A.size(); m_ = vector<vector<double>>(K + 1, vector<double>(n + 1, 0.0)); sums_ = vector<double>(n + 1, 0.0); for (int i = 1; i <= n; ++i) sums_[i] = sums_[i - 1] + A[i - 1]; return LSA(A, n, K); } private: vector<vector<double>> m_; vector<double> sums_; // Largest sum of averages for first n elements in A paritioned into k groups double LSA(const vector<int>& A, int n, int k) { if (m_[k][n] > 0) return m_[k][n]; if (k == 1) return sums_[n] / n; for (int i = k - 1; i < n; ++i) m_[k][n] = max(m_[k][n], LSA(A, i, k - 1) + (sums_[n] - sums_[i]) / (n - i)); return m_[k][n]; } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment