Skip to content

Commit 9392e7b

Browse files
authored
Merge pull request #645 from LetMeFly666/935
problem.935
2 parents 6f3ea87 + 31623a8 commit 9392e7b

8 files changed

+421
-1
lines changed

Codes/0935-knight-dialer.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-10 14:52:45
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-10 15:08:24
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
const vector<vector<int>> canFrom = {
12+
{4, 6}, // 0可以来自4,6
13+
{6, 8},
14+
{7, 9},
15+
{4, 8},
16+
{3, 9, 0},
17+
{},
18+
{1, 7, 0},
19+
{2, 6},
20+
{1, 3},
21+
{2, 4}
22+
};
23+
const int mod = 1e9 + 7;
24+
25+
class Solution {
26+
public:
27+
int knightDialer(int n) {
28+
int last[10], now[10];
29+
fill(last, last + 10, 1);
30+
for (int i = 2; i <= n; i++) {
31+
memset(now, 0, sizeof(now));
32+
for (int j = 0; j < 10; j++) {
33+
for (int from : canFrom[j]) {
34+
now[j] = (now[j] + last[from]) % mod;
35+
}
36+
}
37+
memcpy(last, now, sizeof(now));
38+
}
39+
// return accumulate(last, last + 10, 0); // WA,这里没取模
40+
int ans = 0;
41+
for (int i = 0; i < 10; i++) {
42+
ans = (ans + last[i]) % mod;
43+
}
44+
return ans;
45+
}
46+
};

Codes/0935-knight-dialer.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-10 15:29:56
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-10 15:39:22
6+
*/
7+
package main
8+
9+
var canFrom = [][]int{
10+
{4, 6}, // 0可以来自4,6
11+
{6, 8},
12+
{7, 9},
13+
{4, 8},
14+
{3, 9, 0},
15+
{},
16+
{1, 7, 0},
17+
{2, 6},
18+
{1, 3},
19+
{2, 4},
20+
};
21+
var mod = 1000000007
22+
23+
func knightDialer(n int) (ans int) {
24+
last := make([]int, 10)
25+
for i := range last {
26+
last[i] = 1
27+
}
28+
for i := 2; i <= n; i++ {
29+
now := make([]int, 10)
30+
for j := 0; j < 10; j++ {
31+
for _, from := range canFrom[j] {
32+
now[j] = (now[j] + last[from]) % mod
33+
}
34+
}
35+
last = now
36+
}
37+
for i := range last {
38+
ans = (ans + last[i]) % mod;
39+
}
40+
return
41+
}

Codes/0935-knight-dialer.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-10 15:17:45
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-10 15:29:23
6+
*/
7+
import java.util.Arrays;
8+
9+
class Solution {
10+
private final int[][] canFrom = {
11+
{4, 6}, // 0可以来自4,6
12+
{6, 8},
13+
{7, 9},
14+
{4, 8},
15+
{3, 9, 0},
16+
{},
17+
{1, 7, 0},
18+
{2, 6},
19+
{1, 3},
20+
{2, 4}
21+
};
22+
private final int mod = 1000000007;
23+
24+
public int knightDialer(int n) {
25+
int[] last = new int[10];
26+
int[] now = new int[10];
27+
Arrays.fill(last, 1);
28+
for (int i = 2; i <= n; i++) {
29+
for (int j = 0; j < 10; j++) {
30+
for (int from : canFrom[j]) {
31+
now[j] = (now[j] + last[from]) % mod;
32+
}
33+
}
34+
last = now;
35+
}
36+
int ans = 0;
37+
for (int i = 0; i < 10; i++) {
38+
ans = (ans + last[i]) % mod;
39+
}
40+
return ans;
41+
}
42+
}

Codes/0935-knight-dialer.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-12-10 15:14:37
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-12-10 15:17:05
6+
'''
7+
# AC,57.50%,56.76%
8+
CAN_FROM = [
9+
[4, 6],
10+
[6, 8],
11+
[7, 9],
12+
[4, 8],
13+
[3, 9, 0],
14+
[],
15+
[1, 7, 0],
16+
[2, 6],
17+
[1, 3],
18+
[2, 4]
19+
]
20+
MOD = 1_000_000_007
21+
22+
class Solution:
23+
def knightDialer(self, n: int) -> int:
24+
last = [1] * 10
25+
for _ in range(n - 1):
26+
now = [0] * 10
27+
for i in range(10):
28+
for j in CAN_FROM[i]:
29+
now[i] = (now[i] + last[j]) % MOD
30+
last = now
31+
return sum(last) % MOD

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@
309309
|0927.三等分|困难|<a href="https://leetcode.cn/problems/three-equal-parts/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/06/LeetCode%200927.%E4%B8%89%E7%AD%89%E5%88%86/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127189062" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/three-equal-parts/solution/letmefly-927san-deng-fen-by-tisfy-88qx/" target="_blank">LeetCode题解</a>|
310310
|0931.下降路径最小和|中等|<a href="https://leetcode.cn/problems/minimum-falling-path-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/13/LeetCode%200931.%E4%B8%8B%E9%99%8D%E8%B7%AF%E5%BE%84%E6%9C%80%E5%B0%8F%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131694030" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-falling-path-sum/solutions/2341854/letmefly-931xia-jiang-lu-jing-zui-xiao-h-ier1/" target="_blank">LeetCode题解</a>|
311311
|0934.最短的桥|中等|<a href="https://leetcode.cn/problems/shortest-bridge/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/25/LeetCode%200934.%E6%9C%80%E7%9F%AD%E7%9A%84%E6%A1%A5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127509391" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/shortest-bridge/solution/letmefly-934zui-duan-de-qiao-by-tisfy-dtew/" target="_blank">LeetCode题解</a>|
312+
|0935.骑士拨号器|中等|<a href="https://leetcode.cn/problems/knight-dialer/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/10/LeetCode%200935.%E9%AA%91%E5%A3%AB%E6%8B%A8%E5%8F%B7%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144375933" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/knight-dialer/solutions/3016246/letmefly-935qi-shi-bo-hao-qi-dong-tai-gu-bwmn/" target="_blank">LeetCode题解</a>|
312313
|0938.二叉搜索树的范围和|简单|<a href="https://leetcode.cn/problems/range-sum-of-bst/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/02/26/LeetCode%200938.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E8%8C%83%E5%9B%B4%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136306565" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/range-sum-of-bst/solutions/2654910/letmefly-938er-cha-sou-suo-shu-de-fan-we-fj3h/" target="_blank">LeetCode题解</a>|
313314
|0946.验证栈序列|中等|<a href="https://leetcode.cn/problems/validate-stack-sequences/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/31/LeetCode%200946.%E9%AA%8C%E8%AF%81%E6%A0%88%E5%BA%8F%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126616819" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/validate-stack-sequences/solution/letmefly-946yan-zheng-zhan-xu-lie-by-tis-st5n/" target="_blank">LeetCode题解</a>|
314315
|0952.按公因数计算最大组件大小|困难|<a href="https://leetcode.cn/problems/largest-component-size-by-common-factor/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/30/LeetCode%200952.%E6%8C%89%E5%85%AC%E5%9B%A0%E6%95%B0%E8%AE%A1%E7%AE%97%E6%9C%80%E5%A4%A7%E7%BB%84%E4%BB%B6%E5%A4%A7%E5%B0%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126069985" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/largest-component-size-by-common-factor/solution/letmefly-952an-gong-yin-shu-ji-suan-zui-4ljri/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)