File tree Expand file tree Collapse file tree 1 file changed +10
-19
lines changed
Algorithms/0823.binary-trees-with-factors Expand file tree Collapse file tree 1 file changed +10
-19
lines changed Original file line number Diff line number Diff line change 11package problem0823
22
3- import (
4- "sort"
5- )
3+ import "sort"
64
75const (
86 modulo = 1E9 + 7
@@ -17,35 +15,24 @@ func numFactoredBinaryTrees(A []int) int {
1715 }
1816
1917 ress := make ([]int , len (A ))
20- for i := range ress {
21- ress [i ] = 1
22- }
2318
24- for i := 1 ; i < len (A ); i ++ {
19+ for i := 0 ; i < len (A ); i ++ {
20+ ress [i ] = 1
2521 for j := 0 ; j < i ; j ++ {
2622 quotient , remainder := A [i ]/ A [j ], A [i ]% A [j ]
23+ k , isFactor := factorIndex [quotient ]
2724
28- if remainder != 0 ||
29- (factorIndex [quotient ] == 0 && quotient != A [0 ]) {
25+ if remainder != 0 || ! isFactor {
3026 continue
3127 }
3228
33- k := factorIndex [quotient ]
34-
35- t := mod (ress [j ] * ress [k ])
36-
37- ress [i ] = mod (ress [i ] + t )
38-
29+ ress [i ] = mod (ress [i ] + ress [j ]* ress [k ])
3930 }
4031 }
4132
4233 return sum (ress )
4334}
4435
45- func mod (n int ) int {
46- return n % modulo
47- }
48-
4936func sum (a []int ) int {
5037 res := 0
5138 for i := range a {
@@ -54,3 +41,7 @@ func sum(a []int) int {
5441 }
5542 return res
5643}
44+
45+ func mod (n int ) int {
46+ return n % modulo
47+ }
You can’t perform that action at this time.
0 commit comments