|
42 | 42 |
|
43 | 43 | <!-- 这里可写通用的实现逻辑 -->
|
44 | 44 |
|
| 45 | +**方法一:动态规划** |
| 46 | + |
| 47 | +我们可以枚举 `arr` 中的每一个数 $a$ 作为二叉树的根节点(根节点一定最大),然后枚举枚举左子树的值 $b$,若 $a$ 能被 $b$ 整除,则右子树的值为 $a / b$,若 $a / b$ 也在 `arr` 中,则可以构成一棵二叉树。此时,以 $a$ 为根节点的二叉树的个数为 $f(a) = f(b) \times f(a / b)$,其中 $f(b)$ 和 $f(a / b)$ 分别为左子树和右子树的二叉树个数。 |
| 48 | + |
| 49 | +因此,我们先将 `arr` 排序,然后用 $f[i]$ 表示以 $arr[i]$ 为根节点的二叉树的个数,最终答案即为 $f[0] + f[1] + \cdots + f[n - 1]$。 |
| 50 | + |
| 51 | +时间复杂度为 $O(n^2)$,空间复杂度为 $O(n)$。其中 $n$ 为 `arr` 的长度。 |
| 52 | + |
45 | 53 | <!-- tabs:start -->
|
46 | 54 |
|
47 | 55 | ### **Python3**
|
48 | 56 |
|
49 | 57 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
50 | 58 |
|
51 | 59 | ```python
|
52 |
| - |
| 60 | +class Solution: |
| 61 | + def numFactoredBinaryTrees(self, arr: List[int]) -> int: |
| 62 | + mod = 10**9 + 7 |
| 63 | + n = len(arr) |
| 64 | + arr.sort() |
| 65 | + idx = {v: i for i, v in enumerate(arr)} |
| 66 | + f = [1] * n |
| 67 | + for i, a in enumerate(arr): |
| 68 | + for j in range(i): |
| 69 | + b = arr[j] |
| 70 | + if a % b == 0 and (c := (a // b)) in idx: |
| 71 | + f[i] = (f[i] + f[j] * f[idx[c]]) % mod |
| 72 | + return sum(f) % mod |
53 | 73 | ```
|
54 | 74 |
|
55 | 75 | ### **Java**
|
56 | 76 |
|
57 | 77 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
58 | 78 |
|
59 | 79 | ```java
|
| 80 | +class Solution { |
| 81 | + private static final int MOD = (int) 1e9 + 7; |
| 82 | + |
| 83 | + public int numFactoredBinaryTrees(int[] arr) { |
| 84 | + Arrays.sort(arr); |
| 85 | + int n = arr.length; |
| 86 | + long[] f = new long[n]; |
| 87 | + Arrays.fill(f, 1); |
| 88 | + Map<Integer, Integer> idx = new HashMap<>(n); |
| 89 | + for (int i = 0; i < n; ++i) { |
| 90 | + idx.put(arr[i], i); |
| 91 | + } |
| 92 | + for (int i = 0; i < n; ++i) { |
| 93 | + int a = arr[i]; |
| 94 | + for (int j = 0; j < i; ++j) { |
| 95 | + int b = arr[j]; |
| 96 | + if (a % b == 0) { |
| 97 | + int c = a / b; |
| 98 | + if (idx.containsKey(c)) { |
| 99 | + int k = idx.get(c); |
| 100 | + f[i] = (f[i] + f[j] * f[k]) % MOD; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + long ans = 0; |
| 106 | + for (long v : f) { |
| 107 | + ans = (ans + v) % MOD; |
| 108 | + } |
| 109 | + return (int) ans; |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +### **C++** |
| 115 | + |
| 116 | +```cpp |
| 117 | +class Solution { |
| 118 | +public: |
| 119 | + const int mod = 1e9 + 7; |
| 120 | + |
| 121 | + int numFactoredBinaryTrees(vector<int>& arr) { |
| 122 | + sort(arr.begin(), arr.end()); |
| 123 | + unordered_map<int, int> idx; |
| 124 | + int n = arr.size(); |
| 125 | + for (int i = 0; i < n; ++i) { |
| 126 | + idx[arr[i]] = i; |
| 127 | + } |
| 128 | + vector<long> f(n, 1); |
| 129 | + for (int i = 0; i < n; ++i) { |
| 130 | + int a = arr[i]; |
| 131 | + for (int j = 0; j < i; ++j) { |
| 132 | + int b = arr[j]; |
| 133 | + if (a % b == 0) { |
| 134 | + int c = a / b; |
| 135 | + if (idx.count(c)) { |
| 136 | + int k = idx[c]; |
| 137 | + f[i] = (f[i] + 1l * f[j] * f[k]) % mod; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + long ans = 0; |
| 143 | + for (long v : f) { |
| 144 | + ans = (ans + v) % mod; |
| 145 | + } |
| 146 | + return ans; |
| 147 | + } |
| 148 | +}; |
| 149 | +``` |
60 | 150 |
|
| 151 | +### **Go** |
| 152 | +
|
| 153 | +```go |
| 154 | +func numFactoredBinaryTrees(arr []int) int { |
| 155 | + const mod int = 1e9 + 7 |
| 156 | + sort.Ints(arr) |
| 157 | + f := make([]int, len(arr)) |
| 158 | + for i := range f { |
| 159 | + f[i] = 1 |
| 160 | + } |
| 161 | + idx := map[int]int{} |
| 162 | + for i, v := range arr { |
| 163 | + idx[v] = i |
| 164 | + } |
| 165 | + for i, a := range arr { |
| 166 | + for j := 0; j < i; j++ { |
| 167 | + b := arr[j] |
| 168 | + if a%b == 0 { |
| 169 | + c := a / b |
| 170 | + if k, ok := idx[c]; ok { |
| 171 | + f[i] = (f[i] + f[j]*f[k]) % mod |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + ans := 0 |
| 177 | + for _, v := range f { |
| 178 | + ans = (ans + v) % mod |
| 179 | + } |
| 180 | + return ans |
| 181 | +} |
61 | 182 | ```
|
62 | 183 |
|
63 | 184 | ### **...**
|
|
0 commit comments