Skip to content

Commit c23fee6

Browse files
authored
Create 935. Knight Dialer.py
1 parent 4099df5 commit c23fee6

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎leetcode3/935. Knight Dialer.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def knightDialer(self, n: int) -> int:
4+
MOD = 10**9 + 7
5+
candids = {
6+
1:{6, 8},
7+
2:{7,9},
8+
3:{4,8},
9+
4:{0,3,9},
10+
5:{},
11+
6:{0,1,7},
12+
7:{2,6},
13+
8:{1,3},
14+
9:{2,4},
15+
0:{4,6}
16+
}
17+
18+
length = 1
19+
curr = defaultdict(int)
20+
for i in range(10):
21+
curr[i] = 1
22+
while length != n:
23+
temp = defaultdict(int)
24+
for i in range(10):
25+
curr_val = curr[i]
26+
for next_num in candids[i]:
27+
temp[next_num] = (temp[next_num] + curr_val) % MOD
28+
length+=1
29+
curr = temp
30+
31+
ans = 0
32+
for _, count in curr.items():
33+
ans = (ans+count)%MOD
34+
return ans

0 commit comments

Comments
 (0)