-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumCostTreeFromLeafValues.cpp
More file actions
31 lines (30 loc) · 1.04 KB
/
minimumCostTreeFromLeafValues.cpp
File metadata and controls
31 lines (30 loc) · 1.04 KB
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
26
27
28
29
30
31
// Source: https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/
// Author: Miao Zhang
// Date: 2021-04-12
/*************************************************
* dp[i][j] = min{dp[i][k] + dp[k + 1][j] + max(a[i~k]) * max(a[k + 1~j])}
*************************************************/
class Solution {
public:
int mctFromLeafValues(vector<int>& arr) {
int n = arr.size();
vector<vector<int>> dp(n, vector<int>(n));
vector<vector<int>> m(n, vector<int>(n));
for (int i = 0; i < n; i++) {
m[i][i] = arr[i];
for (int j = i + 1; j < n; j++) {
m[i][j] = max(m[i][j - 1], arr[j]);
}
}
for (int l = 2; l <= n; l++) {
for (int i = 0; i <= n - l; i++) {
int j = i + l - 1;
dp[i][j] = INT_MAX;
for (int k = i; k < j; k++) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + m[i][k] * m[k+1][j]);
}
}
}
return dp[0][n - 1];
}
};