Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/1130.html
1130. Minimum Cost Tree From Leaf Values (Medium)
Given an array arr
of positive integers, consider all binary trees such that:
- Each node has either 0 or 2 children;
- The values of
arr
correspond to the values of each leaf in an in-order traversal of the tree. (Recall that a node is a leaf if and only if it has 0 children.) - The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively.
Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node. It is guaranteed this sum fits into a 32-bit integer.
Example 1:
Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4
Constraints:
2 <= arr.length <= 40
1 <= arr[i] <= 15
- It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than
2^31
).
Related Topics:
Dynamic Programming, Stack, Tree
Solution 1. DP
Let dp[i][j]
be the answer to the subproblem on subarray A[i..j]
.
dp[i][j] = min( dp[i][k] + dp[k+1][j] + mx(i, k) * mx(k+1, j) | i <= k < j )
dp[i][i] = 0
where mx[i][j] = max(A[i], ..., A[j])
.
// OJ: https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/
// Time: O(N^3)
// Space: O(N^2)
class Solution {
public:
int mctFromLeafValues(vector<int>& A) {
int N = A.size();
vector<vector<int>> mx(N, vector<int>(N)), dp(N, vector<int>(N, INT_MAX));
for (int i = 0; i < N; ++i) {
mx[i][i] = A[i];
for (int j = i + 1; j < N; ++j) mx[i][j] = max(mx[i][j - 1], A[j]);
}
for (int i = 0; i < N; ++i) dp[i][i] = 0;
for (int i = N - 2; i >= 0; --i) {
for (int j = i + 1; j < N; ++j) {
for (int k = i; k < j; ++k) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + mx[i][k] * mx[k + 1][j]);
}
}
}
return dp[0][N - 1];
}
};
Solution 2. Greedy
// OJ: https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/
// Time: O(N^2)
// Space: O(1)
// Ref: https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/discuss/339959/One-Pass-O(N)-Time-and-Space
class Solution {
public:
int mctFromLeafValues(vector<int>& A) {
int ans = 0;
while (A.size() > 1) {
auto it = min_element(A.begin(), A.end());
int left = it == A.begin() ? INT_MAX : *(it - 1);
int right = it == A.end() - 1 ? INT_MAX : *(it + 1);
ans += *it * min(left, right);
A.erase(it);
}
return ans;
}
};
Solution 3. Greedy
We should greedily pick the smallest pair to form a subtree. Then we can remove the smaller element of the pair from the array since it won’t be used again.
// OJ: https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/
// Time: O(N^2)
// Space: O(1)
class Solution {
public:
int mctFromLeafValues(vector<int>& A) {
int ans = 0, N = A.size();
for (int N = A.size(); N > 1; --N) {
int best = 1;
for (int i = 2; i < N; ++i) {
if (A[i] * A[i - 1] < A[best] * A[best - 1]) best = i;
}
ans += A[best] * A[best - 1];
if (A[best] > A[best - 1]) --best;
A.erase(A.begin() + best);
}
return ans;
}
};
Solution 4. Greedy + Monotonic Stack
// OJ: https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/
// Time: O(N)
// Space: O(N)
// Ref: https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/discuss/339959/One-Pass-O(N)-Time-and-Space
class Solution {
public:
int mctFromLeafValues(vector<int>& A) {
int ans = 0;
vector<int> s{INT_MAX};
for (int n : A) {
while (s.back() <= n) {
int mid = s.back();
s.pop_back();
ans += mid * min(s.back(), n);
}
s.push_back(n);
}
for (int i = 2; i < s.size(); ++i) ans += s[i] * s[i - 1];
return ans;
}
};
-
class Solution { public int mctFromLeafValues(int[] arr) { int length = arr.length; int[][] maxValues = new int[length][length]; for (int i = 0; i < length; i++) { for (int j = i; j < length; j++) { int maxValue = 0; for (int k = i; k <= j; k++) { if (maxValue < arr[k]) maxValue = arr[k]; } maxValues[i][j] = maxValue; } } int[][] dp = new int[length][length]; for (int i = 0; i < length; i++) { for (int j = i + 1; j < length; j++) dp[i][j] = Integer.MAX_VALUE; } for (int curLength = 1; curLength < length; curLength++) { int start = 0, end = length - curLength; for (int i = start; i < end; i++) { int midStart = i, midEnd = i + curLength; for (int j = midStart; j < midEnd; j++) dp[i][i + curLength] = Math.min(dp[i][i + curLength], dp[i][j] + dp[j + 1][i + curLength] + maxValues[i][j] * maxValues[j + 1][i + curLength]); } } return dp[0][length - 1]; } }
-
// OJ: https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ // Time: O(N^3) // Space: O(N^2) class Solution { public: int mctFromLeafValues(vector<int>& A) { int N = A.size(); vector<vector<int>> mx(N, vector<int>(N)), dp(N, vector<int>(N, INT_MAX)); for (int i = 0; i < N; ++i) { mx[i][i] = A[i]; for (int j = i + 1; j < N; ++j) mx[i][j] = max(mx[i][j - 1], A[j]); } for (int i = 0; i < N; ++i) dp[i][i] = 0; for (int i = N - 2; i >= 0; --i) { for (int j = i + 1; j < N; ++j) { for (int k = i; k < j; ++k) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + mx[i][k] * mx[k + 1][j]); } } } return dp[0][N - 1]; } };
-
# 1130. Minimum Cost Tree From Leaf Values # https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: res = 0 while len(arr) > 1: index = arr.index(min(arr)) if 0 < index < len(arr) - 1: res += arr[index] * min(arr[index + 1], arr[index - 1]) else: res += arr[index] * (arr[index + 1] if index == 0 else arr[index - 1]) arr.pop(index) return res
-
func mctFromLeafValues(arr []int) int { n := len(arr) f := make([][]int, n) g := make([][]int, n) for i := range g { f[i] = make([]int, n) g[i] = make([]int, n) g[i][i] = arr[i] for j := i + 1; j < n; j++ { g[i][j] = max(g[i][j-1], arr[j]) } } var dfs func(int, int) int dfs = func(i, j int) int { if i == j { return 0 } if f[i][j] > 0 { return f[i][j] } f[i][j] = 1 << 30 for k := i; k < j; k++ { f[i][j] = min(f[i][j], dfs(i, k)+dfs(k+1, j)+g[i][k]*g[k+1][j]) } return f[i][j] } return dfs(0, n-1) } func max(a, b int) int { if a > b { return a } return b } func min(a, b int) int { if a < b { return a } return b }
-
function mctFromLeafValues(arr: number[]): number { const n = arr.length; const f: number[][] = new Array(n).fill(0).map(() => new Array(n).fill(0)); const g: number[][] = new Array(n).fill(0).map(() => new Array(n).fill(0)); for (let i = n - 1; i >= 0; --i) { g[i][i] = arr[i]; for (let j = i + 1; j < n; ++j) { g[i][j] = Math.max(g[i][j - 1], arr[j]); f[i][j] = 1 << 30; for (let k = i; k < j; ++k) { f[i][j] = Math.min( f[i][j], f[i][k] + f[k + 1][j] + g[i][k] * g[k + 1][j], ); } } } return f[0][n - 1]; }