From ae5dfe601b8974b18e38bafa4f0fe3f11e4142e4 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 17 Jun 2025 08:59:35 +0800 Subject: [PATCH] Add solution and test-cases for problem 3405 --- .../' | 37 ++++++++++++++ .../README.md | 49 ++++++++++++++----- .../Solution.go | 45 ++++++++++++++++- .../Solution_test.go | 22 ++++----- 4 files changed, 128 insertions(+), 25 deletions(-) create mode 100644 leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/' diff --git a/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/' b/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/' new file mode 100644 index 000000000..3d4e83665 --- /dev/null +++ b/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/' @@ -0,0 +1,37 @@ +# [3405.Count the Number of Arrays with K Matching Adjacent Elements][title] + +## Description + +You are given three integers `n`, `m`, `k`. A **good array** arr of size n is defined as follows: + +Each element in arr is in the inclusive range [1, m]. +Exactly k indices i (where 1 <= i < n) satisfy the condition arr[i - 1] == arr[i]. +Return the number of good arrays that can be formed. + +Since the answer may be very large, return it modulo 109 + 7. + +**Example 1:** + +``` +Input: a = "11", b = "1" +Output: "100" +``` + +## 题意 +> ... + +## 题解 + +### 思路1 +> ... +Count the Number of Arrays with K Matching Adjacent Elements +```go +``` + + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/count-the-number-of-arrays-with-k-matching-adjacent-elements/ +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/README.md b/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/README.md index 1e150cbc8..1ed4c27d2 100755 --- a/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/README.md +++ b/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/README.md @@ -1,28 +1,53 @@ # [3405.Count the Number of Arrays with K Matching Adjacent Elements][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 three integers `n`, `m`, `k`. A **good array** `arr` of size `n` is defined as follows: + +- Each element in `arr` is in the **inclusive** range `[1, m]`. +- Exactly `k` indices `i` (where `1 <= i < n`) satisfy the condition `arr[i - 1] == arr[i]`. + +Return the number of **good arrays** that can be formed. + +Since the answer may be very large, return it **modulo** `10^9 + 7`. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 3, m = 2, k = 1 + +Output: 4 + +Explanation: + +There are 4 good arrays. They are [1, 1, 2], [1, 2, 2], [2, 1, 1] and [2, 2, 1]. +Hence, the answer is 4. ``` -## 题意 -> ... +**Example 2:** -## 题解 +``` +Input: n = 4, m = 2, k = 2 + +Output: 6 + +Explanation: + +The good arrays are [1, 1, 1, 2], [1, 1, 2, 2], [1, 2, 2, 2], [2, 1, 1, 1], [2, 2, 1, 1] and [2, 2, 2, 1]. +Hence, the answer is 6. +``` + +**Example 3:** -### 思路1 -> ... -Count the Number of Arrays with K Matching Adjacent Elements -```go ``` +Input: n = 5, m = 2, k = 0 + +Output: 2 +Explanation: + +The good arrays are [1, 2, 1, 2, 1] and [2, 1, 2, 1, 2]. Hence, the answer is 2. +``` ## 结语 diff --git a/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/Solution.go b/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/Solution.go index d115ccf5e..03eda8b24 100644 --- a/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/Solution.go +++ b/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/Solution.go @@ -1,5 +1,46 @@ package Solution -func Solution(x bool) bool { - return x +const ( + MOD = 1e9 + 7 + MX = 100000 +) + +var ( + fact = make([]int64, MX) + invFact = make([]int64, MX) +) + +func qpow(x int64, n int) int64 { + res := int64(1) + for n > 0 { + if n&1 == 1 { + res = res * x % MOD + } + x = x * x % MOD + n >>= 1 + } + return res +} + +func initFact() { + if fact[0] != 0 { + return + } + fact[0] = 1 + for i := 1; i < MX; i++ { + fact[i] = fact[i-1] * int64(i) % MOD + } + invFact[MX-1] = qpow(fact[MX-1], MOD-2) + for i := MX - 1; i > 0; i-- { + invFact[i-1] = invFact[i] * int64(i) % MOD + } +} + +func comb(n, m int) int64 { + return fact[n] * invFact[m] % MOD * invFact[n-m] % MOD +} + +func Solution(n, m, k int) int { + initFact() + return int(comb(n-1, k) * int64(m) % MOD * qpow(int64(m-1), n-k-1) % MOD) } diff --git a/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/Solution_test.go b/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/Solution_test.go index 14ff50eb4..96e9ba3c6 100644 --- a/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/Solution_test.go +++ b/leetcode/3401-3500/3405.Count-the-Number-of-Arrays-with-K-Matching-Adjacent-Elements/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + n, m, k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 3, 2, 1, 4}, + {"TestCase2", 4, 2, 2, 6}, + {"TestCase3", 5, 2, 0, 2}, } // 开始测试 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.m, c.k) 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 %v", + c.expect, got, c.n, c.m, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }