44
44
45
45
** 方法一:动态规划**
46
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)$ 分别为左子树和右子树的二叉树个数。
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
48
49
- 因此,我们先将 ` arr ` 排序,然后用 $f[ i] $ 表示以 $arr[ i] $ 为根节点的二叉树的个数,最终答案即为 $f[ 0] + f[ 1] + \cdots + f[ n - 1] $。
49
+ 因此,我们先将 $ arr$ 排序,然后用 $f[ i] $ 表示以 $arr[ i] $ 为根节点的二叉树的个数,最终答案即为 $f[ 0] + f[ 1] + \cdots + f[ n - 1] $。注意答案可能很大,需要对 $10^9 + 7$ 取模 。
50
50
51
- 时间复杂度为 $O(n^2)$,空间复杂度为 $O(n)$。其中 $n$ 为 ` arr ` 的长度。
51
+ 时间复杂度为 $O(n^2)$,空间复杂度为 $O(n)$。其中 $n$ 为 $ arr$ 的长度。
52
52
53
53
<!-- tabs:start -->
54
54
@@ -78,9 +78,8 @@ class Solution:
78
78
79
79
``` java
80
80
class Solution {
81
- private static final int MOD = (int ) 1e9 + 7 ;
82
-
83
81
public int numFactoredBinaryTrees (int [] arr ) {
82
+ final int mod = (int ) 1e9 + 7 ;
84
83
Arrays . sort(arr);
85
84
int n = arr. length;
86
85
long [] f = new long [n];
@@ -97,14 +96,14 @@ class Solution {
97
96
int c = a / b;
98
97
if (idx. containsKey(c)) {
99
98
int k = idx. get(c);
100
- f[i] = (f[i] + f[j] * f[k]) % MOD ;
99
+ f[i] = (f[i] + f[j] * f[k]) % mod ;
101
100
}
102
101
}
103
102
}
104
103
}
105
104
long ans = 0 ;
106
105
for (long v : f) {
107
- ans = (ans + v) % MOD ;
106
+ ans = (ans + v) % mod ;
108
107
}
109
108
return (int ) ans;
110
109
}
@@ -116,9 +115,8 @@ class Solution {
116
115
``` cpp
117
116
class Solution {
118
117
public:
119
- const int mod = 1e9 + 7;
120
-
121
118
int numFactoredBinaryTrees(vector<int >& arr) {
119
+ const int mod = 1e9 + 7;
122
120
sort(arr.begin(), arr.end());
123
121
unordered_map<int, int> idx;
124
122
int n = arr.size();
@@ -155,18 +153,15 @@ func numFactoredBinaryTrees(arr []int) int {
155
153
const mod int = 1e9 + 7
156
154
sort.Ints(arr)
157
155
f := make([]int, len(arr))
158
- for i := range f {
159
- f[i] = 1
160
- }
161
156
idx := map[int]int{}
162
157
for i, v := range arr {
158
+ f[i] = 1
163
159
idx[v] = i
164
160
}
165
161
for i, a := range arr {
166
162
for j := 0; j < i; j++ {
167
163
b := arr[j]
168
- if a%b == 0 {
169
- c := a / b
164
+ if c := a / b; a%b == 0 {
170
165
if k, ok := idx[c]; ok {
171
166
f[i] = (f[i] + f[j]*f[k]) % mod
172
167
}
@@ -181,6 +176,35 @@ func numFactoredBinaryTrees(arr []int) int {
181
176
}
182
177
```
183
178
179
+ ### ** TypeScript**
180
+
181
+ ``` ts
182
+ function numFactoredBinaryTrees(arr : number []): number {
183
+ const mod = 10 ** 9 + 7 ;
184
+ arr .sort ((a , b ) => a - b );
185
+ const idx: Map <number , number > = new Map ();
186
+ const n = arr .length ;
187
+ for (let i = 0 ; i < n ; ++ i ) {
188
+ idx .set (arr [i ], i );
189
+ }
190
+ const f: number [] = new Array (n ).fill (1 );
191
+ for (let i = 0 ; i < n ; ++ i ) {
192
+ const a = arr [i ];
193
+ for (let j = 0 ; j < i ; ++ j ) {
194
+ const b = arr [j ];
195
+ if (a % b === 0 ) {
196
+ const c = a / b ;
197
+ if (idx .has (c )) {
198
+ const k = idx .get (c )! ;
199
+ f [i ] = (f [i ] + f [j ] * f [k ]) % mod ;
200
+ }
201
+ }
202
+ }
203
+ }
204
+ return f .reduce ((a , b ) => a + b ) % mod ;
205
+ }
206
+ ```
207
+
184
208
### ** ...**
185
209
186
210
```
0 commit comments