Skip to content

Commit c1a077c

Browse files
committedApr 22, 2025·
Add solution and test-cases for problem 2338
1 parent a01e89c commit c1a077c

File tree

3 files changed

+99
-27
lines changed

3 files changed

+99
-27
lines changed
 

‎leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/README.md

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [2338.Count the Number of Ideal Arrays][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two integers `n` and `maxValue`, which are used to describe an **ideal** array.
5+
6+
A **0-indexed** integer array `arr` of length `n` is considered **ideal** if the following conditions hold:
7+
8+
- Every `arr[i]` is a value from `1` to `maxValue`, for `0 <= i < n`.
9+
- Every `arr[i]` is divisible by `arr[i - 1]`, for `0 < i < n`.
10+
11+
Return the number of **distinct** ideal arrays of length `n`. Since the answer may be very large, return it modulo `10^9 + 7`.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: n = 2, maxValue = 5
17+
Output: 10
18+
Explanation: The following are the possible ideal arrays:
19+
- Arrays starting with the value 1 (5 arrays): [1,1], [1,2], [1,3], [1,4], [1,5]
20+
- Arrays starting with the value 2 (2 arrays): [2,2], [2,4]
21+
- Arrays starting with the value 3 (1 array): [3,3]
22+
- Arrays starting with the value 4 (1 array): [4,4]
23+
- Arrays starting with the value 5 (1 array): [5,5]
24+
There are a total of 5 + 2 + 1 + 1 + 1 = 10 distinct ideal arrays.
1325
```
1426

15-
## 题意
16-
> ...
27+
**Example 2:**
1728

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Count the Number of Ideal Arrays
23-
```go
2429
```
25-
30+
Input: n = 5, maxValue = 3
31+
Output: 11
32+
Explanation: The following are the possible ideal arrays:
33+
- Arrays starting with the value 1 (9 arrays):
34+
- With no other distinct values (1 array): [1,1,1,1,1]
35+
- With 2nd distinct value 2 (4 arrays): [1,1,1,1,2], [1,1,1,2,2], [1,1,2,2,2], [1,2,2,2,2]
36+
- With 2nd distinct value 3 (4 arrays): [1,1,1,1,3], [1,1,1,3,3], [1,1,3,3,3], [1,3,3,3,3]
37+
- Arrays starting with the value 2 (1 array): [2,2,2,2,2]
38+
- Arrays starting with the value 3 (1 array): [3,3,3,3,3]
39+
There are a total of 9 + 1 + 1 = 11 distinct ideal arrays.
40+
```
2641

2742
## 结语
2843

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const (
4+
MOD int = 1e9 + 7
5+
MAX_N = 10010
6+
MAX_P = 15 // There are up to 15 prime factors
7+
)
8+
9+
var (
10+
c [MAX_N + MAX_P][MAX_P + 1]int
11+
sieve [MAX_N]int // Minimum prime factor
12+
ps [MAX_N][]int // List of prime factor counts
13+
)
14+
15+
func initialize() {
16+
if c[0][0] != 0 {
17+
return
18+
}
19+
20+
for i := 2; i < MAX_N; i++ {
21+
if sieve[i] == 0 {
22+
for j := i; j < MAX_N; j += i {
23+
if sieve[j] == 0 {
24+
sieve[j] = i
25+
}
26+
}
27+
}
28+
}
29+
30+
for i := 2; i < MAX_N; i++ {
31+
x := i
32+
for x > 1 {
33+
p := sieve[x]
34+
cnt := 0
35+
for x%p == 0 {
36+
x /= p
37+
cnt++
38+
}
39+
ps[i] = append(ps[i], cnt)
40+
}
41+
}
42+
43+
c[0][0] = 1
44+
for i := 1; i < MAX_N+MAX_P; i++ {
45+
c[i][0] = 1
46+
for j := 1; j <= MAX_P && j <= i; j++ {
47+
c[i][j] = (c[i-1][j] + c[i-1][j-1]) % MOD
48+
}
49+
}
50+
}
51+
52+
func Solution(n int, maxValue int) int {
53+
initialize()
54+
ans := 0
55+
for x := 1; x <= maxValue; x++ {
56+
mul := 1
57+
for _, p := range ps[x] {
58+
mul = mul * c[n+p-1][p] % MOD
59+
}
60+
ans = (ans + mul) % MOD
61+
}
62+
return ans
563
}

‎leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
n, maxValue int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 2, 5, 10},
17+
{"TestCase2", 5, 3, 11},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.n, c.maxValue)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.n, c.maxValue)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)
Please sign in to comment.