Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 823 #671

Merged
merged 1 commit into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
}
Loading