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:
parent_child_dict = {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)]
for j in range(10):
dp[0][j] = 1
for i in range(N - 1):
for j in range(10):
children = parent_child_dict[j]
for child in children:
dp[i + 1][child] += dp[i][j]
dp[i][j] = dp[i][j] % (10 ** 9 + 7)
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:
def knightDialer(self, n: int) -> int:
# board = [[str(j) for j in (1 + 3*i, 3*i + 2, 3*i + 3)] for i in range(3)]
# board.append(['*', '0', '#'])
# direct = [(-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1)]
# dp = {}
# def dfs(i, j, step):
# if i < 0 or i > 3 or j < 0 or j > 2: return 0
# if board[i][j] in '*#': return 0
# if step == 1: return 1
# key = (board[i][j], step)
# if key in dp: return dp[key]
# dest = set([(i + dr, j + dc) for dr, dc in direct])
# dp[key] = sum([dfs(r, c, step - 1) for r, c in dest])
# return dp[key]
# res = 0
# for i in range(10):
# res += dfs(str(i), n)
# return res%(10**9 + 7)
dp = {}
def nextStep(k):
if k == 1: return [6, 8]
if k == 2: return [7, 9]
if k == 3: return [4, 8]
if k == 4: return [9, 3, 0]
if k == 6: return [7,1,0]
if k == 7: return [6,2]
if k == 8: return [1,3]
if k == 9: return [4,2]
if k == 0: return [4, 6]
def dfs(k, step):
if k == 5 and step > 1: return 0
if step == 1: return 1
key = (k, step)
if key in dp: return dp[key]
dp[key] = sum([dfs(e, step - 1) for e in nextStep(k)])
return dp[key]
res = 0
for i in range(10):
# if i ==5:continue
res += dfs(i, 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 = {i:1 for i in range(10)}
mod = 10**9+7
for _ in range(1, n):
dp2 = {}
dp2[0] = (dp[4]+dp[6])%mod
dp2[1] = (dp[6]+dp[8])%mod
dp2[2] = (dp[7]+dp[9])%mod
dp2[3] = (dp[4]+dp[8])%mod
dp2[4] = (dp[0]+dp[3]+dp[9])%mod
dp2[5] = 0
dp2[6] = (dp[0]+dp[1]+dp[7])%mod
dp2[7] = (dp[2]+dp[6])%mod
dp2[8] = (dp[1]+dp[3])%mod
dp2[9] = (dp[2]+dp[4])%mod
dp = dp2
return sum(dp.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
|
NEIGHBORS_MAP = {
1: (6, 8),
2: (7, 9),
3: (4, 8),
4: (3, 9, 0),
5: tuple(), # 5 has no neighbors
6: (1, 7, 0),
7: (2, 6),
8: (1, 3),
9: (2, 4),
0: (4, 6),
}
def neighbors(position):
return NEIGHBORS_MAP[position]
class Solution:
def knightDialer(self, num_hops):
cache = {}
mod = 10**9 + 7
def helper(position, num_hops):
if (position, num_hops) in cache:
return cache[(position, num_hops)]
if num_hops == 0 or num_hops == 1:
return 1
num_sequences = 0
for neighbor in neighbors(position):
num_sequences += helper(neighbor, num_hops - 1)
cache[(position, num_hops)] = num_sequences
# print(cache)
return num_sequences
res = 0
for i in range(0, 10):
res += helper(i, num_hops)
# print(res, cache)
return res % 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
dirs = [(-2,-1),(-2,1),(-1,-2),(-1,2),(1,-2),(1,2),(2,-1),(2,1)]
dp = [[1]*3 for _ in range(4)]
dp[3][0] = 0
dp[3][2] = 0
for k in range(1,n):
temp = [[0]*3 for _ in range(4)]
for i in range(4):
for j in range(3):
if i == 3 and j != 1: continue
for x, y in dirs:
nx = i+x
ny = j+y
if nx<0 or nx>=4 or ny<0 or ny>=3: continue
temp[i][j] = (temp[i][j] + dp[nx][ny])%Mod
dp = temp[:]
print(dp)
ans = 0
for i in range(4):
for j in range(3):
ans = (ans + dp[i][j])%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:
modulo = pow(10,9) + 7
def get_position(pos):
_map= {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: [4,2],
}
return _map[pos]
cache = {}
def solve(pos, step):
key = (pos, step)
if key not in cache:
if step>=n:
return 1
res = 0
for i in get_position(pos):
res += solve(i, step + 1)
res = res % modulo
cache[key] = res
return cache[key]
res = 0
for i in range(10):
res += solve(i, 1)
return res % modulo
|
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
self.mod = 1e9 + 7
self.cache = {}
self.knight_steps = {
1: [6, 8],
2: [7, 9],
3: [4, 8],
4: [0, 3, 9],
5: [],
6: [1, 7, 0],
7: [2, 6],
8: [1, 3],
9: [2, 4],
0: [4, 6]
}
count = 0
for i in range(10):
count += self.recursive_calc(i, n - 1)
count = count % self.mod
# print(self.cache)
return int(count % self.mod)
def recursive_calc(self, current_number, steps):
if steps == 0: return 1
if (current_number, steps) in self.cache:
return self.cache[(current_number, steps)]
total = 0
for i in self.knight_steps[current_number]:
total += self.recursive_calc(i, steps - 1)
total = total % self.mod
self.cache[(current_number, steps)] = total
return self.cache[(current_number, steps)]
|
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:
# https://blog.csdn.net/fuxuemingzhu/article/details/83716573
self.ans = dict()
self.ans[0] = 10
board = [[1] * 3 for _ in range(4)]
board[3][0] = board[3][2] = 0
pre_dict = {(i, j) : self.prevMove(i, j) for i in range(4) for j in range(3)}
for n in range(1, n):
new_board = copy.deepcopy(board)
for i in range(4):
for j in range(3):
cur_move = 0
for x, y in pre_dict[(i, j)]:
cur_move = (cur_move + board[x][y]) % (10 ** 9 + 7)
new_board[i][j] = cur_move
board = new_board
return sum([board[i][j] for i in range(4) for j in range(3)]) % (10 ** 9 + 7)
def prevMove(self, i, j):
if (i, j) == (3, 0) or (i, j) == (3, 2):
return []
directions = [(-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1)]
res = []
for d in directions:
x, y = i + d[0], j + d[1]
if 0 <= x < 4 and 0 <= y < 3 and (x, y) != (3, 0) and (x, y) != (3, 2):
res.append((x, y))
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
|
from functools import lru_cache
class Solution:
def knightDialer(self, N: int) -> int:
@lru_cache(None)
def _jump(row, col, remaining):
if remaining == 1:
return 1
total = 0
for dr, dc in (2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2):
if (
0 <= row + dr < m and 0 <= col + dc < n and pad[row + dr][col + dc]
):
total += _jump(row + dr, col + dc, remaining - 1)
return total % MAX
MAX = 10**9 + 7
pad = [
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[0, 1, 0]
]
m, n = len(pad), len(pad[0])
total = 0
for i in range(m):
for j in range(n):
if pad[i][j]:
total += _jump(i, j, N)
return total % MAX
|
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:
total = 0
places = {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
dp = [1]*10
for i in range(2, N+1):
tmp_dp = [0]*10
for i in range(0, 10):
if i != 5:
neighbors = places.get(i)
curr = 0
for n in neighbors:
curr += dp[n]
tmp_dp[i] = curr
dp = copy.deepcopy(tmp_dp)
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 __init__(self):
self.memory = {}
def knightDialer(self, n: int) -> int:
steps = {
0: [4,6],
1: [6,8],
2: [7,9],
3: [4,8],
4: [0,9,3],
5: [],
6: [7,1,0],
7: [2,6],
8: [1,3],
9: [2,4]
}
def kDialer(i, n, steps,c):
if n==1:
return 1
if n==0:
return 0
if (i, n) in self.memory:
return self.memory[(i, n)]
self.memory[(i, n)] = 0
for j in steps[i]:
self.memory[(i, n)]+= kDialer(j, n-1, steps,c)%c
self.memory[(i, n)] = self.memory[(i, n)]%c
return self.memory[(i, n)]
combinations = 0
for i in range(0, 10):
combinations+=kDialer(i, n, steps, (10**9) + 7)%((10**9) + 7)
return combinations%((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,0):1, (0,1):1, (0,2):1,
(1,0):1, (1,1):1, (1,2):1,
(2,0):1, (2,1):1, (2,2):1,
(3,1):1}
for x in range(1, n):
temp = {}
for r in range(4):
for c in range(3):
if (r == 3 and c == 0) or (r==3 and c == 2):
continue
sm = 0
for i in (1,-1):
for j in (2, -2):
sm = sm + dp.get((r+i, c+j), 0) + dp.get((r+j, c+i), 0)
temp[(r,c)] = sm
dp = temp
return sum(dp.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 reduce
def make_grid():
g = [[1] * 3 for _ in range(4)]
g[3][0] = None
g[3][2] = None
return g
MOVES = [
(2, 1),
(2, -1),
(1, 2),
(1, -2),
(-1, 2),
(-1, -2),
(-2, 1),
(-2, -1),
]
# def jump(grid):
# for i in range(4):
# for j in range(3):
# if grid[i][j] is not None:
# for di, dj in MOVES:
# ni = i+di
# nj = j+dj
# if (0 <= ni < 4) and (0 <= nj < 3) and (grid[ni][nj] is not None):
# grid[ni][nj] += 1
def jump(grid):
new_grid = make_grid()
for i in range(4):
for j in range(3):
if grid[i][j] is not None:
c = 0
for di, dj in MOVES:
ni = i+di
nj = j+dj
if (0 <= ni < 4) and (0 <= nj < 3) and (grid[ni][nj] is not None):
#grid[ni][nj] += 1
c += grid[ni][nj]
new_grid[i][j] = c
return new_grid
class Solution:
def knightDialer(self, n: int) -> int:
grid = make_grid()
m = 10**9 + 7
c = 0
for _ in range(n-1):
grid = jump(grid)
c += 10
print(grid)
return sum([sum([c for c in row if c]) for row in grid]) % m#- c
#for _ in range(n):
# jump(grid)
#
#return reduce(list.__add__, [[c for c in row] for row in grid])
|
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:
limit = 10**9+7
# print(limit)
direction = [(1,2),(-1,2),(1,-2),(-1,-2),(2,-1),(2,1),(-2,1),(-2,-1)]
dp = [[1 for i in range(3)] for j in range(4)]
dp[3][0],dp[3][2] = 0,0
for i in range(n-1):
temp = [[0 for i in range(3)] for j in range(4)]
for i in range(4):
for j in range(3):
if i ==3 and j !=1:
continue
for di,dj in direction:
new_i,new_j = di+i,dj+j
if 0<=new_i<4 and 0 <= new_j <3:
temp[i][j] = (temp[i][j] + dp[new_i][new_j])%limit
dp = temp[:]
res = 0
for i in range(4):
for j in range(3):
res+=dp[i][j]
return res%limit
|
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:
limit = 10**9+7
dp = [[1 for i in range(3)] for j in range(4)]
dp[3][0],dp[3][2] = 0,0
direction = [(1,2),(-1,2),(1,-2),(-1,-2),(2,-1),(2,1),(-2,1),(-2,-1)]
for k in range(n-1):
temp = [[0 for i in range(3)] for j in range(4)]
for i in range(4):
for j in range(3):
if i == 3 and j != 1:
continue
for di,dj in direction:
new_i,new_j = di+i,dj+j
if 0<= new_i< 4 and 0<= new_j<3:
temp[i][j] = (temp[i][j]+dp[new_i][new_j])%limit
dp = temp[:]
res = 0
for i in range(4):
for j in range(3):
res+= dp[i][j]
return res%limit
|
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:
dic = {}
dic[1] = [6,8]
dic[2] = [7,9]
dic[3] = [4,8]
dic[4] = [0,3,9]
dic[5] = []
dic[6] = [0,1,7]
dic[7] = [2,6]
dic[8] = [1,3]
dic[9] = [2,4]
dic[0] = [4,6]
def knightDialer(self, n: int) -> int:
mem = defaultdict(int)
for j in range(10):
mem[(1,j)] = 1
for i in range(1,n):
for j in range(10):
for num in self.dic[j]:
mem[(i+1,num)] += mem[(i,j)]
mem[(i+1,num)] = mem[(i+1,num)] %(10**9+7)
res = 0
for i in range(10):
res += mem[(n,i)]
res = res%(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
# dp = [ [[0]*3 for _2 in range(4)] for _ in range(N)]
dp = [[0]*3 for _2 in range(4)]
for i in range(4):
for j in range(3):
if (i,j) in [(3,0),(3,2)]:
continue
dp[i][j] = 1
for k in range(1,N):
new_dp = [[0]*3 for _2 in range(4)]
for i in range(3):
for j in range(i,3):
# print(i,j)
a = [[i+2,j+1],[i+2,j-1],[i-2,j+1],[i-2,j-1],
[i+1,j+2],[i-1,j+2],[i+1,j-2],[i-1,j-2]]
for p in a:
if not(0 <= p[0] < 4) or not(0 <= p[1] < 3):
continue
new_dp[i][j] += dp[p[0]][p[1]] % MOD
# new_dp[j][i] = new_dp[i][j]
new_dp[1][0] = new_dp[1][2]
new_dp[2][0] = new_dp[2][2]
new_dp[2][1] = new_dp[0][1]
i = 3
j = 1
new_dp[3][1] = (dp[1][2] + dp[1][0]) % MOD
dp = new_dp
# print(dp)
s = 0
# dp[3][0] = dp[N-1][3][2] = 0
for i in dp:
for j in i:
s += j % MOD
return s % MOD
# cac = {}
# MOD = 10**9 + 7
# def d(k,i,j):
# try:
# return cac[(k,i,j)]
# except:
# pass
# if (i,j) in [(3,0),(3,2)]:
# return 0
# if not(0 <= i < 4) or not(0 <= j < 3):
# return 0
# if k == 0:
# return 1
# temp = 0
# temp += d(k-1,i+2,j+1) % MOD
# temp += d(k-1,i+2,j-1) % MOD
# temp += d(k-1,i-2,j+1) % MOD
# temp += d(k-1,i-2,j-1) % MOD
# temp += d(k-1,i+1,j+2) % MOD
# temp += d(k-1,i-1,j+2) % MOD
# temp += d(k-1,i+1,j-2) % MOD
# temp += d(k-1,i-1,j-2) % MOD
# cac[(k,i,j)] = temp
# return temp
# ans = 0
# for i in range(4):
# for j in range(3):
# ans += d(N-1,i,j) % MOD
# return ans % 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 __init__(self):
self.moves = [
[4,6],
[8,6],
[7,9],
[4,8],
[9,3,0],
[],
[7,1,0],
[6,2],
[1,3],
[2,4],
]
self.memoized_results= {}
def knightDialer(self, n: int) -> int:
total_moves = 0
for number in range(10):
moves = self.knightDialerOnOneDigit(number, n-1)
total_moves += moves
return(total_moves%(pow(10,9) +7))
def knightDialerOnOneDigit(self, current_number, remaining_moves):
if remaining_moves == 0:
return 1
moves = 0
for number in self.moves[current_number]:
if (number, remaining_moves-1) in list(self.memoized_results.keys()):
new_moves = self.memoized_results[(number, remaining_moves-1)]
else:
new_moves = self.knightDialerOnOneDigit(number, remaining_moves-1)
self.memoized_results[(number, remaining_moves-1)] = new_moves
moves += new_moves
return moves
|
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
|
MOD = 10**9 + 7
class Solution:
def knightDialer(self, n: int) -> int:
@lru_cache(None)
def dfs(i, j, k):
# base case
if k == n - 1:
return 1
# recursion
res = 0
for di, dj in [(-2, -1), (-2, 1), (2, -1), (2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2)]:
ni, nj = i + di, j + dj
if not (0 <= ni < 4 and 0 <= nj < 3):
continue
if (ni, nj) in [(3, 0), (3, 2)]:
continue
res += dfs(ni, nj, k + 1)
res %= MOD
return res
ans = 0
for i, j in product(range(4), range(3)):
if (i, j) in [(3, 0), (3, 2)]:
continue
ans += dfs(i, j, 0)
return ans % 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:
M = 4
N = 3
KMOD = 10 ** 9 + 7
dp = [[1 for _ in range(N)] for _ in range(M)] # init as 1 for 0/1 hops
dp[3][0] = dp[3][2] = 0
directions = [(1, 2), (-1, -2), (1, -2), (-1, 2),
(2, 1), (-2, -1), (2, -1), (-2, 1), ]
for _ in range(1, n): # starts from 1 hop
cur = [[0 for _ in range(N)] for _ in range(M)] # cur init as 0
for i in range(M):
for j in range(N):
if i == 3 and (j == 0 or j == 2):
continue
for d in directions:
x = i + d[0]
y = j + d[1]
if 0 <= x < M and 0 <= y < N:
cur[i][j] += dp[x][y]
dp = cur
result = 0
for i in range(M):
for j in range(N):
if i == 3 and (j == 0 or j == 2):
continue
result += dp[i][j]
return result % KMOD
|
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 for n in range(10)]
for i in range(N - 1):
(d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9]) = (
d[4] + d[6], d[6] + d[8], d[7] + d[9], d[4] + d[8], d[0] + d[3] + d[9],
0, d[0] + d[1] + d[7], d[2] + d[6], d[1] + d[3], d[2] + d[4])
return sum(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:
if N == 0:
return 0
dialer = [[1, 1, 1] for _ in range(4)]
directions = [[-1, 2], [-2, 1], [-2, -1], [-1, -2], [1, -2], [2, -1], [2, 1], [1, 2]]
forbidden = set()
forbidden.add((3, 0))
forbidden.add((3, 2))
for _ in range(1, N):
tmp = [[0, 0, 0] for _ in range(4)]
for i in range(4):
for j in range(3):
if (i, j) not in forbidden:
for direction in directions:
new_i, new_j = i + direction[0], j + direction[1]
if 0 <= new_i < 4 and 0 <= new_j < 3:
tmp[new_i][new_j] += dialer[i][j]
dialer = tmp
return (sum(sum(dialer, [])) - dialer[3][0] - dialer[3][2]) % (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:
valid = {
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: [4, 2],
0: [4, 6]
}
MOD = 10 ** 9 + 7
dp = {}
nums = 0
def dfs(pos, steps):
if steps == 0:
return 1
if (pos, steps,) in dp:
return dp[(pos, steps,)]
c = 0
for next in valid[pos]:
x = dfs(next, steps - 1)
dp[(next, steps - 1)] = x
c += x
c %= MOD
dp[(pos, steps,)] = c
return c
ans = 0
for i in range(10):
x = dfs(i, n - 1)
ans += x
ans %= MOD
return ans % 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
dp = [1 for _ in range(10)]
move = [[4,6], [6,8], [7,9], [4,8], [3,9,0], [], [1,7,0], [2,6], [1,3], [4,2]]
for i in range(n-1):
dp2 = [0] *10
for node, count in enumerate(dp):
for nei in move[node]:
dp2[nei] += count
dp2[nei] %MOD
dp = dp2
print(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):
# 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 # table[i] = number of way ending in i
for _ in range(N-1):
temp = [0] * 10
for node, count in enumerate(table):
for nei in moves[node]:
temp[nei] += count
temp[nei]%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:
moves = [(-2, 1),(-1, 2),(1,2),(2,1),(2,-1),(1,-2),(-1,-2),(-2,-1)]
previous = {(r, c): 1 for r, c in ((0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2),(3,1))}
current = {(r, c): 0 for r, c in previous}
for i in range(n-1):
for (r, c), v in previous.items():
for dr, dc in moves:
if (r+dr, c+dc) in current:
current[(r+dr, c+dc)] += v
current[(r+dr, c+dc)] %= 1000000007
previous = current
current = {(r, c): 0 for r, c in previous}
return sum(previous.values()) % 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:
keyboard = [[0] * 3 for _ in range(4)]
k = 1
for i in range(3):
for j in range(3):
keyboard[i][j] = k
k+=1
keyboard[-1][0] = -1
keyboard[-1][-1] = -1
mod = 10**9 + 7
count = [[1] * 3 for _ in range(4)]
count[-1][0] = 0
count[-1][-1] = 0
moves = [(2,1),(2,-1),(-2,1),(-2,-1),(1,2),(-1,2),(1,-2),(-1,-2)]
for k in range(n - 1):
tmp_count = [[0] * 3 for _ in range(4)]
for i in range(4):
for j in range(3):
if keyboard[i][j] < 0 :
continue
for mv in moves:
x = i + mv[0]
y = j + mv[1]
if 0<=x<4 and 0<=y<3 and keyboard[x][y]>=0:
tmp_count[x][y] = (tmp_count[x][y] + count[i][j]) % mod
count = tmp_count
num_count = 0
for r in count:
num_count = (num_count + sum(r)) % mod
return num_count
|
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:
mod = 10 ** 9 + 7
phone = [[1,1,1],[1,1,1],[1,1,1],[0,1,0]]
moves = ((2,1),(1,2),(2,-1),(1,-2),(-2,1),(-1,2),(-2,-1),(-1,-2))
def knightDialer(self, n: int) -> int:
# memo = {}
@lru_cache(None)
def helper(row, col, left):
# if (row,col,left) in memo:
# return memo[(row,col,left)]
# if not 0<=row<=3 or not 0<=col<=2 or Solution.phone[row][col] ==0:
# return 0
if left <= 0:
return 1
ans = 0
for r,c in Solution.moves:
if 0<=row+r<=3 and 0<=col+c<=2 and Solution.phone[row+r][col+c] ==1:
ans += helper(row+r,col+c,left-1)
# memo[(row,col,left)] = ans
return ans
ans = 0
for row in range(4):
for col in range(3):
if Solution.phone[row][col] == 1:
ans += helper(row, col, n-1)
return ans % Solution.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:
cell2jump = dict()
cell2jump[1] = [6, 8]
cell2jump[2] = [7, 9]
cell2jump[3] = [4, 8]
cell2jump[4] = [3, 9, 0]
cell2jump[5] = []
cell2jump[6] = [1, 7, 0]
cell2jump[7] = [2, 6]
cell2jump[8] = [1, 3]
cell2jump[9] = [2, 4]
cell2jump[0] = [4, 6]
# dp[n, i] = helper(n, i)
dp = defaultdict()
def helper(num_jumps, cell):
# return number of jumps if place at cell and have num_jumps remaining
if num_jumps == 0:
return 1
elif num_jumps == 1:
return len(cell2jump[cell])
if (num_jumps, cell) in dp:
return dp[num_jumps, cell]
res = 0
for nei in cell2jump[cell]:
res += helper(num_jumps-1, nei)
dp[num_jumps, cell] = res
return res
total = []
for i in range(10):
total.append(helper(n-1, i))
return sum(total) % (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
f1,f2,f3,f4,f6,f7,f8,f9,f0 = [1]*n,[1]*n,[1]*n,[1]*n,[1]*n,[1]*n,[1]*n,[1]*n,[1]*n
for j in range(1,n):
f0[j] = (f4[j-1]+f6[j-1])%(1e9+7)
f1[j] = (f6[j-1]+f8[j-1])%(1e9+7)
f2[j] = (f7[j-1]+f9[j-1])%(1e9+7)
f3[j] = (f4[j-1]+f8[j-1])%(1e9+7)
f4[j] = (f3[j-1]+f9[j-1]+f0[j-1])%(1e9+7)
f6[j] = (f1[j-1]+f7[j-1]+f0[j-1])%(1e9+7)
f7[j] = (f6[j-1]+f2[j-1])%(1e9+7)
f8[j] = (f1[j-1]+f3[j-1])%(1e9+7)
f9[j] = (f2[j-1]+f4[j-1])%(1e9+7)
# res = 2*(f0[n]+f1[n]+f2[n]+f3[n]+f7[n]+f8[n]+f9[n])+3*(f4[n]+f6[n])
res = f0[n-1]+f1[n-1]+f2[n-1]+f3[n-1]+f7[n-1]+f8[n-1]+f9[n-1]+f4[n-1]+f6[n-1]
return int(res%(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:
N = n
transition = {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]}
# key:cur position
# value: next position
self.count = 0
self.mem = {}
def dfs(cur,steps):
if steps==2:
#self.count+=len(transition[cur])
return len(transition[cur])
else:
tmp = 0
if (cur,steps) in self.mem:
return self.mem[(cur,steps)]
for neigh in transition[cur]:
tmp +=dfs(neigh,steps-1)
#print(tmp)
self.mem[(cur,steps)] = tmp
return self.mem[(cur,steps)]
if N==1:
return 10
for i in range(10):
self.count+=dfs(i,N)
return self.count%(10**9+7)
# if n==1:
# return 10
# jump = {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]}
# def get(n,d):
# if n==1:
# s = 0
# for key in d.keys():
# s+=d[key]
# return s%(10**9+7)
# new_d = collections.defaultdict(int)
# for key in d.keys():
# nexts = jump[key]
# for next in nexts:
# new_d[next]+=d[key]
# return get(n-1,new_d)
# count = 0
# for i in range(10):
# d = collections.defaultdict(int)
# d[i]=1
# count+=get(n,d)
# return 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:
def dp(cur: int, k: int, cache: dict) -> int:
state = tuple([cur, k])
if state in cache:
return cache[state]
if k==0:
return 1
dic = [[4,6],[6,8],[7,9], [4,8], [0,3,9],[], [0,1,7],[2,6], [1,3], [2,4]]
res = 0
for nc in dic[cur]:
res += dp(nc, k-1, cache)
cache[state] = res
return cache[state]
result = 0
cache = {}
mod = 10**9 + 7
for i in range(10):
result += dp(i, n-1, cache)
result = result%mod
return result%(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:
memo = {}
return sum(self.hopper(i, n-1, memo) for i in range(10)) % (10 ** 9 + 7)
def hopper(self, cur_ind, hops_left, memo):
if hops_left == 0:
return 1
if (cur_ind, hops_left) in memo:
return memo[(cur_ind, hops_left)]
counts = 0
for nbr in self.nbrs(cur_ind):
res = self.hopper(nbr, hops_left-1, memo)
counts += res
memo[(nbr, hops_left-1)] = res
return counts
def nbrs(self, ind):
nbrs = {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: [1, 3],
9: [4, 2]}
return nbrs[ind]
|
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:
moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],
[1,7,0],[2,6],[1,3],[2,4]]
@lru_cache(maxsize=100000)
def dfs(s, n):
if n == 0:
return 1
# for v in moves[s]:
return sum([dfs(v, n-1) for v in moves[s]])
answer = 0
for i in range(10):
answer += dfs(i, N-1)
return answer % (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 reduce
class Solution:
validMoves = {
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}
}
def knightDialer(self, n: int) -> int:
cache = {} # from (remainingMoves, location) => numberOfPaths
def reducer(acc: int, x: int) -> int:
return (acc + x) % 1000000007 #10^9+7
def helper(remainingMoves: int, location: int) -> int:
depth = n - remainingMoves
insert = '\\t' * depth
# print('{}ENTER: at {} with {} remaining'.format('\\t' * (depth - 1), location, remainingMoves))
if remainingMoves == 0:
# print('{}Ran out of moves in location {}'.format(insert, location))
return 1
else:
if (remainingMoves, location) in cache:
answer = cache[(remainingMoves, location)]
# print('{}Found ({}, {}) in cache: returning {}'.format(insert, remainingMoves, location, answer))
return answer
else:
nextMoves = self.validMoves[location]
answers = [helper(remainingMoves - 1, newLocation) for newLocation in nextMoves]
reduced = reduce(reducer, answers, 0)
cache[(remainingMoves, location)] = reduced
# print('{}Calculated ({}, {}) = {}'.format(insert, remainingMoves, location, reduced))
return reduced
answers = [helper(n-1, x) for x in range(0, 10)]
reduced = reduce(reducer, answers, 0)
return reduced
|
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:
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [-1, 0, -1]]
R, C = len(grid), len(grid[0])
dp = [[1] * C for _ in range(R)]
invalid = set([(3, 0), (3, 2)])
for r, c in invalid:
dp[r][c] = 0
dirs = [
(2, 1), (-2, 1), (2, -1), (-2, -1),
(1, 2), (-1, 2), (1, -2), (-1, -2)
]
for _ in range(n - 1):
dp2 = [[0] * C for _ in range(R)]
for r, row in enumerate(dp):
for c, val in enumerate(row):
if (r, c) in invalid:
continue
for dr, dc in dirs:
nr, nc = r + dr, c + dc
if (nr, nc) in invalid:
continue
if 0 <= nr < R and 0 <= nc < C:
dp2[nr][nc] += val
dp = dp2
return sum(map(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:
m,n = 4,3
dp = {}
ans = 0
for k in range(1,N+1):
count = 0
for i in range(m):
for j in range(n):
if i==3 and j in [0,2]: continue
temp = 0
if 1==k:
temp+=1
else:
for a,b in [(2,1),(2,-1),(-2,1),(-2,-1),(1,2),(1,-2),(-1,2),(-1,-2)]:
r,c =i+a, j+b
if 0<=r<m and 0<=c<n and (r,c) not in [(3,0), (3,2)]:
# print(r,c,k-1)
temp+=dp[(r,c,k-1)]
dp[(i,j,k)] = temp
count+=temp
ans = count
# print(dp)
return ans%(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
phone = [[1,1,1],[1,1,1],[1,1,1],[0,1,0]]
moves = ((2,1),(1,2),(2,-1),(1,-2),(-2,1),(-1,2),(-2,-1),(-1,-2))
# memo = {}
@lru_cache(None)
def helper(row, col, left):
# if (row,col,left) in memo:
# return memo[(row,col,left)]
if not 0<=row<=3 or not 0<=col<=2 or phone[row][col] ==0:
return 0
if left <= 0:
return 1
ans = 0
for r,c in moves:
if 0<=row+r<=3 and 0<=col+c<=2 and phone[row+r][col+c] ==1:
ans += helper(row+r,col+c,left-1)
# memo[(row,col,left)] = ans
return ans
ans = 0
for row in range(4):
for col in range(3):
if phone[row][col] == 1:
ans += helper(row, col, n-1)
return ans % 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:
# board = [[str(j) for j in (1 + 3*i, 3*i + 2, 3*i + 3)] for i in range(3)]
# board.append(['*', '0', '#'])
# direct = [(-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1)]
# dp = {}
# def dfs(i, j, step):
# if i < 0 or i > 3 or j < 0 or j > 2: return 0
# if board[i][j] in '*#': return 0
# if step == 1: return 1
# key = (board[i][j], step)
# if key in dp: return dp[key]
# dest = set([(i + dr, j + dc) for dr, dc in direct])
# dp[key] = sum([dfs(r, c, step - 1) for r, c in dest])
# return dp[key]
# res = 0
# for i in range(10):
# res += dfs(str(i), n)
# return res%(10**9 + 7)
# dp = {}
# def nextStep(k):
# if k == 1: return [6, 8]
# if k == 2: return [7, 9]
# if k == 3: return [4, 8]
# if k == 4: return [9, 3, 0]
# if k == 6: return [7,1,0]
# if k == 7: return [6,2]
# if k == 8: return [1,3]
# if k == 9: return [4,2]
# if k == 0: return [4, 6]
# def dfs(k, step):
# if k == 5 and step > 1: return 0
# if step == 1: return 1
# key = (k, step)
# if key in dp: return dp[key]
# dp[key] = sum([dfs(e, step - 1) for e in nextStep(k)])
# return dp[key]
# res = 0
# for i in range(10):
# res += dfs(i, n)
# return res%(10**9 + 7)
dp = [1]*10
if n == 1: return 10
for i in range(n-1):
dpc = dp[:]
dpc[0] = dp[4] + dp[6]
dpc[1] = dp[6] + dp[8]
dpc[2] = dp[7] + dp[9]
dpc[3] = dp[4] + dp[8]
dpc[4] = dp[9] + dp[3] + dp[0]
dpc[5] = 0
dpc[6] = dp[7] + dp[1] + dp[0]
dpc[7] = dp[6] + dp[2]
dpc[8] = dp[1] + dp[3]
dpc[9] = dp[2] + dp[4]
dp = dpc
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:
dp = [[None] * 10 for _ in range(N + 1)]
edges = [[1,6], [1,8], [2, 7], [2, 9],
[3, 4], [3, 8], [4, 9], [4, 0],
[6, 7], [6, 0]]
graph = {}
for edge0, edge1 in edges:
graph[edge0] = graph.get(edge0, []) + [edge1]
graph[edge1] = graph.get(edge1, []) + [edge0]
def count(n, x):
if n == 0:
return 1
if dp[n][x] is not None:
return dp[n][x]
dp[n][x] = sum([count(n - 1, y) for y in graph.get(x, [])])
return dp[n][x]
return sum([count(N - 1, x) for x 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:
arr = [1]*10
mul = [[0,0,0,0,1,0,1,0,0,0],
[0,0,0,0,0,0,1,0,1,0],
[0,0,0,0,0,0,0,1,0,1],
[0,0,0,0,1,0,0,0,1,0],
[1,0,0,1,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,0,0],
[1,1,0,0,0,0,0,1,0,0],
[0,0,1,0,0,0,1,0,0,0],
[0,1,0,1,0,0,0,0,0,0],
[0,0,1,0,1,0,0,0,0,0]]
for n in range(N-1):
newarr = [0]*10
for i in range(10):
for j in range(10):
newarr[i] += mul[i][j]*arr[j]
newarr[i] %= 10**9+7
arr = newarr
s = 0
for x in arr:
s = (s+x)%(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:
d = {(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2), (3, 1)}
dirs = [(-2, -1), (-2, 1), (2, -1), (2, 1), (-1, 2), (1, 2), (-1, -2), (1, -2)]
m = 10**9 + 7
dp = {}
for x in d:
dp[x] = [0] * (N + 1)
dp[x][1] = 1
for n in range(2, N + 1):
for r, c in d:
for z in dirs:
rz, cz = r + z[0], c + z[1]
if (rz, cz) in d:
dp[(r, c)][n] = (dp[(r, c)][n] + dp[(rz, cz)][n - 1]) %m
return sum(x[N] for x in dp.values()) % 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:
m,n = 4,3
dp = {}
ans = 0
for k in range(1,N+1):
count = 0
for i in range(m):
for j in range(n):
if i==3 and j in [0,2]: continue
temp = 0
if 1==k:
temp+=1
else:
for a,b in [(2,1),(2,-1),(-2,1),(-2,-1),(1,2),(1,-2),(-1,2),(-1,-2)]:
r,c =i+a, j+b
if 0<=r<m and 0<=c<n and (r,c) not in [(3,0), (3,2)]:
temp+=dp[(r,c,k-1)]
dp[(i,j,k)] = temp
count+=temp
ans = count%(10**9+7)
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:
@lru_cache(None)
def dp(i, j, k):
if k == 0:
return 1
result = 0
for di in [-2, -1, 1, 2]:
for dj in [3 - abs(di), abs(di) - 3]:
ii, jj = i + di, j + dj
if 0 <= ii < m and 0 <= jj < n and not (ii == 3 and (jj == 0 or jj == 2)):
result = (result + dp(ii, jj, k - 1)) % MOD
return result
MOD = 10 ** 9 + 7
k = n
m, n = 4, 3
result = 0
for i in range(m):
for j in range(n):
if i == 3 and (j == 0 or j == 2):
continue
result = (result + dp(i, j, k - 1)) % MOD
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:
def knightDialer(self, left: int) -> int:
moves = [(2,-1),(2,1),(-2,1),(-2,-1),(-1,2),(-1,-2),(1,2),(1,-2)]
n = 4
m = 3
@lru_cache(None)
def back(i,j,left):
if left==1:
return 1
ans = 0
for a,b in moves:
if 0<=i+a<n and 0<=j+b<m and (i+a,j+b)!=(3,0) and (i+a,j+b)!=(3,2):
ans+=back(i+a, j+b, left-1)
return ans
main = 0
for i in range(3):
for j in range(3):
main+= back(i,j, left)
main+=back(3, 1,left)
return main%(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:
def get_position(pos):
_map= {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: [4,2],
}
return _map[pos]
cache = {}
def solve(pos, step):
key = (pos, step)
if key not in cache:
if step>=n:
return 1
res = 0
for i in get_position(pos):
res += solve(i, step + 1)
res = res % (pow(10,9) + 7)
cache[key] = res
return cache[key]
res = 0
for i in range(0, 10):
res += solve(i, 1)
return res % (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:
def knightPos(i,j):
#left top bot
lt = (i+2,j-1)
lb = (i-2, j-1)
#right top bot
rt = (i+2, j+1)
rb = (i-2, j+1)
#top left right
tl = (i+1, j-2)
tr = (i+1, j+2)
#bot left right
bl = (i-1, j-2)
br = (i-1, j+2)
return (lt,lb,rt,rb,tl,tr,bl,br)
dp = [[1]*3 for _ in range(4)]
dp[3][0] = dp[3][2] = 0
mod = (10**9)+7
for k in range(1,n):
tmp = [[0]*3 for _ in range(4)]
for i in range(4):
for j in range(3):
if (i==3 and j==0) or (i==3 and j==2):
continue
for x,y in knightPos(i,j):
if 0 <= x <= 3 and 0 <= y <= 2:
tmp[i][j] += dp[x][y]
dp = tmp
return sum([sum(i) for i in 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:
@lru_cache(maxsize=None)
def startFrom(self, i, l):
if l <= 1:
return 1 if l == 1 else 0
else:
return sum([self.startFrom(j, l-1) for j in self.mapping[i]]) % (10**9+7)
def knightDialer(self, n: int) -> int:
self.mapping = {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]}
return sum([self.startFrom(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
|
import numpy as np
class Solution:
def knightDialer(self, N: int) -> int:
mod = 10**9 + 7
if N == 1: return 10
M = np.matrix([[0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0]])
res = np.matrix([[1]*10])
N -= 1
while N:
if N % 2 != 0:
res = res * M % mod
N -= 1
else:
M = M * M % mod
N /= 2
return int(np.sum(res)) % 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:
ans = [1 for x in range(10)]
mod = 1000_000_007
for i in range(n-1):
ans[0], ans[1], ans[2], ans[3], ans[4], ans[5], ans[6], ans[7], ans[8], ans[9] = ans[4]+ans[6], ans[8]+ans[6], ans[7]+ans[9], ans[8]+ans[4], ans[9]+ans[3]+ans[0], 0, ans[7]+ans[1]+ans[0], ans[6]+ans[2], ans[1]+ans[3], ans[4]+ans[2]
return sum(ans)%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:
from collections import defaultdict
def knightDialer(self, N: int) -> int:
pos_mapping=[[4, 6], [6, 8], [9, 7], [4, 8], [9, 3, 0], [], [7, 1, 0], [6, 2], [1, 3], [4, 2]]
total_count = 0
for i in range(10):
prev_arr = [0]*10
prev_arr[i] = 1
for ii in range(N - 1):
new_arr = [0]*10
for idx,count in enumerate(prev_arr):
if count == 0:
continue
for next_pos in pos_mapping[idx]:
new_arr[next_pos] += count
prev_arr = new_arr
total_count += sum(prev_arr)
MOD = 10**9 + 7
return total_count % 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:
dirs = [(2, 1), (1, 2), (2, -1), (1, -2), (-2, -1), (-1, -2), (-2, 1), (-1, 2)]
dp = [[0 for j in range(N)] for i in range(10)]
for num in range(10):
dp[num][0] = 1
for i in range(1, N):
for num in range(10):
if num == 0:
pos = (1, 3)
else:
pos = ((num - 1) % 3, (num - 1) // 3)
x = pos[0]
y = pos[1]
for d in dirs:
new_x = x - d[0]
new_y = y - d[1]
if new_x < 0 or new_x > 2 or new_y < 0 or new_y > 3:
continue
if (new_x == 0 and new_y == 3) or (new_x == 2 and new_y == 3):
continue
new_num = 0 if (new_x == 1 and new_y == 3) else new_y * 3 + new_x + 1
dp[num][i] += dp[new_num][i-1]
res = 0
for num in range(10):
res += dp[num][N-1]
return res % (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 helper(self, n, board, row, col, cache):
if n == 0:
return 1
if (row, col, n) in cache:
return cache[(row, col, n)]
rows = len(board)
cols = len(board[0])
result = 0
for rMove, cMove in [(-1, -2), (-2, -1), (2, 1), (1, 2), (-2, 1), (1, -2), (2, -1), (-1, 2)]:
if 0 <= row + rMove < rows and 0 <= col + cMove < cols and board[row+rMove][col+cMove] not in ['*', '#']:
result += self.helper(n-1, board, row+rMove, col+cMove, cache)
cache[(row, col, n)] = result
return result
def knightDialer(self, n: int) -> int:
board = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['*', '0', '#']
]
totalResult = 0
cache = {}
rows = len(board)
cols = len(board[0])
for r in range(rows):
for c in range(cols):
if board[r][c] not in ['*', '#']:
totalResult += self.helper(n-1, board, r, c, cache)
return totalResult % (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:
def dp(cur: int, k: int, cache: dict) -> int:
state = tuple([cur, k])
if state in cache:
return cache[state]
if k==0:
return 1
dic = {
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]
}
res = 0
for nc in dic.get(cur,[]):
res += dp(nc, k-1, cache)
cache[state] = res
return cache[state]
result = 0
cache = {}
mod = 10**9 + 7
for i in range(10):
result += dp(i, n-1, cache)
return result%(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:
mod = 10 ** 9 + 7
phone = [[1,1,1],[1,1,1],[1,1,1],[0,1,0]]
moves = ((2,1),(1,2),(2,-1),(1,-2),(-2,1),(-1,2),(-2,-1),(-1,-2))
def knightDialer(self, n: int) -> int:
# memo = {}
@lru_cache(None)
def helper(row, col, left):
# if (row,col,left) in memo:
# return memo[(row,col,left)]
# if not 0<=row<=3 or not 0<=col<=2 or Solution.phone[row][col] ==0:
# return 0
if left <= 0:
return 1
ans = 0
for r,c in Solution.moves:
if 0<=row+r<=3 and 0<=col+c<=2 and Solution.phone[row+r][col+c] ==1:
ans = (ans + helper(row+r,col+c,left-1)) % Solution.mod
# memo[(row,col,left)] = ans
return ans
ans = 0
for row in range(4):
for col in range(3):
if Solution.phone[row][col] == 1:
ans = (ans + helper(row, col, n-1)) % Solution.mod
return ans# % Solution.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:
board=[[1,2,3],[4,5,6],[7,8,9],['*',0,'#']]
dirc=[[-2,-1],[-2,1],[-1,-2],[-1,2],[1,-2],[1,2],[2,-1],[2,1]]
mod=10**9+7;mem={}
def dfs(i,j,k):
if k==1:
return 1
if (i,j,k) in mem:
return mem[(i,j,k)]
ans=0
for x,y in dirc:
if -1<i+x<4 and -1<j+y<3 and board[i+x][j+y]!='#' and board[i+x][j+y]!='*':
ans+=dfs(i+x,j+y,k-1)%mod
ans%=mod
mem[(i,j,k)]=ans
return ans
res=0
for i in range(3):
for j in range(3):
res+=dfs(i,j,n)%mod
res=(res+dfs(3,1,n))%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
# # dp = [ [[0]*3 for _2 in range(4)] for _ in range(N)]
# dp = [[0]*3 for _2 in range(4)]
# for i in range(4):
# for j in range(3):
# if (i,j) in [(3,0),(3,2)]:
# continue
# dp[i][j] = 1
# for k in range(1,N):
# new_dp = [[0]*3 for _2 in range(4)]
# for i in range(3):
# for j in range(i,3):
# # print(i,j)
# a = [[i+2,j+1],[i+2,j-1],[i-2,j+1],[i-2,j-1],
# [i+1,j+2],[i-1,j+2],[i+1,j-2],[i-1,j-2]]
# for p in a:
# if not(0 <= p[0] < 4) or not(0 <= p[1] < 3):
# continue
# new_dp[i][j] += dp[p[0]][p[1]] % MOD
# # new_dp[j][i] = new_dp[i][j]
# new_dp[1][0] = new_dp[1][2]
# new_dp[2][0] = new_dp[2][2]
# new_dp[2][1] = new_dp[0][1]
# i = 3
# j = 1
# new_dp[3][1] = (dp[1][2] + dp[1][0]) % MOD
# dp = new_dp
# # print(dp)
# s = 0
# # dp[3][0] = dp[N-1][3][2] = 0
# for i in dp:
# for j in i:
# s += j % MOD
# return s % MOD
cac = {}
MOD = 10**9 + 7
pmvs = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],[1,7,0],[2,6],[1,3],[4,2]]
def d(k,i):
try:
# 1/0
return cac[(k,i)]
except:
pass
if k == 0:
return 1
s = 0
for j in pmvs[i]:
s += d(k-1,j) % MOD
cac[(k,i)] = s % MOD
return cac[(k,i)]
ans = 0
for i in range(10):
ans += d(N-1,i) % MOD
return ans % 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 __init__(self):
self.helper = {(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)}
def dynamic(self,r,c,dp):
curr = 0
for s in self.helper:
x,y = r+s[0],c+s[1]
if 0<=x<4 and 0<=y<3 and ((x,y) != (3,0) or (x,y) != (3,2)):
curr += dp[x][y]
return curr#%(10**9+7)
def knightDialer(self, n: int) -> int:
dp = [[1]*3 for _ in range(4)]
dp[3][0] = dp[3][2] = 0
for i in range(n-1):
dp2 = [[0]*3 for _ in range(4)]
for r in range(4):
for c in range(3):
if (r,c) == (3,0) or (r,c) == (3,2): continue
dp2[r][c] = self.dynamic(r,c,dp)
dp = dp2[:]
return sum([dp[i][j] for i in range(4) for j in range(3)])%(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 matrixExponentiation(self, a: List[List[int]], k: int) -> List[List[int]]:
if k == 0:
return [[1 if r == c else 0 for c in range(len(a[0]))] for r in range(len(a))]
result = self.matrixExponentiation(a, k // 2)
result = self.matrixMultiplication(result, result)
if k % 2 == 1:
result = self.matrixMultiplication(a, result)
return result
def matrixMultiplication(self, a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
m, n, p = len(a), len(a[0]), len(b[0])
assert len(a[0]) == len(b)
matrix = [[0] * p for _ in range(m)]
for r in range(m):
for c in range(p):
for i in range(n):
matrix[r][c] += a[r][i] * b[i][c]
return matrix
def knightDialer(self, n: int):
matrix = [[(1 if r in Solution.adjacency[c] else 0) for c in range(10)] for r in range(10)]
matrix = self.matrixExponentiation(matrix, n - 1)
count = [[1] for _ in range(10)]
count = self.matrixMultiplication(matrix, count)
return sum((sum(r) for r in count)) % (10 ** 9 + 7)
def knightDialerDP(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:
nos = [[1,2,3], [4,5,6], [7,8,9], [None, 0, None]]
if N == 0:
return 0
memo = {}
def calculate(currentIndex, remainingSteps):
if remainingSteps == 0:
return 1
if (currentIndex, remainingSteps) in memo:
return memo[(currentIndex, remainingSteps)]
i, j = currentIndex
possibleMoves = []
if i+2 < 4 and j - 1 >= 0 and nos[i+2][j-1] is not None:
possibleMoves.append((i+2, j-1))
if i+2 < 4 and j + 1 < 3 and nos[i+2][j+1] is not None:
possibleMoves.append((i+2, j+1))
if i - 1 >= 0 and j+2 < 3 and nos[i-1][j+2] is not None:
possibleMoves.append((i-1,j+2))
if i+1 < 4 and j+2 < 3 and nos[i+1][j+2] is not None:
possibleMoves.append((i+1, j+2))
if i-2 >= 0 and j+1 < 3 and nos[i-2][j+1] is not None:
possibleMoves.append((i-2,j+1))
if i-2 >=0 and j-1 >= 0 and nos[i-2][j-1] is not None:
possibleMoves.append((i-2, j-1))
if i-1>=0 and j-2>=0 and nos[i-1][j-2] is not None:
possibleMoves.append((i-1,j-2))
if i+1 < 4 and j-2 >=0 and nos[i+1][j-2] is not None:
possibleMoves.append((i+1,j-2))
if remainingSteps == 1:
memo[(currentIndex, remainingSteps)] = len(possibleMoves)
return len(possibleMoves)
totalNos = 0
for nextInd in possibleMoves:
if (nextInd, remainingSteps-1) in memo:
totalNos += memo[nextInd, remainingSteps-1]
else:
totalNos += calculate(nextInd, remainingSteps-1)
# if totalNos >= 10**9 + 7:
# totalNos = totalNos % (10**9 + 7)
memo[(currentIndex, remainingSteps)] = totalNos
return memo[(currentIndex, remainingSteps)]
total = 0
for i in range(0, 4):
for j in range(0, 3):
if nos[i][j] is not None:
total += calculate((i,j), N-1)
return total % (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.helper = {(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)}
def dynamic(self,r,c,dp):
curr = 0
for s in self.helper:
x,y = r+s[0],c+s[1]
if 0<=x<4 and 0<=y<3 and ((x,y) != (3,0) or (x,y) != (3,2)):
curr += dp[x][y]
return curr
def knightDialer(self, n: int) -> int:
dp = [[1]*3 for _ in range(4)]
dp[3][0] = dp[3][2] = 0
for i in range(n-1):
dp2 = [[0]*3 for _ in range(4)]
for r in range(4):
for c in range(3):
if (r,c) == (3,0) or (r,c) == (3,2): continue
dp2[r][c] = self.dynamic(r,c,dp)
dp = dp2[:]
#print(dp)
return sum([dp[i][j] for i in range(4) for j in range(3)])%(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:
P = 10**9 + 7
def wrap(x):
return x - P if x >= P else x
s = 0
for k in range(10):
d = [0] * 10
d2 = [0] * 10
d[k] = 1
for i in range(n - 1):
d2[0] = wrap(d[4] + d[6])
d2[1] = wrap(d[6] + d[8])
d2[2] = wrap(d[7] + d[9])
d2[3] = wrap(d[4] + d[8])
d2[4] = wrap(d[3] + d[9] + d[0])
d2[5] = 0
d2[6] = wrap(d[1] + d[7] + d[0])
d2[7] = wrap(d[2] + d[6])
d2[8] = wrap(d[1] + d[3])
d2[9] = wrap(d[2] + d[4])
d, d2 = d2, d
s += sum(d)
return s % (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:
from collections import defaultdict
def knightDialer(self, N: int) -> int:
pos_mapping=[[4, 6], [6, 8], [9, 7], [4, 8], [9, 3, 0], [], [7, 1, 0], [6, 2], [1, 3], [4, 2]]
total_count = 0
for i in range(10):
prev_arr = [0]*10
prev_arr[i] = 1
for _ in range(N - 1):
new_arr = [0]*10
for idx in range(10):
if prev_arr[idx] == 0:
continue
for next_pos in pos_mapping[idx]:
new_arr[next_pos] += prev_arr[idx]
prev_arr = new_arr
total_count += sum(prev_arr)
MOD = 10**9 + 7
return total_count % 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:
DIAL = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
['*', 0, '#']
]
def knightDialer(self, n: int) -> int:
directions = [(1, -2), (2, -1), (2, 1), (1, 2), (-1, 2), (-2, 1), (-2, -1), (-1, -2)]
count = 0
m = len(self.DIAL)
nn = len(self.DIAL[0])
cache = {}
def jump(i, j, path_length):
if path_length == n:
return 1
else:
total = 0
if (i, j, path_length) in cache:
return cache[i, j, path_length]
for inc_r, inc_c in directions:
r = i + inc_r
c = j + inc_c
if 0 <= r < m and 0 <= c < nn and isinstance(self.DIAL[r][c], int):
total += jump(r, c, path_length+1)
cache[i, j, path_length] = total
return total
res = 0
for i in range(m):
for j in range(nn):
if isinstance(self.DIAL[i][j], int):
res += jump(i, j, 1)
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:
path={
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]
}
M=10**9+7
@lru_cache
def paths(i,n):
#number of ways to jump from i n times
if n==1:
return len(path[i])
if n==0:
return 1
s=0
for v in path[i]:
s+=paths(v,n-1)
return s%M
ans=0
for k in path:
ans+=paths(k,n-1)
return ans%(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:
nos = [[1,2,3], [4,5,6], [7,8,9], [None, 0, None]]
if N == 0:
return 0
memo = {}
def calculate(currentIndex, remainingSteps):
if remainingSteps == 0:
return 1
if (currentIndex, remainingSteps) in memo:
return memo[(currentIndex, remainingSteps)]
i, j = currentIndex
possibleMoves = []
if i+2 < 4 and j - 1 >= 0 and nos[i+2][j-1] is not None:
possibleMoves.append((i+2, j-1))
if i+2 < 4 and j + 1 < 3 and nos[i+2][j+1] is not None:
possibleMoves.append((i+2, j+1))
if i - 1 >= 0 and j+2 < 3 and nos[i-1][j+2] is not None:
possibleMoves.append((i-1,j+2))
if i+1 < 4 and j+2 < 3 and nos[i+1][j+2] is not None:
possibleMoves.append((i+1, j+2))
if i-2 >= 0 and j+1 < 3 and nos[i-2][j+1] is not None:
possibleMoves.append((i-2,j+1))
if i-2 >=0 and j-1 >= 0 and nos[i-2][j-1] is not None:
possibleMoves.append((i-2, j-1))
if i-1>=0 and j-2>=0 and nos[i-1][j-2] is not None:
possibleMoves.append((i-1,j-2))
if i+1 < 4 and j-2 >=0 and nos[i+1][j-2] is not None:
possibleMoves.append((i+1,j-2))
if remainingSteps == 1:
memo[(currentIndex, remainingSteps)] = len(possibleMoves)
return len(possibleMoves)
totalNos = 0
for nextInd in possibleMoves:
totalNos += calculate(nextInd, remainingSteps-1)
# if totalNos >= 10**9 + 7:
# totalNos = totalNos % (10**9 + 7)
memo[(currentIndex, remainingSteps)] = totalNos
return memo[(currentIndex, remainingSteps)]
total = 0
for i in range(0, 4):
for j in range(0, 3):
if nos[i][j] is not None:
total += calculate((i,j), N-1)
return total % (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:
modulo = 10**9 + 7
self.count = 0
self.cache = {}
for digit in range(0,10):
self.count += self.getWays(digit, n-1)
return self.count % modulo
def getWays(self, digit, n):
if n == 0:
return 1
if (digit,n) in self.cache:
return self.cache[(digit,n)]
temp = 0
for neighbor in self.neighbors(digit):
temp += self.getWays(neighbor, n-1)
self.cache[(digit, n)] = temp
return self.cache[(digit,n)]
def neighbors(self, digit):
neighbors = {
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)
}
return neighbors[digit]
|
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 math
class Solution:
def knightDialer(self, N: int) -> int:
dp = [[[0 for _ in range(3)] for _ in range(4)] for _ in range(N)]
direction = [(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)]
for k in range(N):
for i in range(4):
for j in range(3):
if k == 0:
if (i == 3 and j == 0) or (i == 3 and j == 2):
dp[0][i][j] = None
else:
dp[0][i][j] = 1
else:
if dp[k-1][i][j] == None:
dp[k][i][j] = None
continue
if i == 1 and j == 1:
dp[k][i][j] = 0
continue
for d in direction:
fromi = i + d[0]
fromj = j + d[1]
#check fromi and fromj are valid
if 0 <= fromi <= 3 and 0 <= fromj <= 2 and dp[k-1][fromi][fromj] != None:
dp[k][i][j] += dp[k-1][fromi][fromj]
res = 0
for i in range(4):
for j in range(3):
if dp[-1][i][j] == None:
continue
res += dp[-1][i][j]
t = pow(10,9)+7
return res % t
|
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.memo = {}
def getOrCreate(self, i, n):
if (i,n) not in self.memo:
self.memo[(i, n)] = self.__knightDialerDP(i, n)
return self.memo[(i, n)]
def __knightDialerDP(self, i, n):
if n == 0:
return 0
if n == 1:
return 1
# if i in set([0,1,2,3,7,8,9]):
# return self.getOrCreate(i, n-1) + 2
# elif i in set([4,6]):
# return self.getOrCreate(i, n-1) + 3
# else:
# return 0
if i == 0:
return self.getOrCreate(4, n-1) + self.getOrCreate(6, n-1)
elif i == 1:
return self.getOrCreate(6, n-1) + self.getOrCreate(8, n-1)
elif i == 2:
return self.getOrCreate(7, n-1) + self.getOrCreate(9, n-1)
elif i == 3:
return self.getOrCreate(4, n-1) + self.getOrCreate(8, n-1)
elif i == 4:
return self.getOrCreate(0, n-1) + self.getOrCreate(3, n-1) + self.getOrCreate(9, n-1)
elif i == 6:
return self.getOrCreate(0, n-1) + self.getOrCreate(1, n-1) + self.getOrCreate(7, n-1)
elif i == 7:
return self.getOrCreate(2, n-1) + self.getOrCreate(6, n-1)
elif i == 8:
return self.getOrCreate(1, n-1) + self.getOrCreate(3, n-1)
elif i == 9:
return self.getOrCreate(2, n-1) + self.getOrCreate(4, n-1)
else:
return 0
def knightDialer(self, n: int) -> int:
if n == 0:
return 0
for k in range(n):
total_count = 0
for i in range(10):
total_count += self.__knightDialerDP(i, k+1)
return total_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:
dp = [1]*10
mod = 10**9 + 7
for i in range(1,n):
dp_old = dp.copy()
dp[0] = dp_old[4]+dp_old[6]
dp[1] = dp_old[6]+dp_old[8]
dp[2] = dp_old[7]+dp_old[9]
dp[3] = dp_old[4]+dp_old[8]
dp[4] = dp_old[3]+dp_old[9]+dp_old[0]
dp[5] = 0
dp[6] = dp_old[1]+dp_old[7]+dp_old[0]
dp[7] = dp_old[2]+dp_old[6]
dp[8] = dp_old[1]+dp_old[3]
dp[9] = dp_old[2]+dp_old[4]
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
|
import math
class Solution:
def knightDialer(self, N: int) -> int:
dp = [[[0 for _ in range(3)] for _ in range(4)] for _ in range(N)]
direction = [(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)]
for k in range(N):
for i in range(4):
for j in range(3):
if k == 0:
if (i == 3 and j == 0) or (i == 3 and j == 2):
dp[k][i][j] = None
else:
dp[k][i][j] = 1
else:
if dp[k-1][i][j] == None:
dp[k][i][j] = None
continue
if i == 1 and j == 1:
dp[k][i][j] = 0
continue
for d in direction:
fromi = i + d[0]
fromj = j + d[1]
#check fromi and fromj are valid
if 0 <= fromi <= 3 and 0 <= fromj <= 2 and dp[k-1][fromi][fromj] != None:
dp[k][i][j] += dp[k-1][fromi][fromj]
res = 0
for i in range(4):
for j in range(3):
if dp[-1][i][j] == None:
continue
res += dp[-1][i][j]
t = pow(10,9)+7
return res % t
|
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 helper(self,cache,position,num_hops):
neighbors = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],
[1,7,0],[2,6],[1,3],[2,4]]
if (position, num_hops) in cache:
return cache[ (position, num_hops) ]
if num_hops == 0:
return 1
else:
num_sequences = 0
for neighbor in neighbors[position]:
num_sequences += self.helper(cache,neighbor, num_hops - 1)
cache[ (position, num_hops) ] = num_sequences
return num_sequences
def knightDialer(self, n: int) -> int:
cache = {}
res = 0
for start in range(10):
res += self.helper(cache,start, n-1)
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:
movesMap=dict()
MOD=10**9+7
movesMap[0]=[4,6]
movesMap[1]=[6,8]
movesMap[2]=[7,9]
movesMap[3]=[4,8]
movesMap[4]=[0,3,9]
movesMap[5]=[]
movesMap[6]=[0,1,7]
movesMap[7]=[2,6]
movesMap[8]=[1,3]
movesMap[9]=[2,4]
totalCount=0
for i in range(10):
curPos=[0]*10
curPos[i]=1
for count in range(n-1):
nextPos=[0]*10
for curIndex in range(10):
if curPos[curIndex]>0:
for nextIndex in movesMap[curIndex]:
nextPos[nextIndex]+=curPos[curIndex]
nextPos[nextIndex]=nextPos[nextIndex]%MOD
curPos=nextPos
for finIndex in range(10):
totalCount+=curPos[finIndex]
totalCount=totalCount%MOD
return totalCount
|
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:
movesMap=dict()
MOD=10**9+7
movesMap[0]=[4,6]
movesMap[1]=[6,8]
movesMap[2]=[7,9]
movesMap[3]=[4,8]
movesMap[4]=[0,3,9]
movesMap[5]=[]
movesMap[6]=[0,1,7]
movesMap[7]=[2,6]
movesMap[8]=[1,3]
movesMap[9]=[2,4]
totalCount=0
for i in range(10):
curPos=[0]*10
curPos[i]=1
for count in range(n-1):
nextPos=[0]*10
for curIndex in range(10):
if curPos[curIndex]>0:
for nextIndex in movesMap[curIndex]:
nextPos[nextIndex]+=curPos[curIndex]
nextPos[nextIndex]%=MOD
curPos=nextPos
for finIndex in range(10):
totalCount+=curPos[finIndex]
totalCount%=MOD
return totalCount
|
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
pad = [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['*', '0', '#']]
dirs = [[-1, -2], [-2, -1], [1, -2], [2, -1], [-1, 2], [-2, 1], [1, 2], [2, 1]]
memo = dict()
for i in range(10):
memo[str(i)] = [0 for j in range(n + 1)]
memo[str(i)][1] = 1
for j in range(2, n + 1):
for r in range(4):
for c in range(3):
number = pad[r][c]
if number == '*' or number == '#':
continue
memo[number][j] = 0
for dir0 in dirs:
x = r + dir0[0]
y = c + dir0[1]
if x < 0 or x >= 4 or y < 0 or y >= 3 or pad[x][y] == '*' or pad[x][y] == '#':
continue
parent = pad[x][y]
memo[number][j] += memo[parent][j - 1]
res = 0
for i in range(10):
number = str(i)
res += memo[number][n]
return res % 1_000_000_007
# Idea: use 2D dp array memo[10][n + 1] to store the number of distinct phone numbers
# memo[i][0] = 0
# memo[i][j] = sum(memo[parent][j - 1])
# Res = sum(memo[i][n])
# Time: O(n)
# Space: O(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:
MOD = 10**9 + 7
# Move records at each idex, which numbers it can jump to
# 0 can jump to 4 and 6, 1 can jump to 6 and 8, etc
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:
moves = [(x, y) for x in range(-2, 3, 2) for y in range(-2, 3, 2) if abs(x) != abs(y)]
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],
}
@lru_cache
def dfs(num_jumps, cell):
if num_jumps == 1:
return 1
cur = 0
for i in mappings[cell]:
cur += dfs(num_jumps - 1, i)
return cur
for i in range(0, 10):
res += dfs(n, i)
return res % 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:
dialer_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]}
total = 0
for i in range(10):
if i == 5 and n==1:
total += 1
continue
level = {i:1}
curr = 1
while curr<n:
next_level = defaultdict(lambda:0)
for l in list(level.keys()):
for d in dialer_map[l]:
next_level[d]+=level[l]
level = next_level
curr += 1
count = sum(level.values())
total += count
total = total%(10**9+7)
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:
mod = 10 ** 9 + 7
dp = [1] * 10
for _ in range(2, n + 1):
dpc = dp.copy()
dp[0] = dpc[4] + dpc[6]
dp[1] = dpc[6] + dpc[8]
dp[2] = dpc[7] + dpc[9]
dp[3] = dpc[4] + dpc[8]
dp[4] = dpc[3] + dpc[9] + dpc[0]
dp[5] = 0
dp[6] = dpc[1] + dpc[7] + dpc[0]
dp[7] = dpc[2] + dpc[6]
dp[8] = dpc[1] + dpc[3]
dp[9] = dpc[4] + dpc[2]
res = sum(dp) % 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:
dialer_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]}
total = 0
for i in range(10):
if i == 5 and n==1:
total += 1
continue
level = {i:1}
curr = 1
while curr<n:
next_level = defaultdict(lambda:0)
for l in list(level.keys()):
for d in dialer_map[l]:
next_level[d]+=level[l]
level = next_level
curr += 1
count = sum(level.values())
total += count
return total%(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:
valid_positions = {
(0, 0): 1,
(1, 0): 2,
(2, 0): 3,
(0, 1): 4,
(1, 1): 5,
(2, 1): 6,
(0, 2): 7,
(1, 2): 8,
(2, 2): 9,
(1, 3): 0
}
mod_val = 10**9 + 7
cache = {}
def find_numbers(start, length):
if start not in valid_positions:
return 0
x,y = start
if (x,y,length) in cache:
return cache[(x,y,length)]
if length == n:
return 1
x,y = start
nums_found = sum(
[find_numbers((x+1, y-2), length+1),
find_numbers((x+2, y-1), length+1),
find_numbers((x-1, y-2), length+1),
find_numbers((x-2, y-1), length+1),
find_numbers((x-2, y+1), length+1),
find_numbers((x-1, y+2), length+1),
find_numbers((x+2, y+1), length+1),
find_numbers((x+1, y+2), length+1)]
) % mod_val
cache[(x,y,length)] = nums_found
return nums_found
numbers_found = 0
for position in valid_positions:
numbers_found += find_numbers(position, 1)
numbers_found %= mod_val
# print(sorted(nums))
return numbers_found
|
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:
self.mod = 10**9 + 7
total_path = 0
self.d = {}
for i in range(0,4):
for j in range(0,3):
total_path = (total_path + self.dfs(i,j,n) )%self.mod
#print(total_path)
return total_path
def dfs(self,i,j,n):
if i<0 or j<0 or j>=3 or i>=4 or (i==3 and j!=1):
return 0
if (i,j,n) in self.d:
return self.d[(i,j,n)]
if n==1:
return 1
total=self.dfs(i-1,j+2,n-1)%self.mod+self.dfs(i-1,j-2,n-1)%self.mod+self.dfs(i+1,j-2,n-1)%self.mod+self.dfs(i+1,j+2,n-1)%self.mod+self.dfs(i+2,j+1,n-1)%self.mod+self.dfs(i+2,j-1,n-1)%self.mod+self.dfs(i-2,j+1,n-1)%self.mod+self.dfs(i-2,j-1,n-1)%self.mod
self.d[(i,j,n)] = total
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:
memo = {}
maxi = (10**9)+7
def paths(i,j,n):
if i>3 or j>2 or i<0 or j <0 or (i==3 and j != 1):
return 0
if n == 1:
return 1
if (i,j,n) in memo:
return memo[(i,j,n)]
result = paths(i - 1, j - 2, n - 1) % maxi + paths(i - 2, j - 1, n - 1) % maxi + paths(i - 2, j + 1, n - 1) % maxi+ paths(i - 1, j + 2, n - 1) % maxi + paths(i + 1, j + 2, n - 1) % maxi + paths(i + 2, j + 1, n - 1) % maxi + paths(i + 2, j - 1, n - 1) % maxi + paths(i + 1, j - 2, n - 1) % maxi
memo[(i,j,n)] = result
return result
totalPaths = 0
for i in range(4):
for j in range(3):
totalPaths = (totalPaths+paths(i,j,N))%maxi
return totalPaths
# Inspired from Sriram's Java Solution
# https://leetcode.com/problems/knight-dialer/discuss/190787/How-to-solve-this-problem-explained-for-noobs!!!
|
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:
self.mod = 10**9 + 7
total_path = 0
self.d = {}
for i in range(0,4):
for j in range(0,3):
total_path = (total_path + self.dfs(i,j,n) )%self.mod
return total_path
def dfs(self,i,j,n):
if i<0 or j<0 or j>=3 or i>=4 or (i==3 and j!=1):
return 0
if (i,j,n) in self.d:
return self.d[(i,j,n)]
if n==1:
return 1
total=self.dfs(i-1,j+2,n-1)%self.mod+self.dfs(i-1,j-2,n-1)%self.mod+self.dfs(i+1,j-2,n-1)%self.mod+self.dfs(i+1,j+2,n-1)%self.mod+self.dfs(i+2,j+1,n-1)%self.mod+self.dfs(i+2,j-1,n-1)%self.mod+self.dfs(i-2,j+1,n-1)%self.mod+self.dfs(i-2,j-1,n-1)%self.mod
self.d[(i,j,n)] = total
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:
total = 0
places = {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
dp = [1]*10
for i in range(2, N+1):
tmp = copy.deepcopy(dp)
for i in range(0, 10):
if i!=5:
vals = places.get(i)
new_tot = 0
for v in vals:
new_tot += dp[v]
tmp[i] = new_tot
dp = copy.deepcopy(tmp)
ans = 0
for d in dp:
ans+=d
return (ans-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 nextPositions(self,n):
x,y = self.x_y_map[n]
arr = [(x-2,y+1),(x-1,y+2),(x+1,y+2),(x+2,y+1),(x+2,y-1),(x+1,y-2),(x-1,y-2),(x-2,y-1)]
return [self.reverse_map[v] for v in arr if v in self.reverse_map]
def build_x_y_map(self):
r,c = 0,0
ht = {}
for i in range(1,10):
ht[i] = (r,c)
c+=1
if c == 3:
r+=1
c = 0
ht[0] = (3,1)
return ht
def knightDialer(self, n: int) -> int:
self.x_y_map = self.build_x_y_map()
self.reverse_map = {v:k for k,v in list(self.x_y_map.items())}
def count_moves(curr_cell,hops,memo):
if hops == 0:
return 1
key = (curr_cell,hops)
if key not in memo:
ways = 0
for pos in self.nextPositions(curr_cell):
ways += count_moves(pos,hops-1,memo)
memo[key] = ways
return memo[key]
total = 0
memo = {}
for i in range(0,10):
total += count_moves(i,n-1,memo)
return total % (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:
keyboard = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[-1, 0, -1],
]
dp = [[
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[0, 1, 0]
]]
modulo = 10**9+7
for k in range(1, n):
dp.append([[0 for _ in range(3)] for _ in range(4)])
for i in range(4):
for j in range(3):
for (x, y) in [(i+2, j+1),
(i+2, j-1),
(i-2, j+1),
(i-2, j-1),
(i+1, j+2),
(i+1, j-2),
(i-1, j+2),
(i-1, j-2)]:
if 0 <= x < 4 and 0 <= y < 3 and keyboard[x][y] != -1:
dp[k][x][y] = (dp[k][x][y]+dp[k-1][i][j])%modulo
return sum(sum(row) for row in dp[n-1])%modulo
|
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
if(n == 1):
return 10
total = 9
digit = {}
for j in range(0,10):
if(j == 5):
continue
digit[j] = 1
const = 10 ** 9 + 7
for i in range(1,n):
num_1 = (digit[6] + digit[8]) % const
num_2 = (digit[7] + digit[9]) % const
num_3 = (digit[4] + digit[8]) % const
num_4 = (digit[3] + digit[9] + digit[0]) % const
num_6 = (digit[1] + digit[7] + digit[0]) % const
num_7 = (digit[2] + digit[6]) % const
num_8 = (digit[1] + digit[3]) % const
num_9 = (digit[2] + digit[4]) % const
num_0 = (digit[4] + digit[6]) % const
digit[0] = num_0
digit[1] = num_1
digit[2] = num_2
digit[3] = num_3
digit[4] = num_4
digit[6] = num_6
digit[7] = num_7
digit[8] = num_8
digit[9] = num_9
total = (num_0 + num_1 + num_2 + num_3 + num_4 + num_6 + num_7 + num_8 + num_9) % const
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:
path={
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
def paths(i,n):
#number of ways to jump from i n times
if n==1:
return len(path[i])
if n==0:
return 1
s=0
for v in path[i]:
s+=paths(v,n-1)
return s
ans=0
for k in path:
ans+=paths(k,n-1)
return ans%(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
dp = [1]*10
for i in range(N-1):
temp = [0]*10
temp[1], temp[2], temp[3] = dp[6]+dp[8], dp[7]+dp[9], dp[4]+dp[8]
temp[4], temp[5], temp[6] = dp[3]+dp[0]+dp[9], 0, dp[1]+dp[7]+dp[0]
temp[7], temp[8], temp[9] = dp[2]+dp[6], dp[1]+dp[3], dp[2]+dp[4]
temp[0] = dp[4]+dp[6]
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:
D = [[0] * 10 for _ in range(2)]
D[0] = [1] * 10
for i in range(1, N):
idx = i % 2
P = D[(i-1)%2]
D[idx][1] = P[8] + P[6]
D[idx][2] = P[7] + P[9]
D[idx][3] = P[4] + P[8]
D[idx][4] = P[3] + P[9] + P[0]
D[idx][5] = 0
D[idx][6] = P[1] + P[7] + P[0]
D[idx][7] = P[2] + P[6]
D[idx][8] = P[1] + P[3]
D[idx][9] = P[2] + P[4]
D[idx][0] = P[4] + P[6]
return sum(D[(N+1)%2]) % (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
|
mexp = 10 ** 9 + 7
class Solution:
def knightDialer(self, n: int) -> int:
dp = [1 for x in range(10)]
for _ in range(n - 1):
ndp = [0 for x in range(10)]
ndp[0] = (dp[4] + dp[6]) % mexp
ndp[1] = (dp[6] + dp[8]) % mexp
ndp[2] = (dp[7] + dp[9]) % mexp
ndp[3] = (dp[4] + dp[8]) % mexp
ndp[4] = (dp[3] + dp[9] + dp[0]) % mexp
ndp[5] = 0
ndp[6] = (dp[1] + dp[7] + dp[0]) % mexp
ndp[7] = (dp[2] + dp[6]) % mexp
ndp[8] = (dp[1] + dp[3]) % mexp
ndp[9] = (dp[2] + dp[4]) % mexp
dp = ndp
return sum(dp) % mexp
|
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:
mod = 10**9 + 7
M = np.matrix([[0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0]])
res = np.matrix([[1]*10])
N -= 1
while N:
if N % 2 != 0:
res = res * M % mod
N -= 1
else:
M = M * M % mod
N /= 2
return int(np.sum(res)) % 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:
mexp = 10 ** 9 + 7
dp = [1 for x in range(10)]
for i in range(n - 1):
ndp = [0 for x in range(10)]
ndp[0] = (dp[4] + dp[6]) % mexp
ndp[1] = (dp[6] + dp[8]) % mexp
ndp[2] = (dp[7] + dp[9]) % mexp
ndp[3] = (dp[4] + dp[8]) % mexp
ndp[4] = (dp[0] + dp[3] + dp[9]) % mexp
ndp[5] = 0
ndp[6] = (dp[0] + dp[1] + dp[7]) % mexp
ndp[7] = (dp[2] + dp[6]) % mexp
ndp[8] = (dp[1] + dp[3]) % mexp
ndp[9] = (dp[2] + dp[4]) % mexp
dp = ndp
return sum(dp) % mexp
|
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
a_0 = 1
a_1 = 1
a_2 = 1
a_3 = 1
a_4 = 1
a_5 = 0
a_6 = 1
a_7 = 1
a_8 = 1
a_9 = 1
A= [a_0,a_1,a_2,a_3,a_4,a_5,a_6,a_7,a_8,a_9]
def count_next_moves(A):
result =[]
result.append(A[6]+A[4])
result.append(A[6]+A[8])
result.append(A[7]+A[9])
result.append(A[4]+A[8])
result.append(A[3]+A[9]+A[0])
result.append(0)
result.append(A[1]+A[7]+A[0])
result.append(A[6]+A[2])
result.append(A[1]+A[3])
result.append(A[2]+A[4])
return result
for i in range(n-1):
A = count_next_moves(A)
result = (sum(A)%((10**9) + 7))
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:
def knightDialer(self, N: int) -> int:
D = [[0] * 10 for _ in range(N)]
D[0] = [1] * 10
for i in range(1, N):
P = D[i-1]
D[i][1] = P[8] + P[6]
D[i][2] = P[7] + P[9]
D[i][3] = P[4] + P[8]
D[i][4] = P[3] + P[9] + P[0]
D[i][5] = 0
D[i][6] = P[1] + P[7] + P[0]
D[i][7] = P[2] + P[6]
D[i][8] = P[1] + P[3]
D[i][9] = P[2] + P[4]
D[i][0] = P[4] + P[6]
return sum(D[-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:
# if N < 1: return 0
# modnum = 10**9 + 7
# memo = defaultdict(lambda:0)
# def dfs(y, x, cnt):
# nonlocal modnum, memo, N
# if (y,x) == (3,0) or (y,x) == (3,2):
# return 0
# elif cnt == N:
# return 1
# elif (y, x, cnt) in memo:
# return memo[(y, x, cnt)]
# tmpcnt = 0
# for (nxy, nxx) in ((y-1, x-2), (y-2, x-1), (y-2, x+1), (y-1, x+2), (y+1, x+2), (y+2, x+1), (y+2, x-1), (y+1, x-2)):
# if(0<=nxy<4 and 0<=nxx<3):
# tmpcnt += dfs(nxy, nxx, cnt+1)
# tmpcnt %= modnum
# memo[(y, x, cnt)] = tmpcnt
# return memo[(y, x, cnt)]
# return sum(dfs(y,x,1) for y in range(4) for x in range(3))%modnum
#超级快的版本
# self.moves = [(4,6),(6,8),(7,9),(4,8),(3,9,0),(),(1,7,0),(2,6),(1,3),(2,4)]
# return sum(self.dfs(N, i) for i in range(10)) % (10**9+7)
# def dfs(self, N, i, dp={}):
# if N == 1: return 1
# if (N,i) not in dp:
# dp[(N,i)] = sum(self.dfs(N-1, j) for j in self.moves[i])%(10**9+7)
# return dp[(N,i)]
#O(N) solution no dfs
if N == 1: return 10;
# current number of hops that end in index i, skip 5 because it is terminal
ends_in = [0 if i == 5 else 1 for i in range(10)]
mod = 1e9+7
for i in range(N-1):
next_ends_in = [0 for _ in range(10)]
next_ends_in[0] = (ends_in[4] + ends_in[6]) % mod
next_ends_in[1] = (ends_in[6] + ends_in[8]) % mod
next_ends_in[2] = (ends_in[7] + ends_in[9]) % mod
next_ends_in[3] = (ends_in[4] + ends_in[8]) % mod
next_ends_in[4] = (ends_in[3] + ends_in[9] + ends_in[0]) % mod
# skip 5 because we can never get to it
next_ends_in[6] = (ends_in[1] + ends_in[7] + ends_in[0]) % mod
next_ends_in[7] = (ends_in[2] + ends_in[6]) % mod
next_ends_in[8] = (ends_in[1] + ends_in[3]) % mod
next_ends_in[9] = (ends_in[2] + ends_in[4]) % mod
ends_in = next_ends_in
return int(sum(ends_in) % 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
MOD = 10 ** 9 + 7
for i in range(2, n+1):
old_dp = dp.copy()
dp[0] = old_dp[4] + old_dp[6]
dp[1] = old_dp[6] + old_dp[8]
dp[2] = old_dp[7] + old_dp[9]
dp[3] = old_dp[4] + old_dp[8]
dp[4] = old_dp[3] + old_dp[9] + old_dp[0]
dp[5] = 0
dp[6] = old_dp[1] + old_dp[7] + old_dp[0]
dp[7] = old_dp[2] + old_dp[6]
dp[8] = old_dp[1] + old_dp[3]
dp[9] = old_dp[2] + old_dp[4]
ans = sum(dp) % 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:
dialer = {}
for i in range(10):
dialer[i] = 1
for j in range(n):
store = [dialer[k] for k in dialer]
dialer[0] = store[4] + store[6]
dialer[1] = store[8] + store[6]
dialer[2] = store[9] + store[7]
dialer[3] = store[8] + store[4]
dialer[4] = store[0] + store[9] + store[3]
dialer[5] = 0
dialer[6] = store[0] + store[7] + store[1]
dialer[7] = store[2] + store[6]
dialer[8] = store[1] + store[3]
dialer[9] = store[2] + store[4]
return sum(store) % (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
|
import numpy as np
class Solution:
def knightDialer(self, N: int) -> int:
mod = 10**9 + 7
M = np.matrix([[0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0]])
res = np.matrix([[1]*10])
N -= 1
while N:
if N % 2 != 0:
res = res * M % mod
N -= 1
else:
print(M)
M = M * M % mod
print(M)
N /= 2
return int(np.sum(res)) % mod
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.