Skip to content

Commit

Permalink
Merge pull request #671 from 0xff-dev/823
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 823
  • Loading branch information
6boris authored Oct 28, 2023
2 parents 485c507 + 89ee256 commit c2bac16
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
27 changes: 13 additions & 14 deletions leetcode/801-900/0823.Binary-Trees-With-Factors/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# [823.Binary Trees With Factors][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.

We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.

Return the number of binary trees we can make. The answer may be too large so return the answer **modulo** `10^9 + 7`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: arr = [2,4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Binary Trees With Factors
```go
```

Input: arr = [2,4,5,10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
```

## 结语

Expand Down
30 changes: 28 additions & 2 deletions leetcode/801-900/0823.Binary-Trees-With-Factors/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
package Solution

func Solution(x bool) bool {
return x
import (
"sort"
)

const mod823 = 1000000007

func Solution(arr []int) int {
ans := 0
set := make(map[uint64]int)
for _, n := range arr {
set[uint64(n)] = 1
}
sort.Ints(arr)
for i := 1; i < len(arr); i++ {
for j := i - 1; j >= 0; j-- {
if arr[i]%arr[j] == 0 {
tmp := uint64(arr[i]) / uint64(arr[j])
if count, ok := set[tmp]; ok {
a := set[uint64(arr[j])]
set[uint64(arr[i])] += (a * count) % mod823
}
}
}
}
for _, c := range set {
ans = (ans + c) % mod823
}
return ans
}
14 changes: 7 additions & 7 deletions leetcode/801-900/0823.Binary-Trees-With-Factors/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{2, 4}, 3},
{"TestCase2", []int{2, 4, 5, 10}, 7},
{"TestCase3", []int{2, 4, 5, 10, 100}, 17},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit c2bac16

Please sign in to comment.