From 89ee25617b3410210e79a5078367a41e8c483a12 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 26 Oct 2023 22:19:36 +0800 Subject: [PATCH] Add solution and test-cases for problem 823 --- .../0823.Binary-Trees-With-Factors/README.md | 27 ++++++++--------- .../Solution.go | 30 +++++++++++++++++-- .../Solution_test.go | 14 ++++----- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/leetcode/801-900/0823.Binary-Trees-With-Factors/README.md b/leetcode/801-900/0823.Binary-Trees-With-Factors/README.md index c4f599d8..14efe3ba 100644 --- a/leetcode/801-900/0823.Binary-Trees-With-Factors/README.md +++ b/leetcode/801-900/0823.Binary-Trees-With-Factors/README.md @@ -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]. +``` ## 结语 diff --git a/leetcode/801-900/0823.Binary-Trees-With-Factors/Solution.go b/leetcode/801-900/0823.Binary-Trees-With-Factors/Solution.go index d115ccf5..c7e19f12 100644 --- a/leetcode/801-900/0823.Binary-Trees-With-Factors/Solution.go +++ b/leetcode/801-900/0823.Binary-Trees-With-Factors/Solution.go @@ -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 } diff --git a/leetcode/801-900/0823.Binary-Trees-With-Factors/Solution_test.go b/leetcode/801-900/0823.Binary-Trees-With-Factors/Solution_test.go index 14ff50eb..f0243ad6 100644 --- a/leetcode/801-900/0823.Binary-Trees-With-Factors/Solution_test.go +++ b/leetcode/801-900/0823.Binary-Trees-With-Factors/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }