inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
el=[[] for i in range(10)]
el[0]=[4,6]
el[1]=[6,8]
el[2]=[7,9]
el[3]=[4,8]
el[4]=[3,9,0]
el[5]=[]
el[6]=[1,7,0]
el[7]=[2,6]
el[8]=[1,3]
el[9]=[2,4]
a=[[0 for i in range(10)] for j in range(n)]
for i in range(10):
a[0][i]=1
for i in range(1,n):
for j in range(10):
for x in el[j]:
a[i][j]=(a[i][j]+a[i-1][x])%(10**9+7)
c=0
for i in range(10):
c+=a[-1][i]
return c%(10**9+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
adj = {}
adj[0] = [4, 6]
adj[1] = [6, 8]
adj[2] = [7, 9]
adj[3] = [4, 8]
adj[4] = [0, 3, 9]
adj[5] = []
adj[6] = [0, 1, 7]
adj[7] = [2, 6]
adj[8] = [1, 3]
adj[9] = [2, 4]
p = 10**9 + 7
f = [None] * n
f[0] = [1] * 10
for i in range(1, n):
f[i] = [0] * 10
for v in range(0, 10):
for u in adj[v]:
f[i][v] += f[i-1][u]
total = 0
for d in range(0, 10):
total = (total % p + f[n-1][d] % p) % p
return total
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
possibilities = {2: {9, 7}, 1: {8, 6}, 3: {4, 8}, 4: {3, 9, 0}, 5: {}, 6: {1, 7, 0}, 7: {6, 2}, 8:{1,3}, 9:{4,2}, 0:{4,6}}
def dp(n):
result = {i:1 for i in range(10)}
for i in range(2, n+1):
temp = defaultdict(int)
for curr_position in result:
for next_position in possibilities[curr_position]:
temp[next_position] += result[curr_position]
result = temp
return sum(result.values()) % (pow(10, 9) + 7)
return dp(n)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
cur_state = [1 for _ in range(10)]
mod = 10 ** 9 + 7
edges = [(1, 6), (1, 8), (2, 7), (2, 9), (3, 4), (3, 8), (4, 9), (4, 0), (6, 7), (6, 0)]
edge_dic = { i: [] for i in range(10) }
for i, j in edges:
edge_dic[i].append(j)
edge_dic[j].append(i)
for _ in range(1, n):
new_state = [0 for _ in range(10)]
for i in range(10):
new_state[i] = sum([cur_state[j] for j in edge_dic[i]]) % mod
cur_state = new_state
return sum(cur_state) % mod
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
d = [[4, 6], [6, 8], [7, 9], [4, 8], [0, 3, 9], [], [0, 1, 7], [2, 6], [1, 3], [2, 4]]
dp = [[1] * 10]
dp += [[0] * 10 for i in range(n-1)]
for i in range(n-1):
for j in range(10):
for k in d[j]:
dp[i+1][k] = (dp[i+1][k] + dp[i][j]) % 1000000007
return sum(dp[-1]) % 1000000007
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
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 # dp[i, N] = dp[nei, N-1] + dp[nei1, N-1] => dp[i] =
for i_n in range(n-1):
dp2 = [0] * 10
for i in range(10):
for nei in moves[i]:
dp2[nei] += dp[i]
dp2[nei] %= MOD
dp = list(dp2)
return sum(dp) % MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
MOD = int(1e9 + 7)
adj = [
[4, 6],
[6, 8],
[7, 9],
[4, 8],
[3, 9, 0],
[],
[0, 1, 7],
[2, 6],
[1, 3],
[2, 4],
]
dp = [[0] * 10 for _ in range(n + 1)]
dp[1] = [1] * 10
for i in range(2, n + 1):
for j in range(10):
for child in adj[j]:
dp[i][j] += dp[i - 1][child]
dp[i][j] %= MOD
ans = 0
for v in dp[n]:
ans += v
ans %= MOD
return ans
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
from collections import Counter
class Solution:
def knightDialer(self, n: int) -> int:
moves = {1 : [6, 8],
2 : [7, 9],
3 : [4, 8],
4 : [3, 9, 0],
6 : [1, 7, 0],
7 : [2, 6],
8 : [1, 3],
9 : [2, 4],
0 : [4, 6],
}
if n == 1:
return 10
starts = dict([(i, 1) for i in moves])
for _ in range(n - 1):
next = dict([(i, 0) for i in moves])
for i in starts:
for j in moves[i]:
next[j] += starts[i]
starts = next
return sum(starts.values()) % (10 ** 9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
if n == 1:
return 10
# [# of 0, # of 4/6, # of 2/8, # of corners]
c = [1,2,2,4]
nx = [0,0,0,0]
for i in range(n-1):
nx[0] = c[1]
nx[1] = 2*c[0] + c[3]
nx[2] = c[3]
nx[3] = 2*(c[1] + c[2])
c = nx[:]
return sum(c) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
moves = [[4, 6], [6, 8], [9, 7], [8, 4], [3, 9, 0], [], [7, 1, 0], [6, 2], [1, 3], [2, 4]]
count = [[0] * 10 for _ in range(n + 1)]
for i in range(10):
count[1][i] = 1
for i in range(1, n + 1):
for num in range(10):
for nei in moves[num]:
count[i][num] += count[i - 1][nei]
count[i][num] %= 10 ** 9 + 7
return sum(count[-1]) % (10 ** 9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
neighbors = {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)
}
def knightDialer(self, n: int) -> int:
if n == 0:
return 0
if n == 1:
return 10
prev_moves = [1] * 10
current_moves = [0] * 10
for i in range(n - 1):
for j in range(10):
current_moves[j] = sum(prev_moves[k] for k in self.neighbors[j]) % (10**9 + 7)
prev_moves = current_moves[:]
return sum(prev_moves) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
neighbors = {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)
}
def knightDialer(self, n: int) -> int:
if n == 0:
return 0
if n == 1:
return 10
prev_moves = [1] * 10
current_moves = [0] * 10
for i in range(n - 1):
for j in range(10):
current_moves[j] = sum(prev_moves[k] for k in self.neighbors[j]) % (10**9 + 7)
prev_moves = current_moves.copy()
return sum(prev_moves) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
MODULE = 10 ** 9 + 7
class Solution:
def knightDialer(self, n: int) -> int:
if n == 1:
return 10
MOVEMENTS = {
0: [4, 6],
1: [6, 8],
2: [7, 9],
3: [4, 8],
4: [3, 9, 0],
5: [],
6: [1, 7, 0],
7: [2, 6],
8: [1, 3],
9: [2, 4]
}
dp = [2, 2, 2, 2, 3, 0, 3, 2, 2, 2]
for idx in range(2, n):
new_dp = [0] * 10
for jdx in range(10):
for next_jdx in MOVEMENTS[jdx]:
new_dp[next_jdx] += dp[jdx]
dp = new_dp
print(dp)
return sum(dp) % MODULE
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
adj = [
[4, 6],
[8, 6],
[7, 9],
[4, 8],
[3, 9, 0],
[],
[1, 7, 0],
[2, 6],
[1, 3],
[2, 4]
]
m = 10 ** 9 + 7
memo = [1] * 10
for i in range(1, n):
memo = [sum(memo[e] for e in adj[d]) % m for d in range(10)]
return sum(memo) % m
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
mod = 10**9+7
moves = {1:[6,8], 2:[7,9], 3:[4,8],
4: [0,3,9], 5:[], 6:[0,1,7],
7:[2,6], 8:[1,3], 9:[2,4], 0:[4,6]}
curr = [1]*10
for _ in range(n-1):
nex = [0]*10
for i in range(10):
nex[i] = sum(curr[j] for j in moves[i])%mod
curr = nex
return sum(curr)%mod
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
MOD = 10 ** 9 + 7
res = 0
mappings = {
1: [6, 8],
2: [7, 9],
3: [4, 8],
4: [0, 3, 9],
5: [],
6: [0, 1, 7],
7: [2, 6],
8: [1, 3],
9: [2, 4],
0: [4, 6],
}
dp = [[0 for i in range(10)] for i in range(n+1)]
dp[0] = [1 for i in range(10)]
dp[1] = [len(mappings[i]) for i in range(10)]
for i in range(1, n-1):
for j in range(10):
pos = mappings[j]
# dp[i + 1][j] += dp[i-1][j] * dp[1][j]
for k in pos:
dp[i + 1][k] += dp[i][j]
# print(dp)
return sum(dp[n - 1]) % MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
'''
use DP instead of BFS, becasue only finite many states
2D DP:
DP[l][s] = number of unique path of length l, starting with state s
DP[l][1] = DP[l-1][6] + DP[l-1][8], becasue 1 can move to 6, 8
'''
class Solution:
def knightDialer(self, n: int) -> int:
MOD = 10**9 + 7
movedict = {0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[3,9,0],
5:[], 6:[1,7,0], 7:[2,6], 8:[1,3], 9:[2,4]}
DP = [[0]*10 for _ in range(n)]
DP[0] = [1]*10
for l in range(1, n):
for s in range(10):
for moveto in movedict[s]:
DP[l][s] += DP[l-1][moveto]
DP[l][s] %= MOD
ans = sum(DP[-1]) % MOD
return ans
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
nextMove = [(4,6), (6,8), (7,9), (4,8), (0,3,9), (),(0,1,7), (2,6), (1,3), (2,4)]
dp = [[0]*10 for _ in range(n)]
if n ==1: return 10
MOD = 10**9 + 7
for i in range(10):
dp[0][i] = 1
for r in range(1,n):
for i in range(10):
for m in nextMove[i]:
# print(r,i,m)
dp[r][m] = dp[r][m] + dp[r-1][i]
# dp[r][m] %= MOD
return sum(dp[-1]) % MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution(object):
def knightDialer(self, N):
# Use This Code
moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],
[1,7,0],[2,6],[1,3],[2,4]]
MOD = 10**9 + 7
table = [1] * 10
for _ in range(N-1):
temp = [0] * 10
for k in range(10):
temp[k] = sum(table[i] for i in moves[k])
temp[k] %= MOD
table = temp
return sum(table)%MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
if n == 1:
return 10
# [# of 0, # of 4/6, # of 2/8, # of corners]
a,b,c,d = [1,2,2,4]
for i in range(n-1):
a,b,c,d = [b, 2*a+d, d, 2*(b+c)]
return sum([a,b,c,d]) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
graph = {
0: [4, 6],
1: [6,8],
2: [7,9],
3: [4,8],
4:[9, 3, 0],
5: [],
6: [7, 1, 0],
7: [6, 2],
8: [1, 3],
9: [2,4]
}
prev_dp = [1] * 10
for i in range(n-1):
dp = [0] * 10
for j in range(10):
dp[j] = sum(prev_dp[k] for k in graph[j]) % (10**9 + 7)
prev_dp = dp
return sum(prev_dp) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
MOD = 10**9 + 7
#dp[i][j] possible answers at step[i], number j
dp =[[0 for _ in range(10)] for _ in range(n)]
#possible next moves
move = [[4,6], [6,8], [7,9], [4,8], [3,9,0], [], [1,7,0], [2,6], [1,3], [4,2]]
#initialization at n = 0, dp[0][j] = 1
dp[0] = [1 for _ in range(10)]
#transition funciton
for i in range(1, n):
for j in range(10):
for nei in move[j]:
dp[i][j] = (dp[i][j] + dp[i-1][nei])%MOD
#final answer
return sum(dp[-1])%MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution():
def knightDialer(self, n):
max_val = 10**9 + 7
mem = {(1,1):1,
(1,2):1,
(1,3):1,
(1,4):1,
(1,5):1,
(1,6):1,
(1,7):1,
(1,8):1,
(1,9):1,
(1,0):1,
}
fields = {0:(4,6),
1:(6,8),
2:(7,9),
3:(4,8),
4:(3,9,0),
5:(),
6:(0,1,7),
7:(2,6),
8:(3,1),
9:(2,4),
}
new_mem = {}
for length in range(1,n):
for num in range(10):
possible_jumps = fields[num]
possible_paths = 0
for jump in possible_jumps:
possible_paths = (possible_paths + mem[(length, jump)]) % max_val
new_mem[(length+1, num)] = possible_paths
mem = new_mem
new_mem = {}
result = 0
for key in list(mem.keys()):
result = (result + mem[key]) % max_val
return result % max_val
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
neighbors = {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)
}
def knightDialer(self, n: int) -> int:
if n == 0:
return 0
if n == 1:
return 10
prev_moves = [1] * 10
for i in range(n - 1):
current_moves = [sum(prev_moves[k] for k in self.neighbors[j]) % (10**9 + 7) for j in range(10)]
prev_moves = current_moves
return sum(prev_moves) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
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 = [[0] * 10 for _ in range(n + 1)]
for i in range(10):
dp[1][i] = 1
for i in range(2, n + 1):
for j in range(10):
for move in moves[j]:
dp[i][move] += dp[i - 1][j]
dp[i][move] %= MOD
return sum(dp[n]) % MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
mod = 10**9 + 7
d = dict()
d[0] = {4, 6}
d[1] = {6, 8}
d[2] = {7, 9}
d[3] = {4, 8}
d[4] = {0, 3, 9}
d[6] = {0, 1, 7}
d[7] = {2, 6}
d[8] = {1, 3}
d[9] = {2, 4}
memo = [[0]*10 for i in range(n+1)]
memo[1] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
# print(memo)
for i in range(2, n+1):
for key, val in list(d.items()):
for v in val:
memo[i][key] += memo[i-1][v]
# print(memo[-3:-1])
return sum(memo[-1])%mod
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, N: int) -> int:
#OPT(i, j) = number of numbers you can dial with j hops reaching i
#k - possible places I could have come from
#OPT(i, j) = SUM(OPT(k, j-1))
#base cases
#if j = 0, then 0
#if i = 5, j = j, then OPT(i, j) = 0
jumps = dict({1:[8,6], 2:[7,9], 3:[4,8], 4:[0,3,9], 5:[], 6:[0,1,7], 7:[2,6], 8:[1,3],9:[2,4], 0:[4,6]})
table = [[0] * N for i in range(10)]
for j in range(N):
for i in range(10):
if j == 0:
table[i][0] = 1
elif j == 1:
table[i][j] = len(jumps[i])
else:
kList = jumps[i]
for k in kList:
table[i][j] += table[k][j-1]
res = 0
for i in range(len(table)):
res = (res +table[i][-1])%(10**9 + 7)
return res
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
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 = [[0 for _ in range(n+1)] for _ in range(10)] #dp[i][j] --> f(i, j) start from i and take j steps
for i in range(10):
dp[i][1] = 1
for j in range(2,n+1):
for i in range(10):
for move in moves[i]:
dp[i][j] += dp[move][j-1]
dp[i][j] %= MOD
sum_ = 0
for i in range(10):
sum_ += dp[i][n]
sum_ %= MOD
return sum_
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
d = {
0:(4, 6),
1:(6, 8),
2:(7, 9),
3:(4, 8),
4:(3, 9, 0),
5:(),
6:(1, 7, 0),
7:(2, 6),
8:(1, 3),
9: (2, 4)
}
dp = [1] * 10
for _ in range(n-1):
temp = [0] * 10
for i in range(10):
for j in d[i]:
temp[j] = dp[i] + temp[j]
dp = temp
return sum(dp) % (10 ** 9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
# class Solution:
# def knightDialer(self, N: int) -> int:
# steps = {1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0],5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6]}
# ret = set()
# def DFS(cur, i):
# if i == N:
# n = 0
# for c in cur:
# n = n * 10 +c
# ret.add(n)
# print(n)
# return
# for n in steps[cur[-1]]:
# DFS(cur+[n], i+1)
# return
# for k in steps.keys():
# DFS([k], 1)
# return len(ret)
class Solution:
def knightDialer(self, N: int) -> int:
steps = {1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6]}
dp = [1 for _ in range(10)]
for _ in range(N-1):
cur = [0 for _ in range(10)]
for i in range(10):
for j in steps[i]:
cur[j] += dp[i]
dp = cur
return sum(dp) % (10**9 +7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
d = [(4,6),(6,8),(7,9),(4,8),(3,9,0),(),(1,7,0),(2,6),(1,3),(2,4)] # Create a graph, index is the vertex and values are the edges
cache = {}
def dfs(self, i, n):
if n == 0: return 1
elif (i, n) not in self.cache:
self.cache[i, n] = sum(self.dfs(val, n-1) for val in self.d[i]) # Memoization
if i in [1,4,7]: self.cache[i+2, n] = self.cache[i, n] # Symmetry
elif i in [3,6,9]: self.cache[i-2, n] = self.cache[i, n]
return self.cache[i, n]
def knightDialer(self, n: int) -> int:
return sum(self.dfs(i, n-1) for i in range(10)) % (int(1e9)+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
if n == 1:
return 10
mod = 10**9 + 7
dp = [[0 for i in range(n+1)] for j in range(10)]
for i in range(10):
dp[i][1] = 1
d = {0:[4, 6], 1:[6, 8], 2:[7, 9], 3:[4, 8], 4:[0, 3, 9], 6:[0, 1, 7], 7:[2, 6], 8:[1, 3], 9:[2, 4]}
for j in range(2, n+1):
for i in range(10):
if i != 5:
for ele in d[i]:
dp[i][j] += dp[ele][j-1]
dp[i][j] %= mod
# print(dp)
ans = 0
for i in range(10):
ans += dp[i][n]
ans %= mod
return ans
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
dp = [[0 for _ in range(10)] for _ in range(n+1)]
prevs = {0: [4, 6], 1: [8, 6], 2: [7, 9], 3: [8, 4], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [6, 2], 8: [1, 3], 9: [4, 2]}
for i in range(10):
dp[1][i] = 1
for i in range(2, n+1):
for j in range(10):
for prev in prevs[j]:
dp[i][j] += dp[i-1][prev]
return sum(dp[-1]) % 1000000007
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
if n==1:
return 10
numpad = {'0':['4','6'],'1':['6','8'],'2':['7','9'],'3':['4','8'],'4':['0','3','9'],'6':['0','1','7'],'7':['2','6'],'8':['1','3'],'9':['4','2']}
dp={}
for i in ['0','1','2','3','4','6','7','8','9']:
dp[i]=[1,0]
for i in range(1,n):
for j in ['0','1','2','3','4','6','7','8','9']:
dp[j][i%2]=0
for k in numpad[j]:
dp[j][i%2]+=dp[k][1-i%2]
dp[j][i%2]%=(1000000007)
res=0
for i in ['0','1','2','3','4','6','7','8','9']:
res+=dp[i][(n-1)%2]
res%=(1000000007)
return res
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
dp = [ [1]*10 for _ in range(n+1) ]
d = {
0:(4,6),
1:(6,8),
2:(7,9),
3:(8,4),
4:(3,9,0),
5:(),
6:(1,7,0),
7:(6,2),
8:(1,3),
9:(2,4)
}
for i in range(2, n+1):
for j in range(0,10):
dp[i][j] = 0
for k in d[j]:
dp[i][j] += dp[i-1][k]
return sum(dp[n]) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
if n==0:
return 0
dic = {0: (4, 6),
1: (8, 6),
2: (7, 9),
3: (4, 8),
4: (3, 9, 0),
5: (),
6: (1, 7, 0),
7: (2, 6),
8: (1, 3),
9: (2, 4)}
dp = [1]*10
for _ in range(n-1):
new = [0]*10
for i in range(10):
for nei in dic[i]:
new[i] += dp[nei]
dp = new
return sum(dp)%(10**9+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
arr = [[0 for i in range(10)] for i in range(n)]
mappings = {
0: [4,6],
1: [6,8],
2: [7,9],
3: [4,8],
4: [0,3,9],
5: [],
6: [0,1,7],
7: [2,6],
8: [1,3],
9: [2,4]
}
mod = 10**9+7
arr[0] = [1 for i in range(10)]
for i in range(1,n):
for j in range(10):
for k in mappings[j]:
arr[i][j] += arr[i-1][k]
return sum(arr[n-1])%mod
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
N=n
l=10
dp=[1]*l
m={1:[6,8],2:[7,9],3:[4,8],4:[0,3,9],5:[],6:[0,1,7],7:[2,6],8:[1,3],9:[2,4],0:[4,6]}
for _ in range(2,N+1):
dp2=[0]*l
for i in range(l):
for j in m[i]:
dp2[i]+=dp[j]
dp=dp2
return sum(dp)%(10**9+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
nextMove = [(4,6), (6,8), (7,9), (4,8), (0,3,9), (),(0,1,7), (2,6), (1,3), (2,4)]
dp = [[0]*10 for _ in range(n)]
if n ==1: return 10
MOD = 10**9 + 7
for i in range(10):
dp[0][i] = 1
for r in range(1,n):
for i in range(10):
for m in nextMove[i]:
# print(r,i,m)
dp[r][m] = dp[r][m] + dp[r-1][i]
dp[r][m] %= MOD
return sum(dp[-1]) % MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
next_move = {0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4]}
memo = defaultdict(int)
for i in range(10):
memo[i] = 1
for _ in range(n-1):
next_memo = defaultdict(int)
for i in range(10):
for next_step in next_move[i]:
next_memo[next_step] += memo[i]
next_memo[next_step] %= 10**9 + 7
memo = next_memo
return sum(memo.values()) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
MOVES = [
[4, 6], # 0
[6, 8], # 1
[7, 9], # 2
[4, 8], # 3
[3, 9, 0], # 4
[], # 5
[1, 7, 0], # 6
[2, 6], # 7
[1, 3], # 8
[2, 4], # 9
]
modulus = 10**9 + 7
class Solution:
def knightDialer(self, n: int) -> int:
dp = [1] * 10 # dp[i] = number of ways to get to digit in n moves, for n = 1, 2, ...
dp2 = [0] * 10 # second copy of dp2 to avoid mutating dp while iterating over it
for _ in range(1, n):
for digit in range(10):
dp2[digit] = 0
for next_digit in MOVES[digit]:
dp2[digit] += dp[next_digit]
dp2[digit] %= modulus
dp2, dp = dp, dp2 # swap out current dp
result = 0
for ways in dp:
result += ways
result %= modulus
return result
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
d = [(4,6),(6,8),(7,9),(4,8),(3,9,0),(),(1,7,0),(2,6),(1,3),(2,4)]
cache = {}
def dfs(self, i, n):
if n == 0: return 1
if (i, n) not in self.cache:
self.cache[i, n] = sum(self.dfs(val, n-1) for val in self.d[i])
if i in [1,4,7]: self.cache[i+2, n] = self.cache[i, n]
elif i in [3,6,9]: self.cache[i-2, n] = self.cache[i, n]
return self.cache[i, n]
def knightDialer(self, n: int) -> int:
mod = int(1e9)+7
return sum(self.dfs(i, n-1) for i in range(10)) % mod
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
### f[i][j]: # of different codes with length i and with j as the last number
### f[i][j] = sum(f[i-1][l]), where l can be achieved from j on the map
### f[1][j] = 1, 0<=j<=9
achievable = {}
achievable[0] = [ 4,6 ]
achievable[1] = [ 6,8 ]
achievable[2] = [ 7,9 ]
achievable[3] = [ 4,8 ]
achievable[4] = [ 0,3,9 ]
achievable[5] = [ ]
achievable[6] = [ 0,1,7 ]
achievable[7] = [ 2,6 ]
achievable[8] = [ 1,3 ]
achievable[9] = [ 2,4 ]
f1 = [1 for _ in range(10)]
m = 10 ** 9 + 7
for i in range(1, n):
f2 = [0 for _ in range(10)]
for j in range(10):
f2[j] = sum( f1[l] for l in achievable[j]) % m
f1 = f2.copy()
return sum(f1) % m
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
moves = {0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9],
5:[], 6:[0,1,7], 7:[2,6], 8:[1,3], 9:[2,4]}
mem = [[0 for i in range(n)] for i in range(10)]
for i in range(10):
mem[i][0] = 1
for col in range(1,n):
for row in range(10):
for move in moves[row]:
mem[row][col] += mem[move][col-1]
mem[row][col] %= (10**9) + 7
tot_sum = 0
for row in range(10):
tot_sum += mem[row][n-1]
tot_sum %= (10**9) + 7
return tot_sum
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
d = {
0: [4,6],
1: [6,8],
2: [9,7],
3: [4,8],
4: [3,9,0],
5: [],
6: [1,7,0],
7: [6,2],
8: [3,1],
9: [4,2]
}
res = 0
dp = [1]*10
for x in range(1,n):
temp = [0]*10
for i in range(10):
for j in d[i]:
temp[j] += dp[i]
dp = temp
for i in dp:
res += i
return res % ((10**9)+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
tab = [[0 for i in range(n + 1)] for i in range(10)]
for i in range(10):
tab[i][1] = 1
for i in range(2, n + 1):
for j in range(10):
if j == 0:
tab[j][i] = tab[4][i - 1] + tab[6][i - 1]
if j == 1:
tab[j][i] = tab[6][i - 1] + tab[8][i - 1]
if j == 2:
tab[j][i] = tab[7][i - 1] + tab[9][i - 1]
if j == 3:
tab[j][i] = tab[4][i - 1] + tab[8][i - 1]
if j == 4:
tab[j][i] = tab[3][i - 1] + tab[9][i - 1] + tab[0][i - 1]
if j == 5:
tab[j][i] = 0
if j == 6:
tab[j][i] = tab[1][i - 1] + tab[7][i - 1] + tab[0][i - 1]
if j == 7:
tab[j][i] = tab[2][i - 1] + tab[6][i - 1]
if j == 8:
tab[j][i] = tab[1][i - 1] + tab[3][i - 1]
if j == 9:
tab[j][i] = tab[2][i - 1] + tab[4][i - 1]
tab[j][i] = tab[j][i] % (10**9 + 7)
s = 0
for i in range(10):
s += tab[i][n]
s = s % (10**9 + 7)
return s
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
dp=[[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[0,1,7],[2,6],[1,3],[4,2]]
ct=[1]*10
while n-1:
ct=[sum(ct[j] for j in i) for i in dp]
n-=1
return sum(ct)%(10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
dic = {0:(4,6),1:(6,8),2:(7,9),3:(4,8),4:(0,3,9),5:(),6:(1,0,7),7:(2,6),8:(1,3),9:(2,4)}
prev_dic = {}
for i in range(10):
prev_dic[i] = 1
for i in range(n-1):
cur_dic = collections.defaultdict(int)
for j in prev_dic:
for k in dic[j]:
cur_dic[k] += prev_dic[j]
prev_dic = cur_dic
return sum(prev_dic.values())%(10**9+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
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 cell in range(n - 1):
dp2 = [0] * 10
for num_cell, count in enumerate(dp):
for knight in moves[num_cell]:
dp2[knight] += count
dp2[knight] %= MOD
dp = dp2
return sum(dp) % MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
moves = {1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6]}
# dp[i][j] = x
# x possible calls of length i when knight is placed on j
dp = [[0 for j in range(10)] for i in range(n)]
for i in range(n):
for j in range(10):
if i == 0:
dp[i][j] = 1
else:
for move in moves[j]:
dp[i][j] += dp[i-1][move]
return sum(dp[n-1]) % (10 ** 9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
d = [(4,6),(6,8),(7,9),(4,8),(3,9,0),(),(1,7,0),(2,6),(1,3),(2,4)] # Create a graph, index is the vertex and values are the edges
cache = {}
def dfs(self, i, n):
if n == 0: return 1
elif (i, n) not in self.cache:
self.cache[i, n] = sum(self.dfs(val, n-1) for val in self.d[i]) # Memoization
if i in [1,4,7]: self.cache[i+2, n] = self.cache[i, n] # Symmetry
elif i in [3,6,9]: self.cache[i-2, n] = self.cache[i, n]
return self.cache[i, n]
def knightDialer(self, n: int) -> int:
return sum(self.dfs(i, n-1) for i in range(10)) % (int(1e9)+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
arr = [[0 for i in range(10)] for i in range(n)]
mappings = {
0: [4,6],
1: [6,8],
2: [7,9],
3: [4,8],
4: [0,3,9],
5: [],
6: [0,1,7],
7: [2,6],
8: [1,3],
9: [2,4]
}
mod = 10**9+7
arr[0] = [1 for i in range(10)]
for i in range(1,n):
for j in range(10):
for k in mappings[j]:
arr[i][j] = (arr[i][j]%mod + arr[i-1][k]%mod)%mod
return sum(arr[n-1])%mod
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
import numpy as np
class Solution:
def knightDialer(self, n: int) -> int:
memo, dp = [[4,6],[8,6],[7,9],[4,8],[3,9,0],[],[0,1,7],[2,6],[1,3],[2,4]], [1] * 10
for i in range(n - 1):
dp = [sum([dp[j] for j in memo[i]]) for i in range(10)]
return sum(dp) % (10 ** 9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
#recursive + memo
# next_map = {0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[0,3,9],5:[],6:[0,1,7],7:[2,6],8:[1,3],9:[2,4]}
# def dfs(cur_num,n):
# if (cur_num,n) in memo.keys():
# return memo[(cur_num,n)]
# if n == 0:
# return 1
# ans = 0
# for next_num in next_map[cur_num]:
# ans += dfs(next_num,n-1)
# memo[(cur_num,n)] = ans
# return ans
# ans = 0
# MOD = pow(10,9)+7
# memo = {}
# for i in range(10):
# ans += dfs(i,n-1)%MOD
# ans = ans%MOD
# # print (\"i:{}, ans={}\".format(i,ans))
# return ans
#dp[numr][n]: represent the total combinations when start with number :num with n steps
dp = [ [0 for _ in range(n+1)] for _ in range(10)]
next_map = [[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[0,1,7],[2,6],[1,3],[2,4]]
MOD = pow(10,9)+7
for i in range(10):
dp[i][1] = 1
for k in range(2,n+1):
for i in range(10):
for next_num in next_map[i]:
dp[i][k] += dp[next_num][k-1]%MOD
dp[i][k] %= MOD
ans = 0
for i in range(10):
ans += dp[i][n]
ans %= MOD
return ans
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
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 = [[0]*10 for _ in range(n)]
for i in range(n):
for j in range(10):
dp[i][j] = 0
for i in range(10):
dp[0][i] = 1
for t in range(1, n):
for num in range(10):
for move in moves[num]:
dp[t][num] += dp[t - 1][move]
dp[t][num] %= MOD
return sum(dp[n-1])%MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, k: int) -> int:
self.dirs = [
[1, 2], [1, -2], [-1, 2], [-1, -2],
[2, 1], [2, -1], [-2, 1], [-2, -1]
]
self.mod = 10 ** 9 + 7
self.n = 4
self.m = 3
if k == 1:
return 10
self.forbidden = set([(3, 0), (3, 2)])
self.next_pos = self.build_pos()
# print(self.next_pos)
self.dp = [1] * 10
self.helper(k - 1)
return sum(self.dp) % self.mod
def helper(self, k):
if k == 0:
return
# print(self.dp)
next_dp = [0] * 10
for i in range(10):
for j in self.next_pos[i]:
next_dp[j] += (self.dp[i] % self.mod)
next_dp[j] %= self.mod
# print(next_dp)
self.dp = next_dp
self.helper(k - 1)
def build_pos(self):
res = {}
for i in range(10):
next_num = []
x, y = self.get_pos(i)
for d in self.dirs:
nx, ny = d
if self.is_valid(x + nx, y + ny):
num = self.get_num(x + nx, y + ny)
# print(i, x + nx, y + ny, num)
next_num.append(num)
res[i] = next_num
return res
def is_valid(self, x, y):
return 0 <= x < self.n and 0 <= y < self.m and (x, y) not in self.forbidden
def get_num(self,x, y):
if x == 3 and y == 1:
return 0
if self.is_valid(x, y):
return x * 3 + y + 1
def get_pos(self, num):
if num == 0:
return (3, 1)
num -= 1
row = num // 3
col = num % 3
return (row, col)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
'''
0->[4,6]->[1,3,7,9]->
2 20 (3, 6), (2, 14)
'''
if n == 1: return 10
m = {
1: [6, 8],
2: [7, 9],
3: [4, 8],
4: [0, 3, 9],
5: [],
6: [0, 1, 7],
7: [2, 6],
8: [1, 3],
9: [2, 4],
0: [4, 6]
}
cnt = defaultdict(int)
for k in m:
for v in m[k]:
cnt[v] += 1
for _ in range(n-2):
cnt2 = defaultdict(int)
for k in cnt:
for v in m[k]:
cnt2[v] += cnt[k]
cnt = cnt2
return sum(cnt[k] for k in cnt) % (10**9+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, N: int) -> int:
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
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
dp = [[1] * 10 for _ in range(n)]
moves = {0: [4, 6],
1: [6, 8],
2: [7, 9],
3: [4, 8],
4: [0, 3, 9],
5: [],
6: [0, 1, 7],
7: [2, 6],
8: [1, 3],
9: [2, 4]}
for i in range(1, n):
for j in range(10):
count = 0
for digit in moves[j]:
count += dp[i-1][digit]
dp[i][j] = count
return sum(dp[-1]) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
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 _ in range(n - 1):
prevDp = copy.copy(dp)
for i in range(10):
dp[i] = sum([prevDp[move] for move in moves[i]])
return sum(dp) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
if n == 1:
return 10
LARGEINT = 10**9 + 7
moves = {1:2, 2:1, 4:2, 7:2, 8:1, 0:1}
while n > 1:
new_moves = {
1: (moves[4] + (2*moves[8])) % LARGEINT,
2: moves[7],
4: (moves[1] + moves[7] + (2*moves[0])) % LARGEINT,
7: (moves[4] + (2*moves[2])) % LARGEINT,
8: moves[1],
0: moves[4]
}
moves = new_moves
n -= 1
return sum(moves.values()) % LARGEINT
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
neighbors = {
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)
}
dp = [[0 for _ in range(10)] for _ in range(n+1)]
for r in range(n+1):
for c in range(10):
if r == 0:
dp[r][c] = 0
elif r == 1:
dp[r][c] = 1
else:
for ne in neighbors[c]:
dp[r][c] += dp[r-1][ne]
return sum(dp[-1]) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
d = {1:[6,8],
2:[7,9],
3:[4,8],
4:[3,9,0],
5:[],
6:[1,7,0],
7:[2,6],
8:[1,3],
9:[2,4],
0:[4,6]}
@lru_cache(None)
def dfs(i, n):
if n == 1:
return 1
else:
res = 0
for c in d[i]:
res += dfs(c, n-1)
return res % (10**9+7)
return sum(dfs(i, n) for i in range(10)) % (10**9+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
if n == 1: return 10
MOD = 1000000007
graph = {1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6]}
prev = [1,1,1,1,1,0,1,1,1,1]
for _ in range(n-1):
val = [0] * 10
for i in range(10):
for x in graph[i]:
val[x] += prev[i]
prev = val
return sum(prev) % MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def _knightDialer(self, n: int, curr_n: int, moves_map: dict, cache: dict) -> int:
if n == 0:
return 1
if (curr_n,n) in cache:
return cache[(curr_n,n)]
distinct_nums = 0
for next_n in moves_map[curr_n]:
distinct_nums += self._knightDialer(n-1, next_n, moves_map, cache)
cache[(curr_n, n)] = distinct_nums
return cache[(curr_n,n)]
def knightDialer(self, n: int) -> int:
# TLE, let's take care of some symmetry
# 1 == 3 == 9 == 7, 4 == 6, 2 == 8, 0
if n == 1:
return 10
mod = 10 ** 9 + 7
moves_map = {
1: [6,8],
2: [7,9],
3: [4,8],
4: [0,3,9],
5: [],
6: [0,1,7],
7: [2,6],
8: [1,3],
9: [2,4],
0: [4,6],
}
# Bottom-up
dp_curr = [1] * 10
dp_next = [0] * 10
for _ in range(1, n):
for start, poss_moves in list(moves_map.items()):
dp_next[start] = sum(dp_curr[poss_move] for poss_move in poss_moves)
dp_curr = dp_next
dp_next = [0] * 10
return sum(dp_curr) % mod
# nums = 0
# for m, weight in [(1,4),(2,2),(4,2),(0,1)]:
# nums += weight * self._knightDialer(n-1, m, moves_map, cache)
# return nums % mod
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
k = {
0: [4, 6],
1: [6, 8],
2: [7, 9],
3: [4, 8],
4: [0, 3, 9],
5: [],
6: [0, 1, 7],
7: [2, 6],
8: [1, 3],
9: [2, 4],
}
dp = [[1] * 10, [0] * 10]
turn = 0
for t in range(n-1):
turn = 1 - turn
for i in range(10):
dp[turn][i] = sum(dp[1-turn][j] for j in k[i]) % (10 ** 9 + 7)
return sum(dp[turn]) % (10 ** 9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
adjacency = {
0: {4, 6},
1: {8, 6},
2: {7, 9},
3: {8, 4},
4: {3, 9, 0},
5: {},
6: {1, 7, 0},
7: {2, 6},
8: {1, 3},
9: {2, 4}
}
def knightDialer(self, n: int):
count = [1 for _ in range(10)]
for _ in range(n - 1):
count = [sum(count[next_digit] for next_digit in Solution.adjacency[digit]) for digit in range(10)]
return sum(count) % (10 ** 9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
next_move = { 0: [4,6], 1: [6,8], 2: [7,9], 3: [4,8], 4: [0,3,9], 5: [], 6: [0,1,7], 7:[2,6], 8: [1,3], 9: [2,4]}
mod_num = 10**9 + 7
dp_prev = [1] * 10
for i in range(n-1):
dp_new = [0] * 10
for num in range(10):
phones_for_num = sum((dp_prev[x] for x in next_move[num]))
dp_new[num] = phones_for_num
dp_prev = dp_new
return sum(dp_prev) % mod_num
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
# class Solution:
# def knightDialer(self, n: int) -> int:
# possible_moves = {1:[6,8], 2:[7,9], 3:[4,8], 4:[3,9,0], 5:[],
# 6:[1,7,0], 7:[2,6], 8:[1,3], 9:[2,4], 0:[4,6]}
# self.memo = {}
# def dfs(cur_point, count, n):
# if count == n:
# return 1
# if (cur_point, count) not in self.memo:
# self.memo[(cur_point, count)] = sum(dfs(nxt_point, count + 1, n) for nxt_point in possible_moves[cur_point])
# if cur_point in [1,4,7]:
# self.memo[(cur_point+2, count)] = self.memo[(cur_point, count)]
# elif cur_point in [3,6,9]:
# self.memo[(cur_point-2, count)] = self.memo[(cur_point, count)]
# return self.memo[(cur_point, count)]
# return sum(dfs(i, 1, n) for i in range(10)) % (10**9 + 7)
class Solution:
d = [(4,6),(6,8),(7,9),(4,8),(3,9,0),(),(1,7,0),(2,6),(1,3),(2,4)] # Create a graph, index is the vertex and values are the edges
cache = {}
def dfs(self, i, n):
if n == 0: return 1
elif (i, n) not in self.cache:
self.cache[i, n] = sum(self.dfs(val, n-1) for val in self.d[i]) # Memoization
if i in [1,4,7]: self.cache[i+2, n] = self.cache[i, n] # Symmetry
elif i in [3,6,9]: self.cache[i-2, n] = self.cache[i, n]
return self.cache[i, n]
def knightDialer(self, n: int) -> int:
return sum(self.dfs(i, n-1) for i in range(10)) % (int(1e9)+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
neighbors = {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)
}
def knightDialer(self, n: int) -> int:
if n == 0:
return 0
if n == 1:
return 10
prev_moves = [1] * 10
for i in range(n - 1):
current_moves = [sum(prev_moves[k] for k in self.neighbors[j]) for j in range(10)]
prev_moves = current_moves
return sum(prev_moves) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def __init__(self):
self.mem = dict()
def knightDialer(self, n: int) -> int:
reach = [ [4,6], [6,8], [7,9], [4,8], [0,3,9], [], [0,1,7], [2,6], [1,3], [2,4] ]
counts = [1] * 10
for i in range(n-1):
counts2 = [ sum(counts[k] for k in reach[j] ) for j in range(10) ]
# print(i,counts2)
counts = counts2
return sum(counts) % (10**9 + 7)
# def doit(start, hopsLeft):
# key = (start,hopsLeft)
# if key in self.mem:
# return self.mem[key]
# if hopsLeft == 0:
# count = 1
# else:
# count = sum(doit(z,hopsLeft-1) for z in reach[start])
# self.mem[key] = count
# return count
# return sum(doit(z,n-1) for z in range(10)) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
jumps = [[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[0,1,7],[2,6],[1,3],[2,4]]
mod = 10**9 + 7
if n == 1:
return 10
curr_total = []
last_step_totals = [1 for _ in range(10)]
for i in range(n - 1):
curr_total = [0 for _ in range(10)]
for i in range(10):
for jump_location in jumps[i]:
curr_total[i] += last_step_totals[jump_location]
last_step_totals = curr_total
return sum(curr_total) % mod
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
#recursive + memo
# next_map = {0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[0,3,9],5:[],6:[0,1,7],7:[2,6],8:[1,3],9:[2,4]}
# def dfs(cur_num,n):
# if (cur_num,n) in memo.keys():
# return memo[(cur_num,n)]
# if n == 0:
# return 1
# ans = 0
# for next_num in next_map[cur_num]:
# ans += dfs(next_num,n-1)
# memo[(cur_num,n)] = ans
# return ans
# ans = 0
# MOD = pow(10,9)+7
# memo = {}
# for i in range(10):
# ans += dfs(i,n-1)%MOD
# ans = ans%MOD
# # print (\"i:{}, ans={}\".format(i,ans))
# return ans
#dp[numr][n]: represent the total combinations when start with number :num with n steps
dp = [ [0 for _ in range(n+1)] for _ in range(10)]
next_map = {0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[0,3,9],5:[],6:[0,1,7],7:[2,6],8:[1,3],9:[2,4]}
MOD = pow(10,9)+7
for i in range(10):
dp[i][1] = 1
for k in range(2,n+1):
for i in range(10):
for next_num in next_map[i]:
dp[i][k] += dp[next_num][k-1]%MOD
dp[i][k] = dp[i][k]%MOD
ans = 0
for i in range(10):
ans += dp[i][n]
ans = ans%MOD
return ans
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
move_map = {
0: {4,6},
1: {6,8},
2: {7,9},
3: {4,8},
4: {3, 9, 0},
5: {},
6: {1, 0, 7},
7: {2, 6},
8: {1, 3},
9: {2, 4}
}
class Solution:
def knightDialer(self, n: int) -> int:
table = {}
for key in move_map:
table[key, 1] = 1
for N in range(2,n+1):
for key in move_map:
val = 0
for new_key in move_map[key]:
val += table[new_key, N-1]
table[key, N] = val
res = 0
for key in move_map:
res += table[key, n]
return res % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
# dp(0, digit) = 0
# dp(n steps, digit) = sum(dp(n - 1, f) for f in digit's friends)
if n == 0:
return 0
friends = [0] * 10
friends[0] = [4, 6]
friends[1] = [6, 8]
friends[2] = [7, 9]
friends[3] = [8, 4]
friends[4] = [0, 3, 9]
friends[5] = []
friends[6] = [0, 1, 7]
friends[7] = [2, 6]
friends[8] = [1, 3]
friends[9] = [2, 4]
dp = [[0 for _ in range(10)] for _ in range(n + 1)]
dp[1] = [1] * 10
for steps in range(2, n + 1):
for digit in range(10):
dp[steps][digit] = sum([dp[steps - 1][f] for f in friends[digit]]) % (10**9 + 7)
return sum(dp[-1]) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
next_move = { 0: [4,6], 1: [6,8], 2: [7,9], 3: [4,8], 4: [0,3,9], 5: [], 6: [0,1,7], 7:[2,6], 8: [1,3], 9: [2,4]}
mod_num = 10**9 + 7
dp_prev = [1] * 10
for i in range(n-1):
dp_new = [0] * 10
for num in range(10):
phones_for_num = sum((dp_prev[x] for x in next_move[num])) % mod_num
dp_new[num] = phones_for_num
dp_prev = dp_new
return sum(dp_prev) % mod_num
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
if n == 1:
return 10
maps = {
0: {4, 6},
1: {6, 8},
2: {7, 9},
3: {4, 8},
4: {3, 9, 0},
5: {},
6: {1, 7, 0},
7: {2, 6},
8: {1, 3},
9: {4, 2}
}
prev = [1]*10
for i in range(1, n):
curr = [0]*10
for j in range(0, 10):
curr[j] = sum(prev[k] for k in maps[j])
prev = curr
return sum(prev)%(10**9+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
dp=[[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[0,1,7],[2,6],[1,3],[4,2]]
ct=[1]*10
while n-1:
tmp=[]
for i in range(10):
tmp+=[sum(ct[j] for j in dp[i])]
ct=tmp
n-=1
return sum(ct)%(10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
possible = {
-1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
0: [4, 6],
1: [6, 8],
2: [7, 9],
3: [4, 8],
4: [0, 3, 9],
5: [],
6: [0, 1, 7],
7: [2, 6],
8: [1, 3],
9: [2, 4]
}
cache = {}
class Solution:
def knightDialer(self, n: int) -> int:
total = self.helper(-1, n, 0) % 1000000007
return total
def helper(self, num, n, total):
if (num, n) in cache:
total += cache[(num, n)]
return total
nxt = possible[num]
total_begin = total
if n == 1:
total += len(nxt)
else:
for num_next in nxt:
total = self.helper(num_next, n-1, total) % 1000000007
cache[(num, n)] = total - total_begin
return total
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
from collections import deque
class Solution:
cache = {}
d = [(4,6),(6,8),(7,9),(4,8),(3,9,0),(),(1,7,0),(2,6),(1,3),(2,4)]
def dfs(self, i, n):
if n == 0: return 1
elif (i, n) not in self.cache:
self.cache[i, n] = sum(self.dfs(val, n-1) for val in self.d[i]) # Memoization
if i in [1,4,7]: self.cache[i+2, n] = self.cache[i, n] # Symmetry
elif i in [3,6,9]: self.cache[i-2, n] = self.cache[i, n]
return self.cache[i, n]
def knightDialer(self, n: int) -> int:
return sum(self.dfs(i, n-1) for i in range(10)) % (int(1e9)+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
d = {
0: [4,6],
1: [6,8],
2: [9,7],
3: [4,8],
4: [3,9,0],
5: [],
6: [1,7,0],
7: [6,2],
8: [3,1],
9: [4,2]
}
res = 0
dp = [1]*10
for x in range(1,n):
temp = [0]*10
for i in range(10):
for j in d[i]:
temp[j] += dp[i]
dp = temp
res = sum(dp) % ((10**9)+7)
return res
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
moveable = {
1: [6, 8],
2: [7, 9],
3: [4, 8],
4: [0, 3, 9],
5: [],
6: [0, 1, 7],
7: [2, 6],
8: [1, 3],
9: [2, 4],
0: [4, 6]
}
MOD = 7 + 1e9
@lru_cache(maxsize=None)
def helper(step, num=-1):
if step == n:
return 1
result = 0
if num == -1:
for i in range(10):
result = (result + helper(step + 1, i)) % MOD
else:
for move in moveable[num]:
result = (result + helper(step + 1, move)) % MOD
return result
return int(helper(0))
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
d = {1:[6,8],2:[7,9],3:[8,4],4:[0,3,9],5:[],6:[0,1,7],7:[2,6],8:[1,3],9:[2,4],0:[4,6]}
f = [[0]*10 for i in range(2)]
f[1&1] = [1]*10
for i in range(2,n+1):
f[i&1] = [0]*10
for j in range(10):
for v in d[j]:
f[i&1][v] += f[(i-1)&1][j] % (10**9+7)
# print(f[i&1])
return sum(f[n&1]) % (10**9+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
# recursive backtracking:
# you have 4 options:
# (2 right, 1 down)
# (2 right, 1 up)
# (2 left, 1 down)
# (2 left, 1 up)
# we have a grid, 4 by 3
if n == 1:
return 10
# grid = [
# [1, 2, 3],
# [4, 5, 6],
# [7, 8, 9],
# [-1,0,-1]
# ]
# # append the number after reaching node
# # we could do dynamic programming and remember how many we can reach if
# # we are at a specific n
# # at 0,0:
# # we can create a graph of things we can connect,
# # ie how many things can we reach from where we are right now
# 0 - 9 are the connections
maps = {
0: {4, 6},
1: {6, 8},
2: {7, 9},
3: {4, 8},
4: {3, 9, 0},
5: {},
6: {1, 7, 0},
7: {2, 6},
8: {1, 3},
9: {4, 2}
}
prev = [1]*10
for i in range(1, n):
curr = [0]*10
for j in range(0, 10):
curr[j] = sum(prev[k] for k in maps[j])
prev = curr
return sum(prev)%(10**9+7)
'''
this index is the sum of all previous possibilities
we see when we reach at index 1, there are 2 places we could have came from
we have n total moves and 9 total numbers
we start at number 1, number 1 is connected to 6 and 8
now we go to 6 and 8,
6 connected to 1, 0, 7
8 connected to 1, 3
can we some how sum for each n?
opt(i, j) = sum(opt(i-1, j) for j in connected )
return the sum of the top part of the array
n=2 [1= arr[i-1][6]+arr[i-1][[8] ... ]]
n=1 [1:1, 2:1, 3:1 ... 0:1]
'''
# count = 0
# numbers = set()
# def recursion(x, y, number, depth):
# nonlocal count, numbers
# if depth > n:
# return
# if not(0<= x < 4 and 0<=y<3 and grid[x][y]!=-1):
# return
# if depth == n:
# numbers.add(number)
# return
# else:
# number+=grid[x][y]*(10**depth)
# recursion(x+2, y-1, number, depth+1)
# recursion(x+2, y+1, number, depth+1)
# recursion(x-2, y-1, number, depth+1)
# recursion(x-2, y+1, number, depth+1)
# recursion(x+1, y+2, number, depth+1)
# recursion(x+1, y-2, number, depth+1)
# recursion(x-1, y+2, number, depth+1)
# recursion(x-1, y-2, number, depth+1)
# return
# for x in range(0, 4):
# for y in range(0, 3):
# recursion(x, y, 0, 0)
# print(numbers)
# return len(numbers)%(10**9+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
possibilities = {
1: [6,8],
2: [7,9],
3: [8,4],
4: [9,3,0],
5: [],
6: [1,7,0],
7: [6,2],
8: [1,3],
9: [2,4],
0: [4,6],
}
class Solution:
def knightDialer(self, N: int) -> int:
if N == 1:
return 10
counter = 0
num_ways = [1,1,1,1,1,0,1,1,1,1]
for i in range(N-1):
updated = [0]*10
for digit in range(10):
updated[digit] = sum([num_ways[previous] % int(10e8+7) for previous in possibilities[digit]])
num_ways = updated
return sum(num_ways) % int(10e8+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, N: int) -> int:
res = [1] * 10 # 每个位置出现的次数
next_move = [[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[0,1,7],[2,6],[1,3],[2,4]]
i = 1
while i < N:
res2 = [0] * 10
for k, v in enumerate(res):
for j in next_move[k]: # 对于跳到的下一个位置,更新出现的次数
res2[j] += v
res = res2 # 更新结果
i += 1
return sum(res) % (10**9 +7) # 不要忘记取余
#https://www.jianshu.com/p/caa2c35321a7
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knight(self, n, current, memo):
knight_mp = {1: {8, 6},
2: {9, 7},
3: {4, 8},
4: {3, 9, 0}, # 0 also
5: {},
6: {7, 1, 0}, # 0 also
7: {2, 6},
8: {3, 1},
9: {2, 4},
0: {6, 4}}
if n==1:
return 1
if (current, n) in memo:
return memo[(current, n)]
res = 0
for item in knight_mp.get(current, set()):
res+=self.knight(n-1, item, memo)
memo[(current, n)] = res
return res
def knightDialer(self, n: int) -> int:
# res = 0
# mod = 10**9+7
# memo = {}
# for i in range(0, 10):
# res+=self.knight(n, i, memo)%mod
# return res%mod
knight_mp = {1: {8, 6},
2: {9, 7},
3: {4, 8},
4: {3, 9, 0}, # 0 also
5: {},
6: {7, 1, 0}, # 0 also
7: {2, 6},
8: {3, 1},
9: {2, 4},
0: {6, 4}}
dp = [[0 if i!=0 else 1 for _ in range(0, 10)] for i in range(n)]
mod = 10**9+7
for step in range(1, n):
for cell in range(0, 10):
for neighbor in knight_mp.get(cell, set()):
dp[step][cell] += dp[step-1][neighbor]
return sum(dp[n-1])%mod
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, N: int) -> int:
possible_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(1, N):
dp1 = [0 for _ in range(10)]
for i in range(10):
for m in possible_moves[i]:
dp1[i] += dp[m]
dp = dp1
return sum(dp) % (pow(10, 9) + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
if n == 1: return 10
move_dict = {0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9],
5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4]}
MOD = 10**9+7
# dp[i] suggests how many ways we can move to i on the phone pad
dp = [2, 2, 2, 2, 3, 0, 3, 2, 2, 2]
# the first two moves are done
for i in range(n-2):
new_dp = [2, 2, 2, 2, 3, 0, 3, 2, 2, 2]
for d in range(10):
new_dp[d] = sum(dp[move] for move in move_dict[d]) % MOD
dp = new_dp
return sum(dp) % MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
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 i, c in enumerate(dp):
for nei in moves[i]:
dp2[i] += dp[nei]
dp2[i] %= MOD
dp = dp2
return sum(dp) % MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
# DP 每次要加上那个格的DP值
#{4, 6}, {6, 8}, {7, 9}, {4, 8}, {3, 9, 0}, {}, {1, 7, 0}, {2, 6}, {1, 9}, {4, 2}
# 二维数组 表示第i次跳到j上有多少种方法
# 比如到4的时候 那么可能是从3,9,0 来的 那么i,4 = i,4 + i-1 (3,9,0)
stack = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],[1,7,0],[2,6],[1,9],[4,2]]
dp = [[0 for _ in range(10)] for __ in range(n)]
for i in range(10): # 随便放 所有按键都初始为1 因为n是跳的次数 放的不算
dp[0][i] = 1
for i in range(1,n):
for j in range(10):
for k in stack[j]:
dp[i][j] = (dp[i][j] + dp[i-1][k]) % (10**9 + 7)
return sum(dp[-1][i] for i in range(10)) % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def initMoveTable(self):
moveTable = dict()
moveTable[0] = [4, 6]
moveTable[1] = [6, 8]
moveTable[2] = [7, 9]
moveTable[3] = [4, 8]
moveTable[4] = [0, 3, 9]
moveTable[5] = []
moveTable[6] = [0, 1, 7]
moveTable[7] = [2, 6]
moveTable[8] = [1, 3]
moveTable[9] = [2, 4]
return moveTable
def knightDialer(self, n: int) -> int:
if n == 0:
return 0
if n == 1:
return 10
moveTable = self.initMoveTable()
dp = []
for i in range(n+1):
newRow = []
for j in range(10):
newRow.append(None)
dp.append(newRow)
for i in range(10):
dp[1][i] = 1
for i in range(2, n+1):
for j in range(10):
localNumWays = 0
moves = moveTable[j]
for move in moves:
localNumWays += dp[i-1][move]
dp[i][j] = localNumWays % (pow(10,9) + 7)
return sum(dp[i]) % (pow(10,9) + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
main_dic = {}
def __init__(self):
dic = {}
for i in range(4):
for j in range(3):
if i==3 and (j==0 or j==2): continue
dic[(i,j)] = self.get_ind(i,j)
self.main_dic = dic
def get_ind(self,i,j):
lst = []
lst.append((i+2,j-1))
if not self.check_ind(lst[-1]): lst.pop()
lst.append((i+2,j+1))
if not self.check_ind(lst[-1]): lst.pop()
lst.append((i-2,j-1))
if not self.check_ind(lst[-1]): lst.pop()
lst.append((i-2,j+1))
if not self.check_ind(lst[-1]): lst.pop()
lst.append((i+1,j+2))
if not self.check_ind(lst[-1]): lst.pop()
lst.append((i-1,j+2))
if not self.check_ind(lst[-1]): lst.pop()
lst.append((i+1,j-2))
if not self.check_ind(lst[-1]): lst.pop()
lst.append((i-1,j-2))
if not self.check_ind(lst[-1]): lst.pop()
return lst
def check_ind(self,t):
i,j = t
if i<0 or j<0: return False
if i>3 or j>2: return False
if i==3 and (j==0 or j==2): return False
return True
def knightDialer(self, n: int) -> int:
if n==1: return 10;
old = [[1]*3 for i in range(4)]
old[3][0],old[3][2]=0,0
for k in range(n-1):
new = []
for i in range(4):
temp = []
for j in range(3):
if i==3 and (j==0 or j==2):
temp.append(0)
continue
lst,res = self.main_dic[(i,j)],0
for m,n in lst:
res+=old[m][n]
temp.append(res)
new.append(temp)
old = new
res = sum([sum(l) for l in old])
return res%(10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, N: int) -> int:
hop_map = {1:[6,8],
2:[7,9],
3:[4,8],
4:[0,3,9],
6:[0,1,7],
7:[2,6],
8:[1,3],
9:[2,4],
0:[4,6]}
memo = {(i,1): 1 for i in range(10)}
def num_hops(key:int, N:int):
if N == 1:
return 1
if key in [1,4,7]:
key += 2
elif key == 5:
return 0
if (key, N) in memo:
return memo[(key, N)]
sum_ = 0
for v in hop_map[key]:
sum_ += num_hops(v, N-1)
memo[(key, N)] = sum_ % (10**9 + 7)
return sum_
sum_ = 0
for i in range(10):
sum_ += num_hops(i, N) % (10**9 + 7)
return sum_ % (10**9 + 7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
from collections import deque
MOD = 10 ** 9 + 7
class Solution:
def knightDialer(self, n: int) -> int:
m = {
0: [4, 6],
1: [6, 8],
2: [7, 9],
3: [4, 8],
4: [3, 9, 0],
5: [],
6: [1, 7, 0],
7: [2, 6],
8: [1, 3],
9: [2, 4]
}
curt = {i: 1 for i in range(0, 10)}
n -= 1
while n > 0:
next_set = {}
for key in curt:
for nei in m[key]:
next_set[nei] = (next_set.get(nei, 0) + curt[key]) % MOD
curt = next_set
n -= 1
return sum(curt.values()) % MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 = 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
mod = pow(10, 9) + 7
for i in range(n - 1):
t0 = (x4 + x6) % mod
t1 = (x6 + x8) % mod
t2 = (x7 + x9) % mod
t3 = (x4 + x8) % mod
t4 = (x0 + x3 + x9) % mod
t5 = 0
t6 = (x0 + x1 + x7) % mod
t7 = (x2 + x6) % mod
t8 = (x1 + x3) % mod
t9 = (x2 + x4) % mod
x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 = t0, t1, t2, t3, t4, t5, t6, t7, t8, t9
return (x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9) % mod
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
dct={0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[0,3,9],5:[],6:[0,1,7],7:[2,6],8:[1,3],9:[2,4]}
def func(k):
if k==1:
return {i:1 for i in range(10)}
res=func(k-1)
counts={i:0 for i in range(10)}
for i in range(10):
for t in dct[i]:
counts[t]+=res[i]
return counts
res=func(n)
return sum(res.values())%(10**9+7)
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
from functools import lru_cache
class Solution:
def knightDialer(self, n: int) -> int:
@lru_cache(None)
def jumps(start, n):
if n == 0:
return 1
ans = 0
for neighbor in neighbors[start]:
ans = (ans + jumps(neighbor, n-1)) % MOD
return ans % MOD
neighbors = {
1: [6,8],
2: [7,9],
3: [4,8],
4: [0,3,9],
5: [],
6: [0,1,7],
7: [2,6],
8: [1,3],
9: [2,4],
0: [4,6]
}
MOD = pow(10, 9) + 7
res = 0
for i in range(10):
res = (res + jumps(i, n-1)) % MOD
return res
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
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 = [[None]*10 for _ in range(n)]
for i in range(10):
dp[0][i] = 1
def helper(node, jumpsLeft):
if dp[jumpsLeft][node]:
return dp[jumpsLeft][node]
cnt = 0
for nextJump in moves[node]:
cnt += helper(nextJump, jumpsLeft - 1)
cnt %= MOD
dp[jumpsLeft][node] = cnt
return cnt
for i in range(10):
helper(i, n-1)
return sum(dp[n-1]) % MOD
|
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n, return how many distinct phone numbers of length n we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 109 + 7.
Example 1:
Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
Example 2:
Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
Example 3:
Input: n = 3
Output: 46
Example 4:
Input: n = 4
Output: 104
Example 5:
Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.
Constraints:
1 <= n <= 5000
|
class Solution:
def knightDialer(self, n: int) -> int:
mod = 10 ** 9 + 7
nei = {0: [4, 6,], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4]}
@lru_cache(None)
def dfs(i, pre):
if i == n:
return 1
cand = nei[pre] if pre != None else range(10)
ans = 0
for k in cand:
ans += dfs(i + 1, k) % mod
return ans % mod
return dfs(0, None)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.