-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathknight-dialer.py
94 lines (87 loc) · 2.97 KB
/
knight-dialer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/83716573
class Solution:
def knightDialer(self, N):
"""
:type N: int
:rtype: int
"""
if N == 1: return 10
x1 = x2 = x3 = x4 = x5 = x6 = x7 = x8 = x9 = x0 = 1
MOD = 10 ** 9 + 7
for i in range(N - 1):
x1, x2, x3, x4, x5, x6, x7, x8, x9, x0 = (x6 + x8) % MOD,\
(x7 + x9) % MOD, (x4 + x8) % MOD, (x3 + x9 + x0) % MOD, 0, (x1 + x7 + x0) % MOD,\
(x2 + x6) % MOD, (x1 + x3) % MOD, (x2 + x4) % MOD, (x4 + x6) % MOD
return (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x0) % MOD
# V1'
# https://www.jiuzhang.com/solution/knight-dialer/#tag-highlight-lang-python
class Solution(object):
def knightDialer(self, N):
MOD = 10**9 + 7
moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],
[1,7,0],[2,6],[1,3],[2,4]]
dp = [1] * 10
for hops in range(N-1):
dp2 = [0] * 10
for node, count in enumerate(dp):
for nei in moves[node]:
dp2[nei] += count
dp2[nei] %= MOD
dp = dp2
return sum(dp) % MOD
# V2
# Time: O(logn)
# Space: O(1)
import itertools
class Solution(object):
def knightDialer(self, N):
"""
:type N: int
:rtype: int
"""
def matrix_expo(A, K):
result = [[int(i==j) for j in range(len(A))] \
for i in range(len(A))]
while K:
if K % 2:
result = matrix_mult(result, A)
A = matrix_mult(A, A)
K /= 2
return result
def matrix_mult(A, B):
ZB = zip(*B)
return [[sum(a*b for a, b in itertools.izip(row, col)) % M \
for col in ZB] for row in A]
M = 10**9 + 7
T = [[0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0]]
return sum(map(sum, matrix_expo(T, N-1))) % M
# Time: O(n)
# Space: O(1)
class Solution2(object):
def knightDialer(self, N):
"""
:type N: int
:rtype: int
"""
M = 10**9 + 7
moves = [[4, 6], [6, 8], [7, 9], [4, 8], [3, 9, 0], [],
[1, 7, 0], [2, 6], [1, 3], [2, 4]]
dp = [[1 for _ in range(10)] for _ in range(2)]
for i in range(N-1):
dp[(i+1) % 2] = [0] * 10
for j in range(10):
for nei in moves[j]:
dp[(i+1) % 2][nei] += dp[i % 2][j]
dp[(i+1) % 2][nei] %= M
return sum(dp[(N-1) % 2]) % M