-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path935 Knight Dialer.py
152 lines (116 loc) · 3.11 KB
/
935 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python3
"""
A chess knight can move as indicated in the chess diagram below:
This time, we place our chess knight on any numbered key of a phone pad
(indicated above), and the knight makes N-1 hops. Each hop must be from one key
to another numbered key.
Each time it lands on a key (including the initial placement of the knight), it
presses the number of that key, pressing N digits total.
How many distinct numbers can you dial in this manner?
Since the answer may be large, output the answer modulo 10^9 + 7.
Example 1:
Input: 1
Output: 10
Example 2:
Input: 2
Output: 20
Example 3:
Input: 3
Output: 46
Note:
1 <= N <= 5000
"""
MOD = 10 ** 9 + 7
dirs = [
(-2, 1),
(-1, 2),
(1, 2),
(2, 1),
(2, -1),
(1, -2),
(-1, -2),
(-2, -1),
]
nbrs = {
1: (6, 8),
2: (7, 9),
3: (4, 8),
4: (3, 9, 0),
5: tuple(),
6: (1, 7, 0),
7: (2, 6),
8: (1, 3),
9: (2, 4),
0: (4, 6),
}
from collections import defaultdict
class Solution:
def knightDialer(self, N: int) -> int:
"""
DP
F[pos][step] = sum(F[nbr][step+1] for all nbr)
"""
F = defaultdict(lambda: defaultdict(int))
for pos in range(10):
F[pos][N-1] = 1
for n in range(N-2, -1, -1):
for pos in range(10):
for nbr in nbrs[pos]:
F[pos][n] += F[nbr][n+1]
F[pos][n] %= MOD
ret = 0
for i in range(10):
ret += F[i][0]
ret %= MOD
return ret
class SolutionTLE2:
def __init__(self):
self.cache = {}
def knightDialer(self, N: int) -> int:
ret = 0
for i in range(10):
ret += self.dfs(i, N-1)
ret %= MOD
return ret
def dfs(self, i, r):
if (i, r) not in self.cache:
ret = 0
if r == 0:
ret = 1
else:
for nbr in nbrs[i]:
ret += self.dfs(nbr, r-1)
self.cache[i, r] = ret
return self.cache[i, r]
class SolutionTLE:
def __init__(self):
# row, col size
self.m = 4
self.n = 3
self.cache = {}
def knightDialer(self, N: int) -> int:
ret = 0
for i in range(self.m):
for j in range(self.n):
if (i, j) != (3, 0) and (i, j) != (3, 2):
ret += self.dfs(i, j, N-1)
ret %= MOD
return ret
def dfs(self, i, j, r):
if (i, j, r) not in self.cache:
ret = 0
if r == 0:
ret = 1
else:
for di, dj in dirs:
I = i + di
J = j + dj
if 0 <= I < self.m and 0 <= J < self.n and (I, J) != (3, 0) and (I, J) != (3, 2):
ret += self.dfs(I, J, r - 1)
ret %= MOD
self.cache[i, j, r] = ret
return self.cache[i, j, r]
if __name__ == "__main__":
assert Solution().knightDialer(1) == 10
assert Solution().knightDialer(2) == 20
assert Solution().knightDialer(3) == 46