From c1a077cb15eb4160be6327f3d2ffc783bb11aa9f Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 22 Apr 2025 09:22:21 +0800 Subject: [PATCH] Add solution and test-cases for problem 2338 --- .../README.md | 43 ++++++++----- .../Solution.go | 62 ++++++++++++++++++- .../Solution_test.go | 21 +++---- 3 files changed, 99 insertions(+), 27 deletions(-) diff --git a/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/README.md b/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/README.md index d50e3f325..2d69c179a 100755 --- a/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/README.md +++ b/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/Solution.go b/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/Solution.go index d115ccf5e..680447b2b 100644 --- a/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/Solution.go +++ b/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/Solution.go @@ -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 } diff --git a/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/Solution_test.go b/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/Solution_test.go index 14ff50eb4..f7fb76188 100644 --- a/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/Solution_test.go +++ b/leetcode/2301-2400/2338.Count-the-Number-of-Ideal-Arrays/Solution_test.go @@ -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() { }