Welcome to Subscribe On Youtube
1130. Minimum Cost Tree From Leaf Values
Description
Given an array arr
of positive integers, consider all binary trees such that:
- Each node has either
0
or2
children; - The values of
arr
correspond to the values of each leaf in an in-order traversal of the tree. - 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.
A node is a leaf if and only if it has zero children.
Example 1:
Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees shown. The first has a non-leaf node sum 36, and the second has non-leaf node sum 32.
Example 2:
Input: arr = [4,11] Output: 44
Constraints:
2 <= arr.length <= 40
1 <= arr[i] <= 15
- It is guaranteed that the answer fits into a 32-bit signed integer (i.e., it is less than 231).
Solutions
Solution 1: Memoization Search
According to the problem description, the values in the array
We design a function
The calculation process of the function
- If
, it means that there is only one element in the arrayi=j , and there are no non-leaf nodes, soarr[i..j] .dfs(i,j)=0 - Otherwise, we enumerate
, divide the arrayk∈[i,j−1] into two sub-arraysarr andarr[i..k] . For eacharr[k+1..j] , we recursively calculatek anddfs(i,k) . Here,dfs(k+1,j) represents the minimum possible sum of all non-leaf node values in the index rangedfs(i,k) of the array[i,k] , andarr represents the minimum possible sum of all non-leaf node values in the index rangedfs(k+1,j) of the array[k+1,j] . Soarr .dfs(i,j)=min
In summary, we can get:
In the above recursive process, we can use the method of memoization search to avoid repeated calculations. Additionally, we can use an array
Finally, we return
The time complexity is
Solution 2: Dynamic Programming
We can change the memoization search in Solution 1 to dynamic programming.
Define
Finally, we return
The time complexity is
-
class Solution { private Integer[][] f; private int[][] g; public int mctFromLeafValues(int[] arr) { int n = arr.length; f = new Integer[n][n]; g = new int[n][n]; for (int i = n - 1; i >= 0; --i) { g[i][i] = arr[i]; for (int j = i + 1; j < n; ++j) { g[i][j] = Math.max(g[i][j - 1], arr[j]); } } return dfs(0, n - 1); } private int dfs(int i, int j) { if (i == j) { return 0; } if (f[i][j] != null) { return f[i][j]; } int ans = 1 << 30; for (int k = i; k < j; k++) { ans = Math.min(ans, dfs(i, k) + dfs(k + 1, j) + g[i][k] * g[k + 1][j]); } return f[i][j] = ans; } }
-
class Solution { public: int mctFromLeafValues(vector<int>& arr) { int n = arr.size(); int f[n][n]; int g[n][n]; memset(f, 0, sizeof(f)); for (int i = n - 1; ~i; --i) { g[i][i] = arr[i]; for (int j = i + 1; j < n; ++j) { g[i][j] = max(g[i][j - 1], arr[j]); } } function<int(int, int)> dfs = [&](int i, int j) -> int { if (i == j) { return 0; } if (f[i][j] > 0) { return f[i][j]; } int ans = 1 << 30; for (int k = i; k < j; ++k) { ans = min(ans, dfs(i, k) + dfs(k + 1, j) + g[i][k] * g[k + 1][j]); } return f[i][j] = ans; }; return dfs(0, n - 1); } };
-
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: @cache def dfs(i: int, j: int) -> Tuple: if i == j: return 0, arr[i] s, mx = inf, -1 for k in range(i, j): s1, mx1 = dfs(i, k) s2, mx2 = dfs(k + 1, j) t = s1 + s2 + mx1 * mx2 if s > t: s = t mx = max(mx1, mx2) return s, mx return dfs(0, len(arr) - 1)[0]
-
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) }
-
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]); } } const dfs = (i: number, j: number): number => { if (i === j) { return 0; } if (f[i][j] > 0) { return f[i][j]; } let ans = 1 << 30; for (let k = i; k < j; ++k) { ans = Math.min(ans, dfs(i, k) + dfs(k + 1, j) + g[i][k] * g[k + 1][j]); } return (f[i][j] = ans); }; return dfs(0, n - 1); }