Skip to content
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

Add solution and test-cases for problem 935 #699

Merged
merged 1 commit into from
Dec 18, 2023
Merged
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: 30 additions & 13 deletions leetcode/901-1000/0935.Knight-Dialer/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
# [935.Knight Dialer][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
The chess knight has a **unique movement**, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an **L**). The possible movements of chess knight are shown in this diagaram:

A chess knight can move as indicated in the chess diagram below:

![chess](./chess.jpeg)

We have a chess knight and a phone pad as shown below, the knight **can only stand on a numeric cell** (i.e. blue cell).

![phone](./phone.jpeg)

Given an integer n, return how many distinct phone numbers of length `n` we can dial.

You are allowed to place the knight **on any numeric cell** initially and then you should perform `n - 1` jumps to dial a number of length `n`. All jumps should be **valid** knight jumps.

As the answer may be very large, **return the answer modulo** `10^9 + 7`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Knight Dialer
```go
```
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
```

**Example 3:**

```
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
```

## 结语

Expand Down
43 changes: 41 additions & 2 deletions leetcode/901-1000/0935.Knight-Dialer/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
package Solution

func Solution(x bool) bool {
return x
const mod935 = 1000000007

func Solution(n int) int {
nexts := map[int][]int{
1: {6, 8},
2: {7, 9},
3: {4, 8},
4: {0, 3, 9},
6: {0, 1, 7},
7: {2, 6},
8: {1, 3},
9: {4, 2},
0: {4, 6},
}
cache := make(map[int]map[int]int)
for i := 0; i <= 9; i++ {
cache[i] = make(map[int]int)
cache[i][1] = 1
}
var dfs func(int, int) int
dfs = func(num, nn int) int {
v, ok := cache[num]
if ok {
vv, ok1 := v[nn]
if ok1 {
return vv
}
}
ans := 0
for _, next := range nexts[num] {
ans = (ans + dfs(next, nn-1)) % mod935
}
cache[num][nn] = ans
return ans
}
ans := 0
for i := 0; i <= 9; i++ {
a := dfs(i, n) % mod935
ans = (ans + a) % mod935
}
return ans
}
16 changes: 9 additions & 7 deletions leetcode/901-1000/0935.Knight-Dialer/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 1, 10},
{"TestCase2", 2, 20},
{"TestCase3", 3, 46},
{"TestCase4", 20, 58689536},
{"TestCase5", 3131, 136006598},
}

// 开始测试
Expand All @@ -30,10 +32,10 @@ func TestSolution(t *testing.T) {
}
}

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
Binary file added leetcode/901-1000/0935.Knight-Dialer/chess.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/901-1000/0935.Knight-Dialer/phone.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading