Skip to content

Commit c1c48d5

Browse files
aQuaaQua
authored andcommitted
823 wrong answer
1 parent 4792a5a commit c1c48d5

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed
Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,53 @@
11
package problem0823
22

3+
import (
4+
"sort"
5+
)
6+
7+
const (
8+
modulo = 1E9 + 7
9+
)
10+
311
func numFactoredBinaryTrees(A []int) int {
4-
5-
return 0
12+
sort.Ints(A)
13+
14+
factorIndex := make(map[int]int, len(A))
15+
for i := range A {
16+
factorIndex[A[i]] = i
17+
}
18+
19+
ress := make([]int, len(A))
20+
for i := range ress {
21+
ress[i] = 1
22+
}
23+
24+
for i := 1; i < len(A); i++ {
25+
for j := 0; j < i; j++ {
26+
quotient, remainder := A[i]/A[j], A[i]%A[j]
27+
28+
if remainder != 0 ||
29+
(factorIndex[quotient] == 0 && quotient != A[0]) {
30+
continue
31+
}
32+
33+
ress[i] += ress[j]
34+
ress[i] = mod(ress[i])
35+
36+
}
37+
}
38+
39+
return sum(ress)
40+
}
41+
42+
func mod(n int) int {
43+
return n % modulo
44+
}
45+
46+
func sum(a []int) int {
47+
res := 0
48+
for i := range a {
49+
res += a[i]
50+
res = mod(res)
51+
}
52+
return res
653
}

Algorithms/0823.binary-trees-with-factors/binary-trees-with-factors_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ var tcs = []struct {
1313
ans int
1414
}{
1515

16+
{
17+
[]int{18, 3, 6, 2},
18+
12,
19+
},
20+
1621
{
1722
[]int{2, 4},
1823
3,

0 commit comments

Comments
 (0)