We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2158819 commit f0e66f2Copy full SHA for f0e66f2
knight-dialer.py
@@ -0,0 +1,33 @@
1
+class Solution:
2
+ """
3
+ @param n: N
4
+ @return: return the number of distinct numbers can you dial in this manner mod 1e9+7
5
6
+ def knight_dialer(self, n: int) -> int:
7
+ #
8
+ '''
9
+ 可以缓存前序位置
10
+ 1. 到位置0,前一个位置可以是4或6;
11
+ 2. 到位置1,前一个位置可以是6或8;
12
+ ...
13
14
+ moves = [[4, 6],
15
+ [6, 8],
16
+ [7, 9],
17
+ [4, 8],
18
+ [0, 3, 9],
19
+ [],
20
+ [0, 1, 7],
21
+ [2, 6],
22
+ [1, 3],
23
+ [2, 4]]
24
+ dp = [1] * 10
25
+ for i in range(n - 1):
26
+ new_dp = [0] * 10
27
+ for num, count in enumerate(dp):
28
+ for m in moves[num]:
29
+ new_dp[m] += count
30
+ dp = new_dp
31
+ return sum(dp) % (10 ** 9 + 7)
32
+
33
+# medium: https://www.lintcode.com/problem/1707
0 commit comments