Skip to content

Add solution and test-cases for problem 2338 #1183

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
43 changes: 29 additions & 14 deletions leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
# [2338.Count the Number of Ideal Arrays][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
You are given two integers `n` and `maxValue`, which are used to describe an **ideal** array.

A **0-indexed** integer array `arr` of length `n` is considered **ideal** if the following conditions hold:

- Every `arr[i]` is a value from `1` to `maxValue`, for `0 <= i < n`.
- Every `arr[i]` is divisible by `arr[i - 1]`, for `0 < i < n`.

Return the number of **distinct** ideal arrays of length `n`. Since the answer may be very large, return it modulo `10^9 + 7`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 2, maxValue = 5
Output: 10
Explanation: The following are the possible ideal arrays:
- Arrays starting with the value 1 (5 arrays): [1,1], [1,2], [1,3], [1,4], [1,5]
- Arrays starting with the value 2 (2 arrays): [2,2], [2,4]
- Arrays starting with the value 3 (1 array): [3,3]
- Arrays starting with the value 4 (1 array): [4,4]
- Arrays starting with the value 5 (1 array): [5,5]
There are a total of 5 + 2 + 1 + 1 + 1 = 10 distinct ideal arrays.
```

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

## 题解

### 思路1
> ...
Count the Number of Ideal Arrays
```go
```

Input: n = 5, maxValue = 3
Output: 11
Explanation: The following are the possible ideal arrays:
- Arrays starting with the value 1 (9 arrays):
- With no other distinct values (1 array): [1,1,1,1,1]
- 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]
- 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]
- Arrays starting with the value 2 (1 array): [2,2,2,2,2]
- Arrays starting with the value 3 (1 array): [3,3,3,3,3]
There are a total of 9 + 1 + 1 = 11 distinct ideal arrays.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
package Solution

func Solution(x bool) bool {
return x
const (
MOD int = 1e9 + 7
MAX_N = 10010
MAX_P = 15 // There are up to 15 prime factors
)

var (
c [MAX_N + MAX_P][MAX_P + 1]int
sieve [MAX_N]int // Minimum prime factor
ps [MAX_N][]int // List of prime factor counts
)

func initialize() {
if c[0][0] != 0 {
return
}

for i := 2; i < MAX_N; i++ {
if sieve[i] == 0 {
for j := i; j < MAX_N; j += i {
if sieve[j] == 0 {
sieve[j] = i
}
}
}
}

for i := 2; i < MAX_N; i++ {
x := i
for x > 1 {
p := sieve[x]
cnt := 0
for x%p == 0 {
x /= p
cnt++
}
ps[i] = append(ps[i], cnt)
}
}

c[0][0] = 1
for i := 1; i < MAX_N+MAX_P; i++ {
c[i][0] = 1
for j := 1; j <= MAX_P && j <= i; j++ {
c[i][j] = (c[i-1][j] + c[i-1][j-1]) % MOD
}
}
}

func Solution(n int, maxValue int) int {
initialize()
ans := 0
for x := 1; x <= maxValue; x++ {
mul := 1
for _, p := range ps[x] {
mul = mul * c[n+p-1][p] % MOD
}
ans = (ans + mul) % MOD
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
n, maxValue int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 2, 5, 10},
{"TestCase2", 5, 3, 11},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.maxValue)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n, c.maxValue)
}
})
}
}

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

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