-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path935.knight-dialer.py
61 lines (54 loc) · 1.77 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
#
# @lc app=leetcode id=935 lang=python3
#
# [935] Knight Dialer
#
# @lc code=start
# TAGS: Dynamic Programming
import sys
sys.setrecursionlimit(5010)
class Solution:
# 2800 ms, 24.18%. Time and Space: O(N). Recursion with memoization
def knightDialer(self, n: int) -> int:
future_moves = {1: [6, 8], 2: [7, 7], 0:[4, 4], 3: [4, 8], 4:[0, 9, 3], 6:[0, 1, 7], 7:[2, 6], 8:[1, 1], 9:[2, 4]}
MOD = 10**9 + 7
if n == 1: return 10
#@functools.lru_cache
memo = {}
def dfs(i, depth=1):
if (i, depth) in memo:
return memo[(i, depth)]
if depth == n:
return 1
rv = 0
for move in future_moves[i]:
rv += dfs(move, depth + 1)
memo[(i, depth)] = rv
return rv
ans = 0
for i in range(10):
if i in (3, 5, 6, 9): continue
elif i in (1, 4, 7):
ans += dfs(i)*2
else:# 2 8
ans += dfs(i)
return ans % MOD
# 832 ms, 87.51%. Time: O(N). Space: O(1). DP
# To get to this, think about the time complexity of memoization:
# [0, 1] [1, 1] [2, 1] ... [9, 1]
# [0, 2] [1, 2] [2, 2] ... [9, 2]
# ...
# [0, n] [1, n] [2, n] ... [9, n]
def knightDialer1(self, n: int) -> int:
future_moves = {1: [6, 8], 2: [7, 7], 0:[4, 4], 3: [4, 8], 4:[0, 9, 3], 6:[0, 1, 7], 7:[2, 6], 8:[1, 1], 9:[2, 4]}
MOD = 10**9 + 7
dp = [1]*10
for _ in range(1, n):
new_dp = [0]*10
for i in [0, 1, 2, 3, 4, 6, 7, 8, 9]:
for move in future_moves[i]:
new_dp[i] += dp[move]
new_dp[i] %= MOD
dp = new_dp
return sum(dp) % MOD
# @lc code=end