Skip to content

Commit 9333a22

Browse files
authored
Create 823-binary-trees-with-factors.js
1 parent a41560b commit 9333a22

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

823-binary-trees-with-factors.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} A
3+
* @return {number}
4+
*/
5+
const numFactoredBinaryTrees = function(A) {
6+
const mod = 10 ** 9 + 7
7+
let res = 0
8+
A.sort((a, b) => a - b)
9+
const dp = {}
10+
for(let i = 0; i < A.length; i++) {
11+
dp[A[i]] = 1
12+
for(let j = 0; j < i; j++) {
13+
if(A[i] % A[j] === 0 && dp.hasOwnProperty(Math.floor( A[i] / A[j]))) {
14+
dp[A[i]] = (dp[A[i]] + dp[A[j]] * dp[Math.floor(A[i] / A[j])]) % mod
15+
}
16+
}
17+
}
18+
for(let el of Object.values(dp)) res = (res + el) % mod
19+
return res
20+
};

0 commit comments

Comments
 (0)