Datasets:

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: graph = {1:[6, 8], 2:[7, 9], 3:[4, 8], 4:[3, 9, 0], 5:[], 6:[1, 7, 0], 7:[6, 2], 8:[1, 3], 9:[2, 4], 0:[4, 6]} @lru_cache(None) def dfs(node, n=n): if n == 1: return 1 else: moves = 0 for neigb in graph[node]: if neigb in (3, 6, 9): neigb -= 2 moves += dfs(neigb, n-1) return moves ans = 0 for node in range(10): ans += dfs(node) 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: cnt = 0 jump = [[4, 6], [6, 8], [7, 9], [4, 8], [ 0, 3, 9], [], [0, 1, 7], [2, 6], [1, 3], [2, 4]] visited = dict() def helper(n,i): if (n,i) in visited: return visited[(n,i)] if n == 1: visited[(n,i)] = 1 return 1 tot = 0 for v in jump[i]: tot += helper(n -1,v) visited[(n,i)] = tot return tot for i in range(10): cnt += helper(n,i) return cnt%(10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: d = {1:[6,8], 2:[7,9], 3:[4,8], 4:[3,9,0], 5:[], 6:[1,7,0], 7:[2,6], 8:[1,3], 9:[2,4], 0:[4,6]} cache = {} def dfs(l,m): nonlocal d nonlocal cache key = (l,m) if key in cache: return cache[key] if m == n: return 1 ans = 0 for v in d[l]: ans += dfs(v,m+1) cache[key] = ans return ans ret = 0 for i in range(10): ret += dfs(i,1) return ret%(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.paths = {1: [8, 6], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6]} def knightDialer(self, n: int) -> int: # 3 # ways(0, 3) # ways(4, 2) # ways(3, 1) # ways(4, 0) # return 0 # ways(8, 0) # 0 # ways(9, 1) # ways(0, 1) # ways(6, 2) cache = {} # @lru_cache def ways(number, turns): if turns < 2: return turns key = (number, turns) if key in cache: return cache[key] total_ways = 0 for new_number in self.paths[number]: total_ways += ways(new_number, turns-1) cache[key] = total_ways return cache[key] total_ways = 0 # 0 for i in range(10): total_ways += ways(i, n) # return total_ways % (10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: moves = {0: (4,6), 1: (8, 6), 2: (7, 9), 3: (4, 8), 4: (0, 3, 9), 6: (0, 1, 7), 7: (2, 6), 8: (1,3), 9: (2, 4)} @lru_cache(None) def dfs(last_val=None, length=0): if length == n: return 1 count = 0 if not length: for i in range(0,10): count += dfs(i, 1) elif last_val != 5: for i in moves[last_val]: count += dfs(i, length+1) return count return dfs() % ((10**9)+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: MOD = 10**9 + 7 moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] # dp = [1] * 10 # dp[i, N] = dp[nei, N-1] + dp[nei1, N-1] => dp[i] = # for i_n in range(n-1): # dp2 = [0] * 10 # for i in range(10): # for nei in moves[i]: # dp2[nei] += dp[i] # dp2[nei] %= MOD # dp = list(dp2) # return sum(dp) % MOD dp = [[0] * 10 for _ in range(n)] for i in range(10): dp[0][i] = 1 for i_n in range(0, n-1): for i in range(10): for nei in moves[i]: dp[i_n+1][nei] += dp[i_n][i] dp[i_n+1][nei] %= MOD res = 0 for i in range(10): res += dp[n-1][i] res %= 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: 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] } MOD = 10**9 + 7 res = 0 cache = {} for i in range(10): res += self.helper(i,n,neighbors,cache) return res % MOD def helper(self,num,hops,neighbors,cache): if hops == 1: return 1 if (num,hops) in cache: return cache[(num,hops)] seq = 0 for i in neighbors[num]: seq += self.helper(i,hops-1,neighbors,cache) cache[(num,hops)] = seq return seq
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 dp = [1] * 10 while n > 1: new_table = [0] * 10 for digit in range(0,10): for neighbor in self.neighbors(digit): new_table[neighbor] += dp[digit] n -= 1 dp = new_table return sum(dp) % 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
jumps = [ (4, 6), (6, 8), (7, 9), (4, 8), (0, 3, 9), (), (0, 1, 7), (2, 6), (1, 3), (2, 4), ] class Solution: @staticmethod def numNumbersFrom(cache, button, ttl): if ttl == 0: return 0 if ttl == 1: return 1 if button in cache and ttl in cache[button]: return cache[button][ttl] total = 0 for i in jumps[button]: total += int(Solution.numNumbersFrom(cache, i, ttl - 1) % (1e9 + 7)) if button not in cache: cache[button] = {} cache[button][ttl] = total return total def knightDialer(self, n: int) -> int: total = 0 cache = {} for i in range(0, len(jumps)): total += self.numNumbersFrom(cache, i, n) return int(total % (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 __init__(self): self.moves = [ [4,6], [6,8], [7,9], [4,8], [3,9,0], [], [7,1,0], [6,2], [1,3], [4,2] ] self.memoized_moves = {} def knightDialer(self, n: int) -> int: moves = 0 for i in range(10): moves += self.calculateMoves(n-1, i) return moves% (pow(10,9) + 7) def calculateMoves(self, moves_remaining, current_number): if moves_remaining == 0: return 1 moves = 0 for next_number in self.getMoves(current_number): if (moves_remaining, next_number) not in self.memoized_moves: self.memoized_moves[(moves_remaining, next_number)] = self.calculateMoves(moves_remaining-1, next_number) moves += self.memoized_moves[(moves_remaining, next_number)] return moves def getMoves(self, number): return self.moves[number]
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: d = [[4,6], [6, 8], [7, 9], [4, 8], [0, 3, 9], [], [0, 1, 7], [2,6], [1,3], [2, 4]] #d = [(4,6),(6,8),(7,9),(4,8),(3,9,0),(),(1,7,0),(2,6),(1,3),(2,4)] def dfs(self, i, n, cache={}): if n == 0: return 1 if (i, n) not in cache: cache[i, n] = sum(self.dfs(val, n-1) for val in self.d[i]) return cache[i, n] def knightDialer(self, n: int) -> int: mod = int(1e9)+7 return sum(self.dfs(i, n-1) for i in range(10)) % mod # class Solution(object): # moves = [(4,6),(6,8),(7,9),(4,8),(3,9,0),(),(1,7,0),(2,6),(1,3),(2,4)] # def knightDialer(self, N): # return sum(self.knightDialer_(N, i) for i in range(10)) % (10**9 + 7) # def knightDialer_(self, N, i, dp={}): # if N == 1: return 1 # if (N,i) not in dp: # dp[(N,i)] = sum(self.knightDialer_(N-1, j) for j in self.moves[i]) # return dp[(N,i)]
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), } class Solution: def nextPositions(self,n): return NEIGHBORS_MAP[n] 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: 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 nextHops(self, row: int, col: int) -> list: hops = [] if (row, col) not in self.hopDict: for jump in [(-2, -1), (-1, -2), (1, -2), (2, -1), (2, 1), (1, 2),(-1, 2), (-2, 1)]: nr, nc = row + jump[0], col + jump[1] if nr < 0 or nc < 0 or nr > 2 or nc > 2: if (nr == 3 and nc == 1): hops.append((nr, nc)) continue hops.append((nr, nc)) self.hopDict[(row, col)] = hops return self.hopDict[(row, col)] def knightDialer(self, n: int) -> int: if n == 0: return reached = [ [1] * 3 for _ in range(4)] reached[3][0] = 0 reached[3][2] = 0 self.hopDict = {} for move in range(2, n + 1): nreached = [ [0] * 3 for _ in range(4)] for i, row in enumerate(reached): for j, button in enumerate(row): if button == 0: continue for hop in self.nextHops(i, j): nreached[hop[0]][hop[1]] += button nreached[hop[0]][hop[1]] %= (10**9 + 7) reached = nreached return sum(map(sum, reached)) % (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: maxs = (10**9 + 7) moves = ((-1, -2), (-2, -1), (-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2)) memo = {} neighbors = { 1: (6, 8) , 2: (7, 9), 3: (4, 8), 4: (3, 9, 0),5: tuple(), 6: (1, 7, 0), 7: (2, 6), 8: (1, 3), 9: (2, 4), 0: (4, 6),} def dfs(i,n): if n == 1: return 1 if (n,i) in memo: return memo[(n,i)] else: nums = 0 for d in neighbors[i]: nums += dfs(d ,n-1) memo[(n,i)] = nums return nums total = 0 for i in range(10): total += dfs(i,N) return total % maxs
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: moves = {1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6]} # dp [i][j] = distinct phone numbers reachable of length j if you are at number i # 0 <= i <= 9 # 0 <= j <= n dp = [[0 for j in range(n + 1)] for i in range(10)] for j in range(n + 1): for i in range(10): if (j <= 1): dp[i][j] = 1 else: dp[i][j] = sum([dp[possibleMove][j - 1] for possibleMove in moves[i]]) return sum([dp[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
move_map = { 0: {4,6}, 1: {6,8}, 2: {7,9}, 3: {4,8}, 4: {3, 9, 0}, 5: {}, 6: {1, 0, 7}, 7: {2, 6}, 8: {1, 3}, 9: {2, 4} } class Solution: def dfs(self, n, key): if n == 1: return 1 if (n, key) in self.memo: return self.memo[n, key] res = 0 for new_key in move_map[key]: res += self.dfs(n-1, new_key) self.memo[n, key] = res return res def knightDialer(self, n: int) -> int: self.memo = {} res = 0 for key in move_map: res += self.dfs(n, key) 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: s = 0 for k in range(10): d = [0] * 10 d2 = [0] * 10 d[k] = 1 for i in range(n - 1): d2[0] = d[4] + d[6] d2[1] = d[6] + d[8] d2[2] = d[7] + d[9] d2[3] = d[4] + d[8] d2[4] = d[3] + d[9] + d[0] d2[5] = 0 d2[6] = d[1] + d[7] + d[0] d2[7] = d[2] + d[6] d2[8] = d[1] + d[3] d2[9] = 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: def knightDialer(self, N: int) -> int: jump_map = {1:[6,8], 2:[7,9], 3:[4,8], 4:[3,9,0], 5: [], 6: [1,7,0], 7:[6,2], 8:[1,3], 9:[2,4], 0:[4,6]} dp_map = {} def get_num(s, n): if (s,n) not in dp_map: if n == 1: dp_map[(s,n)] = 1 return dp_map[(s,n)] ans = 0 for target in jump_map[s]: ans += get_num(target, n-1) dp_map[(s,n)] = ans return dp_map[(s,n)] total = 0 for i in jump_map.keys(): total += get_num(i, N) 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, curr_n: int, moves_map: dict, cache: dict) -> int: if n == 0: return 1 if (curr_n,n) in cache: return cache[(curr_n,n)] distinct_nums = 0 for next_n in moves_map[curr_n]: distinct_nums += self._knightDialer(n-1, next_n, moves_map, cache) cache[(curr_n, n)] = distinct_nums return cache[(curr_n,n)] def knightDialer(self, n: int) -> int: # TLE, let's take care of some symmetry # 1 == 3 == 9 == 7, 4 == 6, 2 == 8, 0 if n == 1: return 10 mod = 10 ** 9 + 7 nums = 0 cache = {} moves_map = { 1: [6,8], 2: [7,9], 3: [4,8], 4: [0,3,9], 5: [], 6: [0,1,7], 7: [2,6], 8: [1,3], 9: [2,4], 0: [4,6], } nums = 0 for m, weight in [(1,4),(2,2),(4,2), (0,1)]: nums += weight * self._knightDialer(n-1, m, moves_map, cache) return nums % mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def helper(self, keys, n, digit, cache): if not n: return 1 if (n, digit) in cache: return cache[(n,digit)] count = 0 for d in keys[digit]: count += self.helper(keys, n-1, d, cache) cache[(n,digit)] = count return cache[(n,digit)] def knightDialer(self, n: int) -> int: keys = {1:[6,8], 2:[7,9], 3:[4,8],4:[0,3,9], 5: [], 6:[7,1,0], 7:[2,6], 8:[1,3], 9:[2,4], 0:[4,6]} count = 0 cache = {} for i in range(0, 10): count += self.helper(keys, n-1, i, cache) return count % (10**9+7) # def helper()
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: ## dp/ recursion neighbors = { 0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[3,9,0], 5:[], # 5 is a stuck point? also no way to get to 5 6:[1,7,0], 7:[2,6], 8:[1,3], 9:[2,4], } @lru_cache(None) def dfs(i,n): if(n == 1): return 1; count = 0; for x in neighbors[i]: count = (count+dfs(x,n-1))%(10**9+7) return count; ansr = 0; for i in range(10): #print(dfs(i,n)) ansr= (ansr+dfs(i,n))%(10**9+7) return ansr
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
# 当前index位置,下一个可能到达的位置(0 ~ 9) MOVES = [ [4, 6], [6, 8], [7,9], [4, 8], [3, 9, 0], [], [1, 7, 0], [2, 6], [1, 3], [2, 4], ] class Solution: def knightDialer(self, N: int) -> int: memo = {} res = 0 for i in range(10): res += self._dfs(i, N, memo) return res % (10 ** 9 + 7) def _dfs(self, curr, steps, memo): if steps == 1: return 1 key = (curr, steps) if key in memo: return memo[key] memo[key] = 0 for next_digit in MOVES[curr]: memo[key] += self._dfs(next_digit, steps - 1, memo) return memo[key]
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: dic = {0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4:[0, 3, 9], 6:[0, 1, 7], 7:[2, 6], 8:[1, 3], 9: [2, 4]} MOD = 10 ** 9 + 7 cache = {} def dfs(start, digit): key = (start, digit) if key in cache: return cache[key] if digit == 0: return 1 if digit < 0: return 0 ans = 0 if dic.get(start, -1) == -1: return 0 for neigh in dic[start]: ans += dfs(neigh, digit -1) cache[key] = ans return ans count = 0 for i in range(10): count = (count + dfs(i, n-1)) % MOD return 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: def knightDialer(self, n: int) -> int: MOD = 10**9 + 7 M = [[0 for i in range(10)] for j in range(n+1)] # print(M) next_move = {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 paths(pos, nhop): if nhop == 1: return 1 if M[nhop][pos] > 0: return M[nhop][pos]; for j in range(len(next_move[pos])): M[nhop][pos] += paths(next_move[pos][j], nhop-1)% MOD return M[nhop][pos] ans = 0 for start in range(10): ans = (ans + paths(start, n)) % MOD return int(ans);
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: mod = 10**9+7 if n == 1: return 10 dic = {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]} memo = {} def dfs(cur, n): if n == 0: return 1 if (cur, n) in memo: return memo[cur, n] ret = 0 for nxt in dic.get(cur, []): ret += dfs(nxt, n-1) memo[cur, n] = ret return ret ret = 0 for i in range(10): ret += dfs(i, n-1) ret %= mod return ret
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 dfs(self, i, N): if N == 1: return 1 if (i, N) in self.memo: return self.memo[(i, N)] ans = 0 for nxt in self.map[i]: ans += self.dfs(nxt, N - 1) self.memo[(i, N)] = ans return ans def knightDialer(self, n: int) -> int: if n == 0: return 0 if n == 1: return 10 self.map = { 0: [4, 6], 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] } self.memo = {} ans = 0 for i in range(10): if i == 5: continue ans = (ans + self.dfs(i, n)) % (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: # DP 每次要加上那个格的DP值 #{4, 6}, {6, 8}, {7, 9}, {4, 8}, {3, 9, 0}, {}, {1, 7, 0}, {2, 6}, {1, 9}, {4, 2} # 二维数组 表示第i次跳到j上有多少种方法 # 比如到4的时候 那么可能是从3,9,0 来的 那么i,4 = i,4 + i-1 (3,9,0) stack = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],[1,7,0],[2,6],[1,9],[4,2]] dp = [[0 for _ in range(10)] for __ in range(n)] for i in range(10): # 随便放 所有按键都初始为1 因为n是跳的次数 放的不算 dp[0][i] = 1 for i in range(1,n): # 跳 1 到 n-1 次 for j in range(10): for k in stack[j]: dp[i][j] = (dp[i][j] + dp[i-1][k]) % (10**9 + 7) return sum(dp[-1][i] for i in range(10)) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: NEIGHBORS = { 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 numsFromPos(self, num, jumps): jumps_remaining = jumps - 1 if not jumps_remaining: return 1 if (num, jumps) in self.cache: return self.cache[(num, jumps)] res = 0 for nei in self.NEIGHBORS[num]: res += self.numsFromPos(nei, jumps_remaining) self.cache[(num, jumps)] = res return res def knightDialer(self, n: int) -> int: self.cache = {} res = 0 for i in range(10): res += self.numsFromPos(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: x1 = x2 = x3 = x4 = x5 = x6 = x7 = x8 = x9 = x0 = 1 for _ in range(N-1): x1, x2, x3, x4, x5, x6, x7, x8, x9, x0 = x8+x6, x7+x9, x4+x8, x3+x9+x0, 0, x1+x7+x0, x2+x6, x1+x3, x2+x4, x4+x6 return (x1+x2+x3+x4+x5+x6+x7+x8+x9+x0)%(10**9+7) # x1 = x2 = x3 = x4 = x5 = x6 = x7 = x8 = x9 = x0 = 1 # for i in range(N-1): # x1, x2, x3, x4, x5, x6, x7, x8, x9, x0 = x6+x8, x7+x9, x4+x8, x3+x9+x0, 0, x1+x7+x0, x2+x6, x1+x3, x2+x4, x4+x6 # return (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x0)%(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 dp = [1] * 10 while n > 1: new_table = [0] * 10 for digit in range(10): for neighbor in self.getNeighbor(digit): new_table[neighbor] += dp[digit] dp = new_table n -= 1 return sum(dp) % (modulo) # def getWays(self, curr, steps): # if steps == 0: # return 1 # if (curr, steps) in self.cache: # return self.cache[(curr,steps)] # temp = 0 # for digit in self.getNeighbor(curr): # temp += self.getWays(digit,steps-1) # self.cache[(curr,steps)] = temp # return temp def getNeighbor(self, digit): neighbors = { 1: (6,8), 2: (7,9), 3: (4,8), 4: (0,3,9), 5: (), 6: (0,1,7), 7: (2,6), 8: (1,3), 9: (2,4), 0: (4,6) } 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
class Solution: def _knightDialer(self, n: int, curr_n: int, moves_map: dict, cache: dict) -> int: if n == 0: return 1 if (curr_n,n) in cache: return cache[(curr_n,n)] distinct_nums = 0 for next_n in moves_map[curr_n]: distinct_nums += self._knightDialer(n-1, next_n, moves_map, cache) cache[(curr_n, n)] = distinct_nums return cache[(curr_n,n)] def knightDialer(self, n: int) -> int: # TLE, let's take care of some symmetry # 1 == 3 == 9 == 7, 4 == 6, 2 == 8, 0 if n == 1: return 10 mod = 10 ** 9 + 7 nums = 0 cache = {} moves_map = { 1: [6,8], 2: [7,9], 3: [4,8], 4: [0,3,9], 5: [], 6: [0,1,7], 7: [2,6], 8: [1,3], 9: [2,4], 0: [4,6], } for m in [1,2,4,0]: self._knightDialer(n-1, m, moves_map, cache) total = cache[(1, n-1)] * 4 + cache[(4,n-1)] * 2 + cache[(2,n-1)] * 2 + cache[(0,n-1)] return total % mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: 'int') -> 'int': path_graph = {0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[0,3,9],5:[],6:[0,1,7],7:[2,6],8:[1,3],9:[2,4]} mem = {} if N == 1: return 10 def dfs(n, steps): if steps == 1: # return [partial] return len(path_graph[n]) if (n,steps) not in mem: all_paths = 0 for next_num in path_graph[n]: # if next_num not in visited: # visited.add(next_num) all_paths += dfs(next_num, steps-1) mem[(n,steps)] = all_paths return mem[(n, steps)] result = 0 for n in range(10): # visited = set([n]) temp = dfs(n, N-1) # print(temp) result += temp return result%(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 dfs(pos, remain): if remain == 0: return 1 if not (pos, remain) in memo: cur = 0 for np in pos_map[pos]: cur += dfs(np, remain-1) memo[(pos, remain)] = cur return memo[(pos, remain)] res = 0 memo = {} pos_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]} for i in range(10): res += dfs(i, 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 knightDialer(self, n: int) -> int: self.mod_Val = 10**9 + 7 self.map = { 1 : [6,8], 2 : [7,9], 3 : [4,8], 4 : [3,9,0], 5 : [], 6 : [1,7,0], 7 : [2,6], 8 : [1,3], 9 : [2,4], 0 : [4,6], } self.ans = 0 self.mem = collections.defaultdict(int) for i in range(10): self.ans+=self.recursion(n-1, i) return self.ans%self.mod_Val def recursion(self, hops, s): if not hops: return 1 count = 0 if (hops, s) not in self.mem: for n in self.map[s]: count += self.recursion(hops-1, n) self.mem[(hops,s)] = count return self.mem[(hops,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: mem = [[1] for i in range(10)] for i in range(1, n): for cell in range(10): mem[cell].append(self.knightDialerHelper(i, cell, mem)) ans = sum([x[n - 1] for x in mem]) return ans % (10 ** 9 + 7) def knightDialerHelper(self, n, cellNumber, mem): moves = [ [4, 6], [6, 8], [7, 9], [4, 8], [3, 9, 0], [], [1, 7, 0], [2, 6], [1, 3], [2, 4] ] ans = 0 for nextCell in moves[cellNumber]: ans += mem[nextCell][n - 1] 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
# 当前index位置,下一个可能到达的位置(0 ~ 9) MOVES = [ [4, 6], [6, 8], [7,9], [4, 8], [3, 9, 0], [], [1, 7, 0], [2, 6], [1, 3], [2, 4], ] class Solution: def knightDialer(self, N: int) -> int: memo = {} res = 0 for i in range(10): res += self._dfs(i, N - 1, memo) return res % (10 ** 9 + 7) def _dfs(self, curr, steps, memo): if steps == 0: return 1 key = (curr, steps) if key in memo: return memo[key] memo[key] = 0 for next_digit in MOVES[curr]: memo[key] += self._dfs(next_digit, steps - 1, memo) return memo[key]
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: moves=[[4,6],[6,8],[7,9],[4,8],[3,9,0],[],[1,7,0],[2,6],[1,3],[2,4]] mod=10**9+7 dp=[[0 for j in range(10)] for i in range(n)] for i in range(10): dp[0][i]=1 for i in range(1,n): for j in range(10): for nei in moves[j]: dp[i][nei]+=dp[i-1][j] dp[i][nei]=dp[i][nei]%mod res=sum(dp[n-1])%mod return res
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: solution = 0 self.valid_jumps = { 1: [6, 8], 2: [9, 7], 3: [8, 4], 4: [9, 3, 0], 5: [], 6: [7, 1, 0], 7: [2, 6], 8: [1, 3], 9: [4, 2], 0: [4, 6] } self.N = N self.cache = {} solution = 0 for i in range(10): solution += self.jump_around(1, i) return solution % (pow(10, 9) + 7) def jump_around(self, number_len_so_far, last_number): if (number_len_so_far, last_number) in self.cache: return self.cache[(number_len_so_far, last_number)] if number_len_so_far == self.N: return 1 else: solution_for_this_set = 0 possible_jumps = self.valid_jumps[last_number] for possible_jump in possible_jumps: solution_for_this_set += self.jump_around(number_len_so_far + 1, possible_jump) self.cache[(number_len_so_far, last_number)] = solution_for_this_set return solution_for_this_set
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: next_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]} memo = {} def dfs(cur_num,n): if (cur_num,n) in memo.keys(): return memo[(cur_num,n)] if n == 0: return 1 ans = 0 for next_num in next_map[cur_num]: ans += dfs(next_num,n-1) memo[(cur_num,n)] = ans return ans ans = 0 MOD = pow(10,9)+7 memo = {} for i in range(10): ans += dfs(i,n-1)%MOD ans = ans%MOD # print (\"i:{}, ans={}\".format(i,ans)) return ans
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 = 10**9 + 7 current_ways = [1]*10 for i in range(1,n): prev = current_ways.copy() current_ways[0] = (prev[4] + prev[6]) % M current_ways[1] = (prev[6] + prev[8]) % M current_ways[2] = (prev[7] + prev[9]) % M current_ways[3] = (prev[4] + prev[8]) % M current_ways[4] = (prev[3] + prev[9] + prev[0]) % M current_ways[5] = 0 current_ways[6] = (prev[1] + prev[7] + prev[0]) % M current_ways[7] = (prev[2] + prev[6]) % M current_ways[8] = (prev[1] + prev[3]) % M current_ways[9] = (prev[2] + prev[4]) % M return sum(current_ways) % 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: # DP 每次要加上那个格的DP值 #{4, 6}, {6, 8}, {7, 9}, {4, 8}, {3, 9, 0}, {}, {1, 7, 0}, {2, 6}, {1, 9}, {4, 2} # 二维数组 表示第i次跳到j上有多少种方法 # 比如到4的时候 那么可能是从3,9,0 来的 那么i,4 = i,4 + i-1 (3,9,0) stack = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],[1,7,0],[2,6],[1,9],[4,2]] dp = [[0 for _ in range(10)] for __ in range(n)] for i in range(10): dp[0][i] = 1 for i in range(1,n): for j in range(10): for k in stack[j]: dp[i][j] = (dp[i][j] + dp[i-1][k]) % (10**9 + 7) return sum(dp[-1][i] for i in range(10)) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def __init__(self): self.moves = [ [4,6], [6,8], [7,9], [4,8], [3,9,0], [], [7,1,0], [6,2], [1,3], [4,2] ] self.memoized_moves = {} def knightDialer(self, n: int) -> int: if n == 0: return 0 moves = 0 for i in range(10): moves += self.calculateMoves(n-1, i) return moves% (pow(10,9) + 7) def calculateMoves(self, moves_remaining, current_number): if moves_remaining == 0: return 1 moves = 0 for next_number in self.getMoves(current_number): if (moves_remaining, next_number) not in list(self.memoized_moves.keys()): self.memoized_moves[(moves_remaining, next_number)] = self.calculateMoves(moves_remaining-1, next_number) moves += self.memoized_moves[(moves_remaining, next_number)] return moves def getMoves(self, number): return self.moves[number]
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: next_map = {0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[0,3,9],5:[],6:[0,1,7],7:[2,6],8:[1,3],9:[2,4]} def dfs(cur_num,n): if (cur_num,n) in memo.keys(): return memo[(cur_num,n)] if n == 0: return 1 ans = 0 for next_num in next_map[cur_num]: ans += dfs(next_num,n-1) memo[(cur_num,n)] = ans return ans ans = 0 MOD = pow(10,9)+7 memo = {} for i in range(10): ans += dfs(i,n-1)%MOD ans = ans%MOD # print (\"i:{}, ans={}\".format(i,ans)) return ans
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 = {} pos = { 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [3, 1], 9: [2, 4], 0: [6, 4] } res = 0 mod = 10**9 + 7 def recur(i, hops): if (i, hops) in memo: return memo[(i, hops)] if hops == 0: return 1 cnt = 0 for nums in pos[i]: cnt += recur(nums, hops - 1) memo[(i, hops)] = cnt return cnt for i in range(10): res += recur(i, n-1) 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: nbrs = { 1: [6,8], 2: [9,7], 3: [4,8], 4: [3,9,0], 5 : [], 6: [1,7,0], 7: [2,6], 8: [1,3], 9: [2,4], 0: [4,6] } self.memo = {} def find_count(idx,num): if idx == n: return 1 if (num,idx) in self.memo: return self.memo[(num,idx)] count = 0 for nbr in nbrs[num]: count += find_count(idx+1,nbr) self.memo[(num,idx)] = count return count ans = 0 for i in range(10): ans += find_count(1,i) mod = 10 ** 9 + 7 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: moves = {1:[6, 8], 2:[7, 9], 3:[4, 8], 4:[0, 3, 9], 5:[], 6:[0, 1, 7], 7:[2, 6], 8:[1, 3], 9:[2, 4], 0:[4, 6]} from functools import lru_cache @lru_cache(maxsize = None) def dfs(i, length): if n == length: return 1 total = 0 for newI in moves[i]: total += dfs(newI, length + 1) return total ans = 0 for i in range(0, 10): ans += dfs(i, 1) if ans >= 10**9+7: ans = ans%(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: dp = [0 for i in range(12)] num = [1,2,3,4,5,6,7,8,9,11] for j in num: dp[j] = 1 for i in range(1,n): temp = [0 for l in range(12)] for j in num: if(j%3==0): temp[j] += dp[j+5] if j+5 in num else 0 temp[j] += dp[j-5] if j-5 in num else 0 temp[j] += dp[j+1] if j+1 in num else 0 temp[j] += dp[j-7] if j-7 in num else 0 elif(j%3==1): temp[j] += dp[j+5] if j+5 in num else 0 temp[j] += dp[j-5] if j-5 in num else 0 temp[j] += dp[j+7] if j+7 in num else 0 temp[j] += dp[j-1] if j-1 in num else 0 else: temp[j] += dp[j+5] if j+5 in num else 0 temp[j] += dp[j-5] if j-5 in num else 0 temp[j] += dp[j+7] if j+7 in num else 0 temp[j] += dp[j-7] if j-7 in num else 0 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 __init__(self): self.moves = [ [4,6], [6,8], [7,9], [4,8], [3,9,0], [], [7,1,0], [6,2], [1,3], [4,2] ] self.memoized_numbers= {} def knightDialer(self, n: int) -> int: distinctPhoneNumbers = 0 for i in range(10): distinctPhoneNumbers += self.getPhoneNumbers(n-1, i) return(distinctPhoneNumbers%(pow(10,9)+7)) def getPhoneNumbers(self, moves_remaining, current_number): if moves_remaining == 0: return 1 else: moves = 0 for number in self.getMoves(current_number): if (moves_remaining, number) not in list(self.memoized_numbers.keys()): self.memoized_numbers[(moves_remaining, number)] = self.getPhoneNumbers(moves_remaining-1, number) moves += self.memoized_numbers[(moves_remaining, number)] return moves def getMoves(self, move): return self.moves[move]
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
from collections import defaultdict class Solution: def knightDialer(self, N: int) -> int: MOD = 10 ** 9 + 7 board = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [-1, 0, -1]] dp = defaultdict(int) movesC = {} def moves(i, j): valids = set() for u,v in [(i-1, j + 2), (i + 1, j + 2), (i - 1,j-2), (i+1, j-2), (i-2, j +1), (i-2,j-1), (i+2, j+1), (i + 2, j-1)]: if u < 0 or v < 0 or u >= len(board) or v >= 3: continue if board[u][v] != -1: valids.add((u,v)) return valids for i in range(len(board)): for j in range(3): if board[i][j] != -1: movesC[(i, j)] = moves(i, j) total = 0 for n in range(1, N+1): for i in range(len(board)): for j in range(len(board[i])): num = board[i][j] if num != -1: key = (i, j, n) if n == 1: dp[key] = 1 else: for u, v in movesC[(i, j)]: dp[key] += dp[(u, v, n - 1)] % MOD if n == N: total += dp[key] % MOD return total % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: MOD = 10**9 + 7 poss = {} poss[1] = [6,8] poss[2] = [7,9] poss[3] = [4,8] poss[4] = [3,9,0] poss[5] = [] poss[6] = [1,7,0] poss[7] = [2,6] poss[8] = [1,3] poss[9] = [2,4] poss[0] = [4,6] def helper(numb,i): # print('numb is ', numb) # print('at i ',i) if i == n: return 1 if (numb,i) in mem: return mem[(numb,i)] out= 0 child = poss[numb] nchild = len(child) for j in range(nchild): val = child[j] out += helper(val,i+1) out = out % MOD mem[(numb,i)] = out return out res = 0 mem = {} for i in range(10): # print('starting at ',i) res += helper(i,1) 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
from collections import defaultdict class Solution: def knightDialer(self, N: int) -> int: board = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [-1, 0, -1]] dp = defaultdict(int) movesC = {} def moves(i, j): valids = set() for u,v in [(i-1, j + 2), (i + 1, j + 2), (i - 1,j-2), (i+1, j-2), (i-2, j +1), (i-2,j-1), (i+2, j+1), (i + 2, j-1)]: if u < 0 or v < 0 or u >= len(board) or v >= 3: continue if board[u][v] != -1: valids.add((u,v)) return valids for i in range(len(board)): for j in range(3): if board[i][j] != -1: movesC[(i, j)] = moves(i, j) total = 0 for n in range(1, N+1): for i in range(len(board)): for j in range(len(board[i])): num = board[i][j] if num != -1: key = (i, j, n) if n == 1: dp[key] = 1 else: for u, v in movesC[(i, j)]: dp[key] += dp[(u, v, n - 1)] if n == N: total += dp[key] 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
from collections import deque class Solution: def __init__(self): self.next_move = [(4,6), (6, 8), (7,9), (4, 8), (3,0,9), (), (1, 7,0), (6, 2), (3,1), (2, 4)] self.cache = {} def dfs(self, start, n): if n == 0: return 1 if not self.cache.get((start, n)): total_numbers = 0 for i in self.next_move[start]: total_numbers += self.dfs(i, n-1) self.cache[(start,n)] = total_numbers return self.cache[(start,n)] def knightDialer(self, n: int) -> int: return sum([self.dfs(i, n-1) for i in range(10)]) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: dp = [0 for i in range(12)] num = [1,2,3,4,5,6,7,8,9,11] for i in range(n): temp = [0 for l in range(12)] for j in num: if(i==0): temp[j] = 1 else: temp[j] += dp[j+5] if j+5 in num else 0 temp[j] += dp[j-5] if j-5 in num else 0 if(j%3==0): temp[j] += dp[j+1] if j+1 in num else 0 temp[j] += dp[j-7] if j-7 in num else 0 elif(j%3==1): temp[j] += dp[j+7] if j+7 in num else 0 temp[j] += dp[j-1] if j-1 in num else 0 else: temp[j] += dp[j+7] if j+7 in num else 0 temp[j] += dp[j-7] if j-7 in num else 0 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 __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_current_number_and_remaining_moves = {} def getNextMoves(self, current_number): return self.moves[current_number] def knightDialer(self, n: int) -> int: total_moves = 0 for number in range(10): total_moves += self.knightDialerOneNumber(number, n-1) return total_moves % (pow(10,9) +7) def knightDialerOneNumber(self, current_number, moves_left): if moves_left == 0: return 1 moves = 0 for move in self.getNextMoves(current_number): if (move, moves_left) in list(self.memoized_current_number_and_remaining_moves.keys()): moves += self.memoized_current_number_and_remaining_moves[(move, moves_left)] else: self.memoized_current_number_and_remaining_moves[(move, moves_left)] = self.knightDialerOneNumber(move, moves_left -1) moves += self.memoized_current_number_and_remaining_moves[(move, moves_left)] 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
from functools import lru_cache class Solution: def knightDialer(self, n: int) -> int: ## dp/ recursion neighbors = { 0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[3,9,0], 5:[], # 5 is a stuck point? also no way to get to 5 6:[1,7,0], 7:[2,6], 8:[1,3], 9:[2,4], } #lru_cache is needed @lru_cache(None) def dfs(i,n): if(n == 1): return 1; count = 0; for x in neighbors[i]: count = (count+dfs(x,n-1))%(10**9+7) return count; ansr = 0; for i in range(10): #print(dfs(i,n)) ansr= (ansr+dfs(i,n))%(10**9+7) return ansr
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: valid_jumps = dict() valid_jumps[0] = [4,6] valid_jumps[1] = [6,8] valid_jumps[2] = [7,9] valid_jumps[3] = [4,8] valid_jumps[4] = [0,3,9] valid_jumps[5] = [] valid_jumps[6] = [0,1,7] valid_jumps[7] = [2,6] valid_jumps[8] = [1,3] valid_jumps[9] = [2,4] cache = dict() def knightDialer(self, n: int) -> int: if n == 1: return 10; agg = 0 for idx in range(10): if idx != 5: agg += self.knightDialerHelper(idx, n-1) return agg % int(1e9 + 7) def knightDialerHelper(self, start, n): if n == 1: return len(self.valid_jumps[start]) if (start, n) in self.cache.keys(): return self.cache[(start,n)] numbers = 0 for k in self.valid_jumps[start]: numbers += self.knightDialerHelper(k, n-1) self.cache[(start,n)] = numbers return numbers
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 for x in range(10)] # jumpMap = {1: [6, 8], 2: [7, 9], 3: [8, 4], 4: [9, 3, 0]: 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [4, 2], 0: [4, 6]} for index in range(n - 1): newDp = [dp[4] + dp[6], dp[6] + dp[8], dp[7] + dp[9], dp[8] + dp[4], dp[9] + dp[3] + dp[0], 0, dp[1] + dp[7] + dp[0], dp[2] + dp[6], dp[1] + dp[3], dp[4] + dp[2]] dp = newDp 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
from collections import defaultdict class Solution: def knightDialer(self, n: int) -> int: # the graph G = {1: [6, 8], 2: [7, 9], 3: [4, 8], 6: [1, 7, 0], 5: [], 4: [3, 9, 0], 7: [2,6], 8: [1,3], 9: [2,4], 0: [4,6]} # keys are 0,1,2....10 # values are dict #. keys are number of movement from this point # how many different combination exist dp = defaultdict(lambda: defaultdict(lambda: 0)) def dfs(number, numberOfMovements): if numberOfMovements == 0: return 1 if dp[number][numberOfMovements]: return dp[number][numberOfMovements] total = 0 for i in G[number]: total += dfs(i, numberOfMovements-1) dp[number][numberOfMovements] = total return total res = 0 for i in range(10): res += dfs(i, n-1) res = res % 1000000007 return res
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: dc = {0:(4,6),1:(6,8),2:(7,9),3:(4,8),4:(3,9,0),5:(),6:(0,7,1),7:(2,6),8:(1,3),9:(2,4)} dp = [[0]*10 for i in range(n+1)] for i in range(10): dp[1][i]=1 for i in range(2,n+1): for j in range(10): for k in range(10): if j in dc[k]: dp[i][j]+=dp[i-1][k] return sum(dp[n])%(10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
import numpy as np class Solution: def knightDialer(self, n: int) -> int: ans = np.ones((10,),dtype=int) table = {0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9], 6:[0,1,7], 7:[2,6], 8:[1,3], 9:[4,2] } for i in range(n-1): tempans = np.zeros((10,),dtype=int) for j in range(10): if j!=5: values = table.get(j) for value in values: tempans[value] += ans[j] ans = tempans if(sum(ans)>10**9+7): ans = ans%(10**9+7) return sum(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: moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] sol = [] cache = {} def backtrack(pos, hops, cur): if (pos, hops) in cache: return cache[(pos, hops)] if hops == 0: return 1 count = 0 for nbr in moves[pos]: cur.append(nbr) count += backtrack(nbr, hops-1, cur) cur.pop() cache[(pos, hops)] = count return count counts = 0 for pos in range(10): counts += backtrack(pos, n-1, []) return counts%(10**9+7) # cur_hops = 1 # prev_counts = [1]*10 # cur_counts = [0]*10 # while cur_hops < n: # cur_hops += 1 # cur_counts = [0]*10 # for pos in range(10): # for nbr in moves[pos]: # cur_counts[pos] += prev_counts[nbr] # prev_counts = cur_counts # return sum(prev_counts)%(10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
from collections import defaultdict class Solution: def knightDialer(self, n: int) -> int: self.buildPad() self.buildMoves() self.memo = defaultdict(lambda: -1) numbers = sum(self.visit(start, n-1) for start in range(10)) return numbers % (10**9 + 7) def visit(self, start: int, n: int) -> int: if n == 0: return 1 elif self.memo[(start, n)] != -1: return self.memo[(start, n)] numbers = 0 for new_start in self.moves[start]: numbers += self.visit(new_start, n-1) self.memo[(start, n)] = numbers return numbers def buildPad(self) -> None: self.pad = [[str(i + j) for j in range(3)] for i in range(1, 10, 3)] self.pad.append(['*', '0', '#']) def buildMoves(self) -> None: self.moves = ( (4, 6), (6, 8), (7, 9), (4, 8), (0, 3, 9), (), (0, 1, 7), (2, 6), (1, 3), (2, 4) )
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 MOD = 10 ** 9 + 7 g = [[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[0,1,7],[2,6],[1,3],[2,4]] dp = [[None] * 10 for _ in range(n+1)] # kd(n,start) = sum(kd(n-1,nxtstart) for nxtstart in g[start])) # kd(1,start) = 1 def kd(n, start): if n == 1: return 1 if dp[n][start] is None: dp[n][start] = sum([kd(n-1, nxt) for nxt in g[start]]) return dp[n][start] return sum(kd(n, start) for start in range(10)) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: g = {0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[3,9,0], 5:[], 6:[1,7,0], 7:[2,6], 8:[1,3], 9:[4,2]} A = [[1 if i in g[j] else 0 for i in range(10)] for j in range(10)] v = [1]*10 for _ in range(N-1): res = [0]*10 for r in range(10): for c in range(10): if A[r][c]: res[c] += v[r] v = res return sum(v) % (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.moves = [ [4,6], [6,8], [7,9], [4,8], [3,9,0], [], [7,1,0], [6,2], [1,3], [4,2] ] self.memoized_numbers= {} def knightDialer(self, n: int) -> int: distinctPhoneNumbers = 0 for i in range(10): distinctPhoneNumbers += self.getPhoneNumbers(n-1, i) return(distinctPhoneNumbers% (pow(10,9) +7)) def getPhoneNumbers(self, moves_remaining, current_number): if moves_remaining == 0: return 1 moves = 0 for number in self.getMoves(current_number): if (moves_remaining-1, number) not in list(self.memoized_numbers.keys()): self.memoized_numbers[(moves_remaining-1, number)] = self.getPhoneNumbers(moves_remaining-1, number) moves += self.memoized_numbers[(moves_remaining-1, number)] return moves def getMoves(self, move): return self.moves[move]
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: adj = {1:[6,8], 2:[7,9], 3:[8,4], 4:[0,9,3], 5:[], 6:[7,1,0], 7:[6,2], 8:[3,1], 9:[4,2], 0:[6,4]} res = 0 memo = collections.defaultdict(int) for i in range(10): res = (res + self.top_down(memo, adj, n-1, i))%(10**9+7) return res def top_down(self, memo, adj, n, cur): if n == 0: return 1 if (cur, n) not in memo: for nb in adj[cur]: memo[(cur, n)] += self.top_down(memo, adj, n-1, nb)%(10**9+7) return memo[(cur, n)] # jump = {1:[6,8], 2:[7,9], 3:[8,4], 4:[0,9,3], 5:[], 6:[7,1,0], 7:[6,2], 8:[3,1], 9:[4,2], 0:[6,4]} # # dp = [1]*10 # # for _ in range(n-1): # # nxt = [0]*10 # # for i in range(10): # # for j in jump[i]: # # nxt[j] += dp[i] # # dp = nxt # # return sum(dp)%(10**9+7) # def count_paths(self, start, n, adj): # if not n: return 1 # dp = {v:1 for v in adj.keys()} # return self.helper([start], n, adj) # def helper(self, cur, n, adj): # if not n: # return len(cur) # nxt = [] # for u in cur: # nxt.extend(adj[u]) # return self.helper(nxt, n-1, adj)
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 pos(self, idx): return {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]}[idx] def knightDialer(self, n: int) -> int: if n == 1: return 10 memo = [[0 for _ in range(n + 1)] for i in range(10)] for i in range(10): memo[i][1] = 1 for s in range(2, n + 1): for idx in range(10): for p in self.pos(idx): memo[idx][s] += memo[p][s - 1] return sum((memo[i][n] for i in range(10))) % (10 ** 9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: jumps = [[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[0,1,7],[2,6],[1,3],[2,4]] mod = 10**9 + 7 memo = {} def helper(n, starting, memo): if (n,starting) in memo: return memo[(n,starting)] if n == 1: return 1 if n == 2: return len(jumps[starting]) sum = 0 for jump_location in jumps[starting]: sum += helper(n-1, jump_location, memo) memo[(n,starting)] = sum return sum total_jumps = 0 for i in range(10): if i == 5 and n != 1: continue total_jumps += helper(n, i, memo) return total_jumps % 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: dc = {0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[3,9,0],5:[],6:[0,7,1],7:[2,6],8:[1,3],9:[2,4]} dp = [[0]*10 for i in range(n+1)] for i in range(10): dp[1][i]=1 for i in range(2,n+1): for j in range(10): for k in range(10): if j in dc[k]: dp[i][j]+=dp[i-1][k] return sum(dp[n])%(10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: path = [(4, 6), (6, 8), (7, 9), (4, 8), (0, 3, 9), (), (0, 1, 7), (2, 6), (1, 3), (2, 4)] memo = [] for i in range(n+1): memo.append({}) for j in range(10): memo[i][j] = -1 def dp(num, steps): if steps == 1: return 1 if memo[steps][num] != -1: return memo[steps][num] ret = 0 for i in path[num]: ret += dp(i, steps-1) memo[steps][num] = ret return ret % 1000000007 res = 0 for num in range(10): res += dp(num, n) res %= 1000000007 return res
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
import numpy as np class Solution: # Time: O(N), Space: O(N). def knightDialer(self, N: int) -> int: goto = { 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], } # We can build backwards... # With x moves left how many numbers numbers can you form from i. # For x = 0, 1. # Shape (10, N). # dp[p, i] = on position p, with i move lefts, how many moves # can the knight make. dp = np.zeros((10, N)).astype(int) # With 0 moves left you can only make 1 number. dp[:, 0] = 1 for c in range(1, N): for p in range(10): for next_vals in goto[p]: dp[p, c] += dp[next_vals, c - 1] % (10**9 + 7) return int(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: myMap = {1: [8, 6], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6], -1: list(range(10))} cache = {} def dfs(start, level): if level == 0: return 1 key = str(start) + str(level) if key in cache: return cache[key] else: ans = 0 for number in myMap[start]: ans += dfs(number, level - 1) cache[key] = ans return ans total = dfs(-1, N) 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: 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), } mx = 10**9 + 7 memo = {} def dp(n, curr_num): if (n, curr_num) in memo: return memo[(n, curr_num)] if n == N - 1: return 1 comb = 0 for neighbor in NEIGHBORS_MAP[curr_num]: comb += dp(n + 1, neighbor) memo[(n, curr_num)] = comb return comb res = 0 for i in range(0, 10): res += dp(0, i) return res % mx
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 c = [1]*10 for _ in range(n-1): c = [ c[4]+c[6], c[6]+c[8], c[7]+c[9], c[4]+c[8], c[0]+c[3]+c[9], 0, c[0]+c[7]+c[1], c[2]+c[6], c[1]+c[3], c[2]+c[4] ] return sum(c)%mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: next = {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), } M = 1000000007 # cache = {} cache = defaultdict(int) def helper(m, n): if (m, n) not in cache: # cache[m,n] = [] if n == 1: cache[m,n] = 1 else: for nn in next[m]: # for e in helper(nn, n-1): # cache[m,n].append([m] + e) cache[m,n] += helper(nn, n-1) cache[m,n] %= M return cache[m,n] # res = [] # for i in range(10): # res.extend(helper(i, n)) # return res return sum(helper(i, n) for i in range(10)) % 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: if n==1: return 10 hash_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)} dp = {} def ways_n(n,val): if n==0: return 1 if (n,val) in dp: return dp[n,val] dp[n,val] = 0 for i in hash_map[val]: dp[n,val] += ways_n(n-1,i) return dp[n,val] ans = 0 for i in range(10): ans+=ways_n(n-1,i) 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 getAccessible(self, key): switch = { 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] } return switch.setdefault(key, []) def knightDialer(self, n: int) -> int: dp = [[0]*10 for _ in range(n)] dp[0] = [1]*10 for r in range(1,len(dp)): for c in range(len(dp[0])): neighbors = self.getAccessible(c) for n in neighbors: dp[r][c] = dp[r-1][n] + (dp[r][c] % (10**9 + 7)) return sum(dp[-1]) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: possible_ways = {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]} remember_dict = {} def numofways(N, startnum): if (startnum,N) in remember_dict: return remember_dict[(startnum,N)] if N == 1: return 1 else: temp = 0 for nextnum in possible_ways[startnum]: temp += numofways(N-1,nextnum) remember_dict[(startnum,N)] = temp return temp summation = 0 for i in range(10): summation += numofways(N, i) return summation % 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: cache = [[0 for i in range(n+1)] for i in range(10)] for i in range(10): cache[i][1] = 1 return self.hopper(n, cache) % (10 ** 9 + 7) def hopper(self, hops_left, cache): if hops_left == 1: return sum([cache[i][1] for i in range(10)]) elif hops_left < 1: return 0 else: for i in range(2, hops_left+1): for j in range(10): for nbr in self.nbrs(j): cache[j][i] += cache[nbr][i-1] return sum([cache[i][hops_left] for i in range(10)]) 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
class Solution: def knightDialer(self, n: int) -> int: moves = { 0: [4, 6], 2: [7, 9], 1: [6, 8], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4] } store = {} def f(num, rem): if rem == 1: return 1 if (num, rem) in store: return store[(num, rem)] res = store[(num, rem)] = sum([f(m, rem - 1) for m in moves[num]]) return res return sum([f(num, n) for num 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: hop_map = {1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9], 5:[], 6:[0,1,7], 7:[2,6], 8:[1,3], 9:[2,4], 0:[4,6]} memo = {(i,1): 1 for i in range(10)} def num_hops(key:int, N:int): if N == 1: return memo[(key, 1)] if (key, N) in memo: return memo[(key, N)] sum_ = 0 for v in hop_map[key]: sum_ += num_hops(v, N-1) memo[(key, N)] = sum_ % (10**9 + 7) return sum_ sum_ = 0 for i in range(10): sum_ += num_hops(i, N) % (10**9 + 7) return sum_ % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: arr = None def knightDialer(self, n: int) -> int: self.init_arr(n) if n == 1: return 10 total = 0 for j in range(2, n+1): for i in range(10): self.dialStart(i, j) return self.get_ans(n) def get_ans(self, n): total = 0 for i in range(10): total = (total + self.arr[i][n]) % 1000000007 return total def init_arr(self, n): self.arr = [[0]*(n+1) for i in range(10)] for i in range(10): self.arr[i][1] = 1 def dialStart(self, start: int, n: int): neighbours = self.get_neighbours() total = 0 for ne in neighbours[start]: total = (total + self.arr[ne][n-1]) % 1000000007 self.arr[start][n] = total def get_neighbours(self): return { 0: [4, 6], 1: [8, 6], 2: [7, 9], 3: [8, 4], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4], }
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: move = [1]*10 for i in range(n-1): new_move = [1]*10 new_move[0], new_move[1] = (move[4]+move[6])%(10**9 + 7), (move[8]+move[6])%(10**9 + 7) new_move[2], new_move[3] = (move[7]+move[9])%(10**9 + 7), (move[8]+move[4])%(10**9 + 7) new_move[4], new_move[5] = (move[0]+move[3]+move[9])%(10**9 + 7), 0 new_move[6], new_move[7] = (move[0]+move[1]+move[7])%(10**9 + 7), (move[2]+move[6])%(10**9 + 7) new_move[8], new_move[9] = (move[1]+move[3])%(10**9 + 7), (move[2]+move[4])%(10**9 + 7) move = new_move return sum(move) % (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 navigation = { 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], } memo = {} def get_num_distinct_dials(last_digit, N_left): if N_left == 0: return 1 memo_key = (last_digit, N_left) if memo_key in memo: return memo[memo_key] result = sum([get_num_distinct_dials(next_digit, N_left - 1) for next_digit in navigation[last_digit]]) memo[memo_key] = result return result return sum([get_num_distinct_dials(first_digit, N - 1) for first_digit in navigation.keys()]) % (10 ** 9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: moves = { 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] } dp1 = [1] * 10 for i in range(n - 1): # do n - 1 times dp2 = [0] * 10 for digit in range(10): for neib in moves[digit]: dp2[digit] += dp1[neib] dp2[digit] %= pow(10, 9) + 7 dp1 = dp2 return (sum(dp1)) % (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
MAX = 10**9 + 7 d = [ [4, 6], [6, 8], [7, 9], [4, 8], [3, 9, 0], [], [1, 7, 0], [2, 6], [1, 3], [2, 4], [4, 6] ] class Solution: def __init__(self): self.memo = {} def knightDialer(self, n: int) -> int: return sum([self.helper(n, p) for p in range(10)]) % MAX def helper(self, n: int, p: int) -> int: if (n, p) in self.memo: return self.memo[(n, p)] if n == 1: return 1 r = sum([self.helper(n - 1, q) for q in d[p]]) self.memo[(n, p)] = r return r
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
MAX = 10**9 + 7 d = { 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6] } class Solution: def __init__(self): self.memo = {} def knightDialer(self, n: int) -> int: return sum([self.helper(n, p) for p in d.keys()]) % MAX def helper(self, n: int, p: int) -> int: if (n, p) in self.memo: return self.memo[(n, p)] if n == 1: return 1 r = sum([self.helper(n - 1, q) for q in d[p]]) self.memo[(n, p)] = r return r
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: graph = {0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[0,9,3],5:[],6:[0,7,1],7:[2,6],8:[1,3],9:[4,2]} ans = 0 self.cache = {} for u in graph.keys(): ans += self.dial(graph,u,n-1) return ans % (10**9 + 7) def dial(self,graph,start,n): if n == 0: return 1 ans = 0 key = str(start) + ' ' + str(n) if key in self.cache.keys(): return self.cache[key] for v in graph[start]: ans += self.dial(graph,v,n-1) self.cache[key] = ans return self.cache[key]
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: graph = {0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[0,9,3],5:[],6:[0,7,1],7:[2,6],8:[1,3],9:[4,2]} ans = 0 self.cache = {} for u in list(graph.keys()): ans += self.dial(graph,u,n-1) return ans % (10**9 + 7) def dial(self,graph,start,n): if n == 0: return 1 ans = 0 key = str(start) + ' ' + str(n) if key in list(self.cache.keys()): return self.cache[key] for v in graph[start]: ans += self.dial(graph,v,n-1) self.cache[key] = ans return self.cache[key] # pkey = str(i) + '-' + str(j) + '-' + str(n) # if pkey in self.dp.keys(): # return self.dp[pkey] # for dx,dy in self.dirs: # x = i + dx # y = j + dy # temp = 0 # if 0 <= x <= self.m -1 and 0 <= y <= self.n-1 and self.numpad[x][y] != -1: # key = str(x) + '-' + str(y) + '-' + str(n-1) # if key in self.dp.keys(): # temp = self.dp[key] # else: # temp = self.solve(x,y,n-1) # self.dp[key] = temp # ans += temp # self.dp[pkey] = 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: # pad = [['1','2','3'],['4','5','6'],['7','8','9'],['*','0','#']] graph = {0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[0,9,3],5:[],6:[0,7,1],7:[2,6],8:[1,3],9:[4,2]} # self.dirs = [(1,2),(2,1),(-1,2),(-2,1),(-1,-2),(-2,-1),(2,-1),(1,-2)] ans = 0 self.cache = {} for u in list(graph.keys()): ans += self.dial(graph,u,n-1) return ans % (10**9 + 7) def dial(self,graph,start,n): if n == 0: return 1 ans = 0 key = str(start) + ' ' + str(n) if key in list(self.cache.keys()): return self.cache[key] for v in graph[start]: ans += self.dial(graph,v,n-1) self.cache[key] = ans return self.cache[key]
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_num = 10 ** 9 + 7 moves = [(-2, -1), (-1, -2), (1, -2), (2, -1), (-2, 1), (-1, 2), (1, 2), (2, 1)] cells = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2), (3, 1)] cell2idx = {} for i, cell in enumerate(cells): cell2idx[cell] = i # number of sequences end at cell sequences = [1 for _ in cells] for step in range(n - 1): new_sequences = [0 for _ in cells] for i, (r, c) in enumerate(cells): for rm, cm in moves: newr = r + rm newc = c + cm if (newr, newc) in cell2idx: new_idx = cell2idx[(newr, newc)] new_sequences[new_idx] += sequences[i] sequences = [cnt % mod_num for cnt in new_sequences] return sum(sequences) % mod_num
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: myMap = {1: [8, 6], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6], -1: list(range(10))} cache = {} def dfs(start, level): if level == 0: return 1 key = str(start) + str(level) ans = 0 for number in myMap[start]: nextK = str(number) + str(level-1) if nextK in cache: ans += cache[nextK] else: ans += dfs(number, level - 1) cache[key] = ans return ans total = dfs(-1, N) 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: if n == 1: return 10 move = [1]*10 for i in range(n-1): new_move = [1]*10 new_move[0], new_move[1] = (move[4]+move[6])%(10**9 + 7), (move[8]+move[6])%(10**9 + 7) new_move[2], new_move[3] = (move[7]+move[9])%(10**9 + 7), (move[8]+move[4])%(10**9 + 7) new_move[4], new_move[5] = (move[0]+move[3]+move[9])%(10**9 + 7), 0 new_move[6], new_move[7] = (move[0]+move[1]+move[7])%(10**9 + 7), (move[2]+move[6])%(10**9 + 7) new_move[8], new_move[9] = (move[1]+move[3])%(10**9 + 7), (move[2]+move[4])%(10**9 + 7) move = new_move return sum(move) % (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: choices = { 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] } self.memo = dict() def dfs(curr, n): key = (curr, n) if key in self.memo: return self.memo[key] if n == 1: return 1 sub = 0 for next in choices[curr]: sub += dfs(next, n - 1) self.memo[key] = sub return sub res = 0 for s in range(10): res += dfs(s, 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: space = {1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9], 5:[], 6:[0,7,1], 7:[2,6],8:[1,3],9:[4,2],0:[4,6]} memory = {} if n < 1: return 0 def recurse(number, n): if n < 2: return 1 key = str([number,n]) if key in memory: return memory[key] moves = 0 for i in space[number]: moves+= recurse(i,n-1) memory[key] = moves return moves s = 0 for number in space: s+= recurse(number, n) 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: def knightDialer(self, N: int) -> int: if N == 0: return 0 d = {} choices = [ [4, 6], [6, 8], [7, 9], [4, 8], [0, 3, 9], [], [0, 1, 7], [2, 6], [1, 3], [2, 4] ] def dp(k, n): if n == 0: return 1 if n == 1: return len(choices[k]) if (k, n) not in d: d[(k, n)] = sum([dp(nxt, n-1) for nxt in choices[k]]) return d[(k, n)] return sum([dp(v, N-1) for v 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: 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 == 1: return 1 elif num_jumps == 2: 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, 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
from functools import lru_cache class Solution: def knightDialer(self, n: int) -> int: to = { 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(maxsize=None) def dfs(i: int, k: int) -> int: if i not in to: return 0 if k == 1: return 1 return sum([dfs(t, k-1) for t in to[i]]) return sum([dfs(i, n) for i in range(10)]) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: mod= (10**9)+7 def getNeighbors(N): neighs=[[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[0,1,7],[2,6],[1,3],[2,4]] return neighs[N] def getCount(start,n): if (start,n) not in dic: if n==0: dic[(start,n)]=1 else: count=0 for nei in getNeighbors(start): count+=getCount(nei,n-1) dic[(start,n)]=count return dic[(start,n)] dic={} val=0 for i in range(10): val+=getCount(i,N-1) return val%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
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=None) 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) answer %= 10 ** 9 + 7 return answer % (10 ** 9 + 7)