Skip to content

Add solution and test-cases for problem 3405 #1236

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading