Skip to content

feat: add solutions to lc problem: No.0823 #1536

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

Merged
merged 1 commit into from
Aug 29, 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
52 changes: 38 additions & 14 deletions solution/0800-0899/0823.Binary Trees With Factors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@

**方法一:动态规划**

我们可以枚举 `arr` 中的每一个数 $a$ 作为二叉树的根节点(根节点一定最大),然后枚举枚举左子树的值 $b$,若 $a$ 能被 $b$ 整除,则右子树的值为 $a / b$,若 $a / b$ 也在 `arr` 中,则可以构成一棵二叉树。此时,以 $a$ 为根节点的二叉树的个数为 $f(a) = f(b) \times f(a / b)$,其中 $f(b)$ 和 $f(a / b)$ 分别为左子树和右子树的二叉树个数。
我们可以枚举 $arr$ 中的每一个数 $a$ 作为二叉树的根节点(根节点一定最大),然后枚举枚举左子树的值 $b$,若 $a$ 能被 $b$ 整除,则右子树的值为 $a / b$,若 $a / b$ 也在 $arr$ 中,则可以构成一棵二叉树。此时,以 $a$ 为根节点的二叉树的个数为 $f(a) = f(b) \times f(a / b)$,其中 $f(b)$ 和 $f(a / b)$ 分别为左子树和右子树的二叉树个数。

因此,我们先将 `arr` 排序,然后用 $f[i]$ 表示以 $arr[i]$ 为根节点的二叉树的个数,最终答案即为 $f[0] + f[1] + \cdots + f[n - 1]$。
因此,我们先将 $arr$ 排序,然后用 $f[i]$ 表示以 $arr[i]$ 为根节点的二叉树的个数,最终答案即为 $f[0] + f[1] + \cdots + f[n - 1]$。注意答案可能很大,需要对 $10^9 + 7$ 取模

时间复杂度为 $O(n^2)$,空间复杂度为 $O(n)$。其中 $n$ 为 `arr` 的长度。
时间复杂度为 $O(n^2)$,空间复杂度为 $O(n)$。其中 $n$ 为 $arr$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -78,9 +78,8 @@ class Solution:

```java
class Solution {
private static final int MOD = (int) 1e9 + 7;

public int numFactoredBinaryTrees(int[] arr) {
final int mod = (int) 1e9 + 7;
Arrays.sort(arr);
int n = arr.length;
long[] f = new long[n];
Expand All @@ -97,14 +96,14 @@ class Solution {
int c = a / b;
if (idx.containsKey(c)) {
int k = idx.get(c);
f[i] = (f[i] + f[j] * f[k]) % MOD;
f[i] = (f[i] + f[j] * f[k]) % mod;
}
}
}
}
long ans = 0;
for (long v : f) {
ans = (ans + v) % MOD;
ans = (ans + v) % mod;
}
return (int) ans;
}
Expand All @@ -116,9 +115,8 @@ class Solution {
```cpp
class Solution {
public:
const int mod = 1e9 + 7;

int numFactoredBinaryTrees(vector<int>& arr) {
const int mod = 1e9 + 7;
sort(arr.begin(), arr.end());
unordered_map<int, int> idx;
int n = arr.size();
Expand Down Expand Up @@ -155,18 +153,15 @@ func numFactoredBinaryTrees(arr []int) int {
const mod int = 1e9 + 7
sort.Ints(arr)
f := make([]int, len(arr))
for i := range f {
f[i] = 1
}
idx := map[int]int{}
for i, v := range arr {
f[i] = 1
idx[v] = i
}
for i, a := range arr {
for j := 0; j < i; j++ {
b := arr[j]
if a%b == 0 {
c := a / b
if c := a / b; a%b == 0 {
if k, ok := idx[c]; ok {
f[i] = (f[i] + f[j]*f[k]) % mod
}
Expand All @@ -181,6 +176,35 @@ func numFactoredBinaryTrees(arr []int) int {
}
```

### **TypeScript**

```ts
function numFactoredBinaryTrees(arr: number[]): number {
const mod = 10 ** 9 + 7;
arr.sort((a, b) => a - b);
const idx: Map<number, number> = new Map();
const n = arr.length;
for (let i = 0; i < n; ++i) {
idx.set(arr[i], i);
}
const f: number[] = new Array(n).fill(1);
for (let i = 0; i < n; ++i) {
const a = arr[i];
for (let j = 0; j < i; ++j) {
const b = arr[j];
if (a % b === 0) {
const c = a / b;
if (idx.has(c)) {
const k = idx.get(c)!;
f[i] = (f[i] + f[j] * f[k]) % mod;
}
}
}
}
return f.reduce((a, b) => a + b) % mod;
}
```

### **...**

```
Expand Down
46 changes: 35 additions & 11 deletions solution/0800-0899/0823.Binary Trees With Factors/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ class Solution:

```java
class Solution {
private static final int MOD = (int) 1e9 + 7;

public int numFactoredBinaryTrees(int[] arr) {
final int mod = (int) 1e9 + 7;
Arrays.sort(arr);
int n = arr.length;
long[] f = new long[n];
Expand All @@ -79,14 +78,14 @@ class Solution {
int c = a / b;
if (idx.containsKey(c)) {
int k = idx.get(c);
f[i] = (f[i] + f[j] * f[k]) % MOD;
f[i] = (f[i] + f[j] * f[k]) % mod;
}
}
}
}
long ans = 0;
for (long v : f) {
ans = (ans + v) % MOD;
ans = (ans + v) % mod;
}
return (int) ans;
}
Expand All @@ -98,9 +97,8 @@ class Solution {
```cpp
class Solution {
public:
const int mod = 1e9 + 7;

int numFactoredBinaryTrees(vector<int>& arr) {
const int mod = 1e9 + 7;
sort(arr.begin(), arr.end());
unordered_map<int, int> idx;
int n = arr.size();
Expand Down Expand Up @@ -137,18 +135,15 @@ func numFactoredBinaryTrees(arr []int) int {
const mod int = 1e9 + 7
sort.Ints(arr)
f := make([]int, len(arr))
for i := range f {
f[i] = 1
}
idx := map[int]int{}
for i, v := range arr {
f[i] = 1
idx[v] = i
}
for i, a := range arr {
for j := 0; j < i; j++ {
b := arr[j]
if a%b == 0 {
c := a / b
if c := a / b; a%b == 0 {
if k, ok := idx[c]; ok {
f[i] = (f[i] + f[j]*f[k]) % mod
}
Expand All @@ -163,6 +158,35 @@ func numFactoredBinaryTrees(arr []int) int {
}
```

### **TypeScript**

```ts
function numFactoredBinaryTrees(arr: number[]): number {
const mod = 10 ** 9 + 7;
arr.sort((a, b) => a - b);
const idx: Map<number, number> = new Map();
const n = arr.length;
for (let i = 0; i < n; ++i) {
idx.set(arr[i], i);
}
const f: number[] = new Array(n).fill(1);
for (let i = 0; i < n; ++i) {
const a = arr[i];
for (let j = 0; j < i; ++j) {
const b = arr[j];
if (a % b === 0) {
const c = a / b;
if (idx.has(c)) {
const k = idx.get(c)!;
f[i] = (f[i] + f[j] * f[k]) % mod;
}
}
}
}
return f.reduce((a, b) => a + b) % mod;
}
```

### **...**

```
Expand Down
61 changes: 30 additions & 31 deletions solution/0800-0899/0823.Binary Trees With Factors/Solutioin.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
class Solution {
private static final int MOD = (int) 1e9 + 7;

public int numFactoredBinaryTrees(int[] arr) {
Arrays.sort(arr);
int n = arr.length;
long[] f = new long[n];
Arrays.fill(f, 1);
Map<Integer, Integer> idx = new HashMap<>(n);
for (int i = 0; i < n; ++i) {
idx.put(arr[i], i);
}
for (int i = 0; i < n; ++i) {
int a = arr[i];
for (int j = 0; j < i; ++j) {
int b = arr[j];
if (a % b == 0) {
int c = a / b;
if (idx.containsKey(c)) {
int k = idx.get(c);
f[i] = (f[i] + f[j] * f[k]) % MOD;
}
}
}
}
long ans = 0;
for (long v : f) {
ans = (ans + v) % MOD;
}
return (int) ans;
}
class Solution {
public int numFactoredBinaryTrees(int[] arr) {
final int mod = (int) 1e9 + 7;
Arrays.sort(arr);
int n = arr.length;
long[] f = new long[n];
Arrays.fill(f, 1);
Map<Integer, Integer> idx = new HashMap<>(n);
for (int i = 0; i < n; ++i) {
idx.put(arr[i], i);
}
for (int i = 0; i < n; ++i) {
int a = arr[i];
for (int j = 0; j < i; ++j) {
int b = arr[j];
if (a % b == 0) {
int c = a / b;
if (idx.containsKey(c)) {
int k = idx.get(c);
f[i] = (f[i] + f[j] * f[k]) % mod;
}
}
}
}
long ans = 0;
for (long v : f) {
ans = (ans + v) % mod;
}
return (int) ans;
}
}
61 changes: 30 additions & 31 deletions solution/0800-0899/0823.Binary Trees With Factors/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
class Solution {
public:
const int mod = 1e9 + 7;

int numFactoredBinaryTrees(vector<int>& arr) {
sort(arr.begin(), arr.end());
unordered_map<int, int> idx;
int n = arr.size();
for (int i = 0; i < n; ++i) {
idx[arr[i]] = i;
}
vector<long> f(n, 1);
for (int i = 0; i < n; ++i) {
int a = arr[i];
for (int j = 0; j < i; ++j) {
int b = arr[j];
if (a % b == 0) {
int c = a / b;
if (idx.count(c)) {
int k = idx[c];
f[i] = (f[i] + 1l * f[j] * f[k]) % mod;
}
}
}
}
long ans = 0;
for (long v : f) {
ans = (ans + v) % mod;
}
return ans;
}
class Solution {
public:
int numFactoredBinaryTrees(vector<int>& arr) {
const int mod = 1e9 + 7;
sort(arr.begin(), arr.end());
unordered_map<int, int> idx;
int n = arr.size();
for (int i = 0; i < n; ++i) {
idx[arr[i]] = i;
}
vector<long> f(n, 1);
for (int i = 0; i < n; ++i) {
int a = arr[i];
for (int j = 0; j < i; ++j) {
int b = arr[j];
if (a % b == 0) {
int c = a / b;
if (idx.count(c)) {
int k = idx[c];
f[i] = (f[i] + 1l * f[j] * f[k]) % mod;
}
}
}
}
long ans = 0;
for (long v : f) {
ans = (ans + v) % mod;
}
return ans;
}
};
7 changes: 2 additions & 5 deletions solution/0800-0899/0823.Binary Trees With Factors/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ func numFactoredBinaryTrees(arr []int) int {
const mod int = 1e9 + 7
sort.Ints(arr)
f := make([]int, len(arr))
for i := range f {
f[i] = 1
}
idx := map[int]int{}
for i, v := range arr {
f[i] = 1
idx[v] = i
}
for i, a := range arr {
for j := 0; j < i; j++ {
b := arr[j]
if a%b == 0 {
c := a / b
if c := a / b; a%b == 0 {
if k, ok := idx[c]; ok {
f[i] = (f[i] + f[j]*f[k]) % mod
}
Expand Down
24 changes: 24 additions & 0 deletions solution/0800-0899/0823.Binary Trees With Factors/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function numFactoredBinaryTrees(arr: number[]): number {
const mod = 10 ** 9 + 7;
arr.sort((a, b) => a - b);
const idx: Map<number, number> = new Map();
const n = arr.length;
for (let i = 0; i < n; ++i) {
idx.set(arr[i], i);
}
const f: number[] = new Array(n).fill(1);
for (let i = 0; i < n; ++i) {
const a = arr[i];
for (let j = 0; j < i; ++j) {
const b = arr[j];
if (a % b === 0) {
const c = a / b;
if (idx.has(c)) {
const k = idx.get(c)!;
f[i] = (f[i] + f[j] * f[k]) % mod;
}
}
}
}
return f.reduce((a, b) => a + b) % mod;
}