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: # dp[i][move] = [dp[neighbor][move - 1]for neighbor in neighbors] 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]} if n == 0: return 0 dp = [[1 for i in range(n)] for i in range(10)] for move in range(1,n): for num in range(10): dp[num][move] = sum([dp[neighbor][move - 1] for neighbor in neighbors[num]]) % 1000000007 return sum([dp[num][n-1] for num in range(10)]) % 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: w = [1] x = [1] y = [1] z = [1] for i in range(n-1): w_n = 2*x[-1] x_n = w[-1] + 2*y[-1] y_n = x[-1] + z[-1] z_n = 2*y[-1] w.append(w_n) x.append(x_n) y.append(y_n) z.append(z_n) return (w[-1] + 2*x[-1] + 4*y[-1] + 2*z[-1] + (1 if n == 1 else 0)) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: path={ 0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9], 5:[], 6:[0,1,7], 7:[2,6], 8:[1,3], 9:[2,4] } M=10**9+7 dp=[[None for _ in range(n) ] for _ in range(10)] def paths(i,n): if dp[i][n]: return dp[i][n] #number of ways to jump from i n times if n==1: return len(path[i]) if n==0: return 1 s=0 for v in path[i]: s+=paths(v,n-1) dp[i][n]=s%M return dp[i][n] ans=0 for k in path: ans+=paths(k,n-1) return ans%(M)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: h = { 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] } if n == 1: return 10 o = [] le = 0 oTrack = [0] * 10 track = [0] * 10 for loops in range(n - 1): track = [0] * 10 for k,v in enumerate(track): #print(\"k: \", k, track) hg = h.get(k) for i in hg: track[i] += max(oTrack[k], 1) #print(\"loop: \", loops, p, track, oTrack) oTrack = track.copy() return sum(track) % (10 ** 9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: MOD = 10**9 + 7 moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] dp = [1] * 10 for hops in range(n-1): dp2 = [0] * 10 for node, count in enumerate(dp): for nei in moves[node]: dp2[nei] += count dp2[nei] %= MOD dp = dp2 return sum(dp) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: prior_case = [1]*10 current_case = [1]*10 paths = {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]} curr_hop = 1 while curr_hop < n: curr_hop += 1 current_case = [0]*10 for num in range(10): for neighbor in paths[num]: current_case[num] += prior_case[neighbor] prior_case = current_case return sum(current_case) % (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: mm = { 1: [6, 8], 2: [7, 9], 3: [8, 4], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [6, 4] } ans = 0 @lru_cache(maxsize=None) def dfs(val, rem): if rem == 0: return 1 res = 0 for adj in mm[val]: res += dfs(adj, rem - 1) return res for key in list(mm.keys()): ans += dfs(key, n - 1) return ans % 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: hop_map = {1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9], 6:[0,1,7], 7:[2,6], 8:[1,3], 9:[2,4], 0:[4,6]} memo = {(i,1): 1 for i in range(10)} def num_hops(key:int, N:int): if N == 1: return 1 if key in [1,4,7]: key += 2 elif key == 5: return 0 if (key, N) in memo: return memo[(key, N)] sum_ = 0 for v in hop_map[key]: sum_ += num_hops(v, N-1) memo[(key, N)] = sum_ return sum_ sum_ = 0 for i in range(10): sum_ += num_hops(i, N) 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: def knightDialer(self, n: int) -> int: if n == 0 or not n: return 0 graph = {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]} @lru_cache(maxsize=None) def helper(left, curr): if left == 1: return 1 cnt = 0 for nei in graph[curr]: cnt += helper(left-1, nei) return cnt #n = 3 ans = 0 for i in range(10): ans += helper(n, 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 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] } count = 0 num_each_digit = { 0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1 } for i in range(1, n+1): count = sum(num_each_digit.values()) temp_dict = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0} for digit in num_each_digit: num_digit = num_each_digit[digit] digit_neighbors = neighbors[digit] for neighbor in digit_neighbors: temp_dict[neighbor] += num_digit num_each_digit = temp_dict return count % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: def matmul(A, B): BT = list(zip(*B)) res = [[sum((x*y) for x, y in zip(row, col))%M for col in BT] for row in A] return res def pow(A, n): l = n.bit_length() res = [[1 if i==j else 0 for j in range(len(A[0]))] for i in range(len(A))] tmp = A for i in range(l): mask = 1 << i if mask & n != 0: res = matmul(res, tmp) tmp = matmul(tmp, tmp) return res M = 10**9+7 A = [[0,0,0,0,1,0,1,0,0,0], [0,0,0,0,0,0,1,0,1,0], [0,0,0,0,0,0,0,1,0,1], [0,0,0,0,1,0,0,0,1,0], [1,0,0,1,0,0,0,0,0,1], [0,0,0,0,0,0,0,0,0,0], [1,1,0,0,0,0,0,1,0,0], [0,0,1,0,0,0,1,0,0,0], [0,1,0,1,0,0,0,0,0,0], [0,0,1,0,1,0,0,0,0,0]] res = matmul([[1,1,1,1,1,1,1,1,1,1]], pow(A,n-1)) return sum(res[0]) % 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: numpad = [[1,2,3],[4,5,6],[7,8,9],['*',0,'#']] dp = [1,1,1,1,1,1,1,1,1,1] square = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],[7,0,1],[2,6],[1,3],[2,4]] mod = 10**9+7 for i in range(2,N+1): temp=[0,0,0,0,0,0,0,0,0,0] for row in range(4): for col in range(3): if numpad[row][col] == '*' or numpad[row][col] == '#': continue else: num = numpad[row][col] for j in square[num]: temp[num] += dp[j]%mod dp=temp return sum(dp)%mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: moves = {1:[6, 8], 2:[7, 9], 3:[4, 8], 4:[0, 3, 9], 5:[], 6:[0, 1, 7], 7:[2, 6], 8:[1, 3], 9:[2, 4], 0:[4, 6]} 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 bound = 10**9+7 ans = 0 for i in range(0, 10): ans += dfs(i, 1) if ans >= bound: ans = ans%bound return ans
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: MOD = 10**9 + 7 next_moves = defaultdict(list) next_moves[1] = [6, 8] next_moves[2] = [7, 9] next_moves[3] = [4, 8] next_moves[4] = [0, 3, 9] next_moves[5] = [] next_moves[6] = [0, 1, 7] next_moves[7] = [2, 6] next_moves[8] = [1, 3] next_moves[9] = [2, 4] next_moves[0] = [4, 6] solutions = [1 for _ in range(10)] while n > 1: next_solutions = [] for last_num in range(0, 10): count = sum(solutions[prev] for prev in next_moves[last_num]) if count >= MOD: count %= MOD next_solutions.append(count) solutions = next_solutions n -= 1 return sum(solutions) % MOD memo = dict() def count(size, at): if size == 0: return 1 if (size, at) in memo: return memo[(size, at)] c = sum(count(size - 1, next_at) for next_at in next_moves[at]) if c >= MOD: c %= MOD memo[(size, at)] = c return c total_possible = sum(count(n - 1, at) for at in range(0, 10)) return total_possible % 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 = pow(10, 9) + 7 maps = { 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 = [[0] * n for _ in range(10)] for i in range(10): dp[i][0] = 1 for i in range(1, n): for digit in range(10): dp[digit][i] = sum(dp[j][i-1] for j in maps[digit]) return sum(dp[digit][-1] for digit 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: if n == 1: return 10 D = [] modulo = 10**9 + 7 movesDict = { 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]} for i in range(10): D.append([-1]*(n+1)) def help(start, length): if length == 1: return 1 if D[start][length] != -1: return D[start][length] moves = movesDict[start] total = 0 for newStart in moves: total += help(newStart, length - 1) total %= modulo D[start][length] = total # print(f'start: {start}, length: {length}, total: {total}') return total res = 0 for i in range(10): res += help(i, n) return res % modulo ''' 1 -> 6, 8 2 -> 7, 9 3 -> 4, 8 4 -> 3, 9, 0 5 -> n/a 6 -> 1, 7, 0 7 -> 2, 6 8 -> 1, 3 9 -> 2, 4 dp - solve how many for n - 1 starting at some index then ? array [start][length] init with [10][n+1] '''
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: 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 knight_dailer(n): paths = {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]} count = 0 import sys, functools sys.setrecursionlimit(10**9) @functools.lru_cache(None) def traverse(i, rem): if rem == 0: return 1 ans = 0 for p in paths[i]: res = traverse(p,rem-1) ans += res return ans for i in range(10): count += (traverse(i,n-1)) return count % ((10**9) + 7) return knight_dailer(n)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: MOD = 10 ** 9 + 7 map = [[4, 6], [6, 8], [7, 9], [4, 8], [0, 3, 9], [], [0, 1, 7], [2, 6], [1, 3], [2, 4]] dp = [1] * len(map) for hops in range(n - 1): dp2 = [0] * len(map) for node, cnt in enumerate(dp): for nei in map[node]: dp2[nei] += cnt dp2[nei] %= MOD dp = dp2 return sum(dp) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: dp = [ [0] * 10 for _ in range(n)] pad_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] } mod = 10**9 + 7 for i in range(10): dp[0][i] = 1 for i in range(n - 1): for j in range(10): dsts = pad_map[j] for dst in dsts: dp[i + 1][dst] = (dp[i][j] + dp[i + 1][dst]) % mod return sum(dp[-1])%mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: d = {1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [4, 2], 5: [], 0: [4, 6]} @lru_cache(None) def helper(k, i): if k == 0: return 1 r = 0 for n in d[i]: r += helper(k - 1, n) return r ans = 0 for n in list(d.keys()): ans += helper(N - 1, n) return ans % (10 ** 9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: mod = int(1e9)+7 d = {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]} ans = {} # 1,3; 4,6; 7,9 @lru_cache(maxsize=None) def dfs(i, n): nonlocal d if n == 0: return 1 ans = 0 for val in d[i]: ans += dfs(val, n-1) return ans for i in range(10): if i in [3,6,9]: ans[i] = ans[i-2] else: ans[i] = dfs(i, n-1) #print(ans) return sum(ans.values()) % mod # mod = int(1e9)+7 # d = {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]} # cache = collections.defaultdict(int) # #@lru_cache(maxsize=None) # def dfs(i, n): # if i in [3,6,9] and cache[i-2, n] != 0: return cache[i-2, n] # if i in [1,4,7] and cache[i+2, n] != 0: return cache[i+2, n] # nonlocal d # if not n: return 1 # s = sum(dfs(val, n-1) for val in d[i]) # cache[i, n] = s # return s # # return sum(dfs(i, n-1) for i in range(10)) % mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: mod = 10**9 + 7 dp = [[0 for _ in range(10)] for _ in range(n)] for i in range(10): dp[0][i] = 1 def check(k): if k == 0: return [4, 6] elif k == 1: return [6,8] elif k == 2: return [7, 9] elif k == 3: return [4, 8] elif k == 4: return [0, 3, 9] elif k == 5: return [] elif k == 6: return [0, 1, 7] elif k == 7: return [2, 6] elif k == 8: return [1, 3] else: return [2, 4] for i in range(1, n): for j in range(10): for k in check(j): dp[i][j] = (dp[i][j] + dp[i - 1][k]) % mod ans = 0 for i in range(10): ans = (ans + dp[n - 1][i])%mod return ans
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
from collections import Counter class Solution: def knightDialer(self, n: int) -> int: moves = {1 : [6, 8], 2 : [7, 9], 3 : [4, 8], 4 : [3, 9, 0], 6 : [1, 7, 0], 7 : [2, 6], 8 : [1, 3], 9 : [2, 4], 0 : [4, 6], } if n == 1: return 10 starts = Counter([i for i in moves]) for _ in range(n - 1): next = Counter() for i in starts: for j in moves[i]: next[j] += starts[i] starts = next return sum(starts.values()) % (10 ** 9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: dic = {1: [6, 8], 2: [7, 9], 4: [0, 3, 9], 5: [], 7: [2, 6], 8: [1, 3], 0: [4, 6]} #1 = 3, 4 = 6, 7 = 9 pair = {3: 1, 6: 4, 9: 7} @lru_cache(None) def dp(i, n): if n == 1: return 1 if i in pair: i = pair[i] res = 0 for j in dic[i]: res += dp(j, n - 1) return res return (2 * (dp(1, n) + dp(4, n) + dp(7, n)) + dp(0, n) + dp(2, n) + dp(5, n) + dp(8, 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: valid_moves = {0: (4, 6), 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), } dp = [[0]*10 for _ in range(n)] for c in range(10): dp[0][c] = 1 if n > 1: for c in range(10): dp[1][c] = len(valid_moves[c]) for i in range(2, n): for j in range(10): valid = valid_moves[j] #print (\"computing i: {0}, j:{1} with valid:{2}\".format(i, j, valid)) dp[i][j] = sum([dp[i-1][r] for r in valid]) # print (dp) # print ( sum(dp[n-1]) % (10**9+7)) return sum(dp[n-1]) % (10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: legal_moves = { 0: [4, 4], 1: [6, 8], 2: [7, 7], 3: [4, 8], 4: [0, 1, 7], # 5: [], 6: [0, 1, 7], 7: [2, 4], 8: [1, 1], 9: [2, 4] } def knightDialer(self, n: int) -> int: total = 0 if n != 1 else 1 memo = {} for i in range(0, 10): if i is not 5: total = total + self.num_jumps(i, n - 1, memo) return total % (pow(10, 9) + 7) def num_jumps(self, start, jumps_left, memo): # If we have no moves left we have completed a number if jumps_left == 0: return 1 # Check if we have done this calculation before if (start, jumps_left) not in memo: # Calculate total number of numbers from this starting position total = 0 for legal_move in self.legal_moves[start]: total = total + self.num_jumps(legal_move, jumps_left - 1, memo) # Save result memo[(start, jumps_left)] = total # Return calculated or retreived result return memo[(start, jumps_left)]
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 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: if n == 1: return 10 ''' build the neighbors dictionary neighbors to a key are the keys reachable to itself from a key ''' 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)} @lru_cache(maxsize=None) def num_ways(key, n): if n == 0: return 0 if n == 1: return len(neighbors[key]) w = 0 for neighbor in neighbors[key]: w += num_ways(neighbor, n-1) return w answer = 0 for i in range(10): answer += num_ways(i, n-1) #print(num_ways.cache_info()) answer %= (pow(10, 9) + 7) return answer
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: 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 ret=0 dp=[[0]*10 for i in range(n)] for i in range(10): dp[0][i]=1 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]} for i in range(1,n): for j in range(10): for neighbor in graph[j]: dp[i][j]+=dp[i-1][neighbor] ret=sum(dp[n-1][i] for i in range(10)) return ret%(10**9+7) #1 dp需要二维的来表示才行,一维的话,值很容易被覆盖,比如dp[1]在第二步的时候更新了,但是后面又用到dp[1]的话,就会用这个更新的值,而不是第一步的dp值 #1 这里我开始写成10^9+7了,^在python里是异或呀,幂要写成** #https://www.jianshu.com/p/5a588aab3ae5
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: 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: knight_graph = { 1:(8,6), 2:(7,9), 3:(8,4), 4:(3,9,0), 5:(), 6:(7,1,0), 7:(2,6), 8:(1,3), 9:(4,2), 0:(4,6) } memo = {} MOD = pow(10,9)+7 def btHelper(coordinate, n): if n == 0: return 1 possibility = 0 for avail_node in knight_graph[coordinate]: if (avail_node,n-1) in memo: possibility += memo[(avail_node, n-1)] else: possibility += btHelper(avail_node,n-1) memo[(coordinate, n)] = possibility % MOD return possibility ans = 0 for i in range(10): ans += btHelper(i,n-1) #print(memo) 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: if n == 1: return 10 x0,x1,x2,x3,x4,x5,x6,x7,x8,x9 = 1,1,1,1,1,0,1,1,1,1 for i in range(1, n): x0,x1,x2,x3,x4,x6,x7,x8,x9 = x4+x6,x6+x8,x7+x9,x4+x8,x0+x3+x9,x0+x1+x7,x2+x6,x1+x3,x2+x4 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: 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)) dp = [1]*10 for _ in range(n-1): new_dp = [0]*10 for i in range(10): new_dp[i] = sum(dp[nxt] for nxt in cell2jump[i]) dp = new_dp return sum(dp) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: moves = [[4,6], [6,8], [7,9], [4,8], [0,3,9], [], [1,7,0], [2,6], [1,3], [2,4]] dp = [1] * 10 for _ in range(n-1): dp2 = [0] * 10 for i in range(10): dp2[i] = sum(dp[j] for j in moves[i]) % (10**9+7) dp = dp2 return sum(dp) % (10**9 + 7) # def knightDialer(self, N: int) -> int: # pad, cnt = [[4, 6], [6, 8], [7, 9], [4, 8], [0, 3, 9], [], [0, 1, 7], [2, 6], [1, 3], [2, 4]], [1]*10 # for _ in range(N-1): # next_cnt = [1]*10 # for i in range(10): # next_cnt[i] = sum(cnt[j] for j in pad[i]) % (10**9+7) # cnt = next_cnt # return sum(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: kd_key = [[4, 6], [6, 8], [7, 9], [4, 8], [3, 9, 0], [], [1, 7, 0], [2, 6], [1, 3], [2, 4]] # kd_key[start] = [ends] memo = dict() out = 0 # all starting pos for i in range(10): out += self.dial(i, n, kd_key, memo) # print(int(1e9+7)) return out % int(1e9 +7) def dial(self, pos, n, kd_key, memo): if n == 1: return 1 out = 0 for next_pos in kd_key[pos]: if (next_pos, n-1) in memo: out += memo[(next_pos, n-1)] else: out += self.dial(next_pos, n-1, kd_key, memo) memo[(pos, n)] = out return out
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: 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 = { 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] } memo = {} def helper(cur, k): if (cur,k) in memo: return memo[(cur,k)] if k == 0: return 0 elif k == 1: return 1 res = 0 for item in jump[cur]: res += helper(item, k-1) memo[(cur,k)] = res % (10**9+7) return res res = 0 for i in range(10): res += helper(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: if n==1: return 10 p=math.pow(10,9)+7 c={} c[1]=[6,8] c[2]=[7,9] c[3]=[4,8] c[4]=[3,9,0] c[5]=[] c[6]=[1,7,0] c[7]=[2,6] c[8]=[1,3] c[9]=[2,4] c[0]=[4,6] dp=[[None for _ in range(n+1)]for _ in range(10)] for i in range(10): dp[i][1]=1 dp[i][2]=len(c[i]) def find(i,x): if dp[i][x]is not None: return dp[i][x] dp[i][x]=0 for j in c[i]: dp[i][x]+=find(j,x-1)%p return dp[i][x] res=0 for i in range(10): res+=find(i,n)%p return int(res%p)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: 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: numpad = [[1,2,3],[4,5,6],[7,8,9],['*',0,'#']] dp = [1]*10 square = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],[7,0,1],[2,6],[1,3],[2,4]] mod = 10**9+7 for i in range(2,N+1): temp=[0]*10 for num in range(10): for j in square[num]: temp[num] += dp[j]%mod dp=temp return sum(dp)%mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: neighbors = { 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: [6,4] } p = 10**9 + 7 dp = defaultdict(lambda:1) for t in range(n-1): for i in range(10): dp[i,t&1] = sum(dp[j,(t+1)&1] for j in neighbors[i]) % p return sum(dp[i,n&1] for i in range(10)) % p
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: 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 = [[] for x in range(10)] moves[0] = [4,6] moves[1] = [6,8] moves[2] = [7,9] moves[3] = [4,8] moves[4] = [3,9,0] moves[5] = [] moves[6] = [0,1,7] moves[7] = [2,6] moves[8] = [1,3] moves[9] = [2,4] dp = {} def dialer(start,n): if (start,n) in dp: return dp[(start,n)] if n==1: dp[(start,n)] = 1 return 1 xmoves = 0 for move in moves[start]: xmoves += dialer(move,n-1) xmoves %= (10**9+7) dp[(start,n)] = xmoves return xmoves xmoves = 0 for i in range(10): xmoves += dialer(i,n) return xmoves % (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: @lru_cache(None) def dp(i, k): if k == 0: return 1 result = 0 for j in jump[i]: result = (result + dp(j, k - 1)) % MOD return result MOD = 10 ** 9 + 7 jump = [[4, 6], [6, 8], [7, 9], [4, 8], [3, 9, 0], [], [0, 1, 7], [2, 6], [1, 3], [2, 4], list(range(10))] return dp(10, n)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: if n == 1: return 10 list_old = [1] * 10 for t in range(2, n+1): list_new = [0] * 10 for i in range(0, 10): temp = Solution().map(i) for j in range(len(temp)): list_new[i] = list_new[i] + list_old[temp[j]] list_old = list_new[0:10] return sum(list_old) % (10**9 + 7) def map(self, d: int) -> List[int]: if d == 0: return [4, 6] elif d == 1: return [6, 8] elif d == 2: return [7, 9] elif d == 3: return [4, 8] elif d == 4: return [0, 3, 9] elif d == 5: return [] elif d == 6: return [0, 1, 7] elif d == 7: return [2, 6] elif d == 8: return [1, 3] else: return [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: G = {5:set(), 1:set([6,8]), 3:set([4,8]), 7:set([2,6]), 9:set([2,4]), 4:set([3,9,0]),6:set([1,7,0]), 2:set([7,9]), 8:set([1,3]),0:set([4,6])} mem = dict() def dfs(val,rem): if (val, rem) in mem: return mem[(val,rem)] if rem == 1: return 1 ans = 0 for n in G[val]: ans += dfs(n,rem-1) mem[(val,rem)] = ans return ans%(10**9+7) ans = 0 for i in range(10): ans += dfs(i,n) 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
from collections import defaultdict move_map = { 0: {4,6}, 1: {6,8}, 2: {7,9}, 3: {4,8}, 4: {3, 9, 0}, 5: {}, 6: {1, 0, 7}, 7: {2, 6}, 8: {1, 3}, 9: {2, 4} } class Solution: def knightDialer(self, n: int) -> int: table = defaultdict(int) for key in move_map: table[key, 1] = 1 for N in range(2,n+1): for key in move_map: for new_key in move_map[key]: table[key, N] += table[new_key, N-1] res = 0 for key in move_map: res += table[key, n] return res % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: map = [[4, 6], [6, 8], [7, 9], [4, 8], [0, 3, 9], [], [0, 1, 7], [2, 6], [1, 3], [2, 4]] memo = [[-1] * n for i in range(len(map))] def recur(num, hops, memo): if hops < 0: return 0 elif hops == 0: return 1 if memo[num][hops] == -1: s = 0 for move in map[num]: s += recur(move, hops - 1, memo) memo[num][hops] = s return memo[num][hops] s = 0 for key in range(len(map)): s += recur(key, n - 1, memo) 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: knight_graph = { 1:(8,6), 2:(7,9), 3:(8,4), 4:(3,9,0), 5:(), 6:(7,1,0), 7:(2,6), 8:(1,3), 9:(4,2), 0:(4,6) } memo = {} MOD = pow(10,9)+7 def btHelper(coordinate, n): if (coordinate, n) in memo: return memo[(coordinate, n)] if n == 0: return 1 possibility = 0 for avail_node in knight_graph[coordinate]: possibility += btHelper(avail_node,n-1) memo[(coordinate, n)] = possibility % MOD return possibility ans = 0 for i in range(10): ans += btHelper(i,n-1) #print(memo) 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: if n == 1:return 10 R = int (math.pow(10,9) + 7) memo = {} options = { 1:[6,8], 2:[7,9], 3:[8,4], 4:[3,9,0], 6:[1,7,0], 7:[2,6], 8:[1,3], 9:[4,2], 0:[4,6] } def dfs(step, dig): if (step, dig) in memo : return memo[(step, dig)] if step==1: return 1 res = 0 for d in options[dig]: res = (res + dfs(step-1, d)) %R memo[(step,dig)] = res return res res = 0 for d in range(0,10,1): if d == 5 : continue res =(res + dfs(n, d))%R return res
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: dp = {} MOD = 10**9+7 def moveOnce(num0, N0): if (num0, N0) in dp: return dp[(num0, N0)] if N0 == 0: return 1 if N0 < 0 : return 0 subAns = 0 if num0 == 1: subAns = moveOnce(6, N0-1) + moveOnce(8, N0-1) elif num0 == 2: subAns = moveOnce(7, N0-1) + moveOnce(9, N0-1) elif num0 == 3: subAns = moveOnce(4, N0-1) + moveOnce(8, N0-1) elif num0 == 4: subAns = moveOnce(3, N0-1) + moveOnce(9, N0-1) + moveOnce(0, N0-1) elif num0 == 5: subAns = 0#moveOnce(5, N0-2) + moveOnce(7, N0-2) + moveOnce(9, N0-2) elif num0 == 6: subAns = moveOnce(1, N0-1) + moveOnce(7, N0-1) + moveOnce(0, N0-1) elif num0 == 7: subAns = moveOnce(2, N0-1) + moveOnce(6, N0-1) #+ moveOnce(5, N0-2) + moveOnce(7, N0-2) elif num0 == 8: subAns = moveOnce(1, N0-1) + moveOnce(3, N0-1) elif num0 == 9: subAns = moveOnce(2, N0-1) + moveOnce(4, N0-1) #+ moveOnce(5, N0-2) + moveOnce(9, N0-2) elif num0 == 0: subAns = moveOnce(4, N0-1) + moveOnce(6, N0-1) dp[(num0, N0)] = subAns%MOD return subAns return sum([moveOnce(i, N-1) for i in range(10)])%MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: 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] mod= 10**9 +7 curr= [1]*10 for i in range(N-1): nextlist= [0]*10 for j in range(10): for neigh in getNeighbors(j): nextlist[neigh]= (nextlist[neigh]+curr[j])%mod curr=nextlist return sum(curr)%mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: MOD = 10 ** 9 + 7 jumps = { 1: (6, 8), 2: (7, 9), 3: (4, 8), 4: (3, 9, 0), 5: (), 6: (1, 7, 0), 7: (2, 6), 8: (1, 3), 9: (2, 4), 0: (4, 6), } dp = [1] * 10 for i in range(2, n + 1): t = list(dp) for i in range(10): dp[i] = sum(t[v] for v in jumps[i]) % MOD return sum(dp) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: if n == 1: return 10 last_dp = [1]*10 this_dp = [0]*10 next_steps = { 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6] } for _ in range(2, n+1): for i in range(10): if i != 5: this_dp[i] = sum(last_dp[next_step] for next_step in next_steps[i]) % int(10**9+7) last_dp = this_dp this_dp = [0]*10 return sum(last_dp) % int(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 graph = {1: (6,8), 2: (9,7), 3: (4,8), 4: (9,3,0), 5: (), 6: (7,1,0), 7: (2,6), 8: (1,3), 9: (4,2), 0: (4,6)} count = 0 MOD = 10**9 + 7 memo = collections.defaultdict(list) def dfs(cur,graph,n,memo): if n == 0: return 1 if (n,cur) in memo: return memo[(n,cur)] count = 0 for nei in graph[cur]: count += dfs(nei,graph,n-1,memo) % MOD memo[(n,cur)] = count return count for nei in range(10): memo[nei] = [] count += dfs(nei,graph,n-1,memo) % MOD return count % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: memo = {} #(number,steps): totalDistinctValues moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] for i in range(0, 10): memo[(i,0)] = 0 memo[(i,1)] = 1 for n in range(2, N+1): for i in range(0, 10): memo[(i,n)] = 0 for nextDig in moves[i]: memo[(i,n)] += memo[(nextDig, n-1)] total = 0 for i in range(0, 10): total += memo[(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) -> int: s = 0 for k in range(10): d = [0] * 10 d[k] = 1 for i in range(n - 1): d2 = [ d[4] + d[6], d[6] + d[8], d[7] + d[9], d[4] + d[8], d[3] + d[9] + d[0], 0, d[1] + d[7] + d[0], d[2] + d[6], d[1] + d[3], d[2] + d[4], ] d = d2 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: memo, dp = [[4,6],[8,6],[7,9],[4,8],[3,9,0],[],[0,1,7],[2,6],[1,3],[2,4]], [1] * 10 for i in range(n - 1): dp = [sum([dp[j] for j in memo[i]]) for i in range(10)] return sum(dp) % (10 ** 9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: nei = { 1: [6, 8], 2: [7, 9], 3: [8, 4], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6] } def helper(n, curr, seen, nei): if n == 1: return 1 if curr not in nei: return 0 key = (n, curr) if key in seen: return seen[key] answer = 0 for ne in nei[curr]: answer = (answer + helper(n - 1, ne, seen, nei)) % (10 ** 9 + 7) seen[key] = answer return answer total = 0 seen = {} for i in range(10): total = (total + helper(n, i, seen, nei)) % (10 ** 9 + 7) return total
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: mapping = { 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6] } if N == 1: return 10 dp = collections.defaultdict(int) for idx in range(10): if idx == 5: continue dp[idx] = 1 while N > 1: next_dp = collections.defaultdict(int) for idx in range(10): if idx == 5: continue for k in mapping[idx]: next_dp[k] += dp[idx] N -= 1 dp = next_dp return sum(dp.values()) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
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]} validNumbers = {(1,i):1 for i in range(10)}##because it will always be 1 thing possible regardless of start digit. If we had used base case of 0, we would've gotten that we would double count. So sometimes it is better to do the simplest nontrivial basecase rather than try to work with 0. BIG_MOD = 10**9+7 def computePhoneNumbers(length,startDigit):##we can assume length is always >= 1 if (length,startDigit) in validNumbers: return validNumbers[(length,startDigit)] else: totalValid = 0 for neighbor in neighbors[startDigit]: totalValid += computePhoneNumbers(length-1,neighbor) totalValid %= (BIG_MOD) validNumbers[(length,startDigit)] = totalValid return totalValid allValid = 0 for i in range(10): allValid += computePhoneNumbers(n,i) allValid %= (BIG_MOD) return allValid
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 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 = [[-1 for _ in range(n)] for _ in range(10)] 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 == 1: return 10 mapper = {1:[8,6], 2:[7,9], 3:[8,4], 4:[0,9,3], 6:[0,1,7], 7:[2,6], 8:[1,3], 9:[2,4], 0:[4,6]} dp =[ [0]*10 for _ in range(N)] MM = 10**9+7 for i in range(10): if i==5: continue dp[0][i] = 1 def searching(s, i): if dp[s][i]>0: return dp[s][i] for k in mapper[i]: dp[s][i] += searching(s-1,k) dp[s][i] = dp[s][i] % MM return dp[s][i] for i in range(10): if i==5: continue searching(N-1,i) return sum(dp[N-1])% MM
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: 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: possibleMoves = {0:[4,6], 1:[6,8],2:[9,7],3:[4,8],4:[0,3,9],5:[],6:[0,1,7],7:[2,6],8:[1,3], 9:[2,4]} upper = 10**9 + 7 cache = {} def dfs(steps,loc): if (steps,loc) in cache: return cache[(steps,loc)] if steps == 0: return 1 ans = 0 for stop in possibleMoves[loc]: ans = (ans + dfs(steps-1,stop))%upper cache[(steps,loc)] = ans return ans return sum(dfs(n-1,loc) for loc in possibleMoves)%upper
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: 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 moves = { 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 = dict() def count_moves(position, digits): if digits == 1: return 1 if (position, digits) in memo: return memo[position, digits] curr = 0 for m in moves[position]: curr += count_moves(m, digits - 1) memo[position, digits] = curr % MOD return memo[position, digits] res = 0 for pos in moves.keys(): res += count_moves(pos, n) 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: MOD = 10**9 + 7 reach = { 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], } memo = {} def helper(num, steps): if (num, steps) in memo: return memo.get((num, steps)) if steps == 0: memo[((num, steps))] = 1 return memo.get((num, steps)) res = 0 for next_no in reach[num]: res += helper(next_no, steps-1) res = res % MOD memo[((num, steps))] = res return res result = 0 for pos in range(0,10): result += helper(pos, N-1) return result % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: paths = {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]} counts = 0 lookup = {} for i in range(10): counts += self.traverse(i,paths, lookup,n,0) return counts % ( 10**9 + 7 ) def traverse(self, starting, paths, lookup, n, level): if level == n-1: return 1 counts = 0 for path in paths[starting]: if (path, level +1) not in lookup: lookup[(path, level +1)] = (self.traverse(path, paths, lookup, n, level +1)) counts += lookup[(path, level +1)] return 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
class Solution: def knightDialer(self, n: int) -> int: adj_graph = { 1: [8,6], 2: [7,9], 3: [4,8], 4: [3,9,0], 5: [], 6: [1,7,0], 7: [2,6], 8: [1,3], 9: [4,2], 0: [4,6] } def moveHelper(num,n,memo): if n == 1: return 1 sumCounts = 0 for number in adj_graph[num]: if (number,n-1) in memo: sumCounts += memo[(number,n-1)] else: res = moveHelper(number,n-1,memo) sumCounts += res memo[(number,n-1)] = res return sumCounts sumCount = 0 memo = {} for k in list(adj_graph.keys()): sumCount += moveHelper(k,n,memo) return sumCount%(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
import numpy as np class Solution: def knightDialer(self, n: int) -> int: if n==1: return 10 def fast_power(a,m): res=np.identity(10,int) for bit_idx,bit in enumerate(reversed(bin(m)[2:])): if bit_idx==0: cur=a else: cur=np.mod(np.dot(cur,cur),10**9+7) if bit == '1': res=np.mod(np.dot(res,cur),10**9+7) return res gh = {} gh[0] = [4, 6] gh[1] = [6, 8] gh[2] = [7, 9] gh[3] = [4, 8] gh[4] = [0,3, 9] gh[5] = [] gh[6] = [0, 1, 7] gh[7] = [2, 6] gh[8] = [1, 3] gh[9] = [2, 4] t = [] for u in gh: if u != 5: row = 0 for v in gh[u]: row = row^(1 << v) row = list(bin(row)[2:]) row.reverse() row = list([int(x) for x in row]) row.extend([0] * (10 - len(row))) t.append(row) else: t.append([0] * 10) t = np.array(t,dtype=int) ans = sum(np.mod(np.dot(fast_power(t, n - 1), np.ones(10,dtype=int)),10**9+7)) % (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: # mod = int(1e9)+7 # d = {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]} # cache = collections.defaultdict(int) # #@lru_cache(maxsize=None) # def dfs(i, n): # #nonlocal d, cache # if not n: return 1 # if (i, n) not in cache: # s = sum(dfs(val, n-1) for val in d[i]) # cache[i, n] = s # if i in [3,6,9]: cache[i-2, n] = s # elif i in [1,4,7]: cache[i+2, n] = s # return cache[i, n] # # return sum(dfs(i, n-1) for i in range(10)) % mod self.moves = [(4,6),(6,8),(7,9),(4,8),(3,9,0),(),(1,7,0),(2,6),(1,3),(2,4)] return sum(self.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
class Solution: def knightDialer(self, n: int) -> int: # MOD = 10**9 + 7 # moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], # [1,7,0],[2,6],[1,3],[2,4]] # dp = [1] * 10 # for hops in range(n-1): # dp2 = [0] * 10 # for node, count in enumerate(dp): # for nei in moves[node]: # dp2[nei] += count # dp2[nei] %= MOD # dp = dp2 # return sum(dp) % MOD modulo = pow(10,9) + 7 get_position= [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] cache = {} def solve(pos, step): key = (pos, step) if key not in cache: if step>=n: return 1 res = 0 for i in get_position[pos]: res += solve(i, step + 1) res = res % modulo cache[key] = res return cache[key] res = 0 for i in range(10): res += solve(i, 1) return res % modulo
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: mod = 10**9 + 7 reachFrom = {1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6]} dp = [[1] * 10 for _ in range(2)] for i in range(1, n): for j in range(10): dp[i % 2][j] = 0 for src in reachFrom[j]: dp[i % 2][j] += dp[(i - 1) % 2][src] return sum(dp[(n - 1) % 2]) % 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: valid = { 1: [8, 6], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [4, 2], 0: [4, 6] } MOD = 10 ** 9 + 7 dp = {} nums = 0 def dfs(pos, steps): if steps == 0: return 1 if (pos, steps,) in dp: return dp[(pos, steps,)] c = 0 for next in valid[pos]: c += dfs(next, steps - 1) c %= MOD dp[(pos, steps,)] = c return c ans = 0 for i in range(10): x = dfs(i, n - 1) ans += x ans %= MOD return ans % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: self.knight_movable_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] } self.dp = [[None for _ in range(10)] for _ in range(n)] def traverse(dial, remaining): if remaining == 0: return 1 if self.dp[remaining][dial] is not None: return self.dp[remaining][dial] cnt = 0 for next_dial in self.knight_movable_map[dial]: cnt += traverse(next_dial, remaining-1) self.dp[remaining][dial] = cnt return cnt cnt = 0 for i in range(10): cnt += traverse(i, n -1) modulo_num = 10**9 + 7 return cnt % modulo_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
# DFS solution # O(n) time and space class Solution: def knightDialer(self, N: int) -> int: steps = { 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(start, remain, seen): if remain == 0: return 1 if (start, remain) in seen: return seen[(start, remain)] ans = 0 for s in steps[start]: ans += dfs(s, remain-1, seen) seen[(start, remain)] = ans return ans ans = 0 seen = {} for i in range(10): ans += dfs(i, N-1, seen) 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: neighbors = { 1: (6, 8), 2: (7, 9), 3: (4, 8), 4: (3, 9, 0), 5: [], # 5 has no neighbors 6: (1, 7, 0), 7: (2, 6), 8: (1, 3), 9: (2, 4), 0: (4, 6), } memo = {} def dp_memo(S, N): if (S, N) in memo: return memo[(S, N)] elif N == 0: return 1 else: moves = 0 for neigh in neighbors[S]: moves += dp_memo(neigh, N - 1) memo[(S, N)] = moves return moves result = 0 for i in range(0, 10): result += dp_memo(i, n - 1) 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: moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] mem = {} MOD = 10**9 + 7 def get(start, k): if k == 1: return 1 if (start, k) in mem: return mem[start, k] total = 0 for nxt in moves[start]: total += get(nxt, k-1) total = total % MOD mem[start,k] = total return total ret = 0 for x in range(10): ret += get(x, n) ret = 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 knightDialer(self, n: int) -> int: mapping = { 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 helper(num, total): if (num, total) in memo: return memo[(num, total)] if total == 0: return 1 count = 0 for next_num in mapping[num]: count += helper(next_num, total-1) memo[(num, total)] = count return count count = 0 for i in range(10): count += helper(i,n-1) return count % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: ''' mod = 10**9 + 7 moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],[1,7,0],[2,6],[1,3],[2,4]] dp = [1]*10 for _ in range(n-1): dp2 = [0]*10 for node, count in enumerate(dp): for nei in moves[node]: dp2[nei] = (dp2[nei] + count) % mod dp = dp2 return sum(dp)%mod ''' mod = 10**9+7 x0 = x1 = x2 = x3 = x4 = x5 = x6 = x7 = x8 = x9 = 1 for k in range(1, n): x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 = x4+x6, x6+x8, x7+x9, x4+x8, x3+x9+x0, 0, x1+x7+x0, x2+x6, x1+x3, x2+x4 return (x0+x1+x2+x3+x4+x5+x6+x7+x8+x9)%mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def __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.getNumberFromNMoves(n-1, i) return moves%(pow(10,9) + 7) def getNumberFromNMoves(self, remaining_moves, current_number): if remaining_moves == 0: return 1 moves = 0 for move in self.moves[current_number]: if (move, remaining_moves) not in self.memoized_moves: self.memoized_moves[(move, remaining_moves)] = self.getNumberFromNMoves(remaining_moves-1, move) moves += self.memoized_moves[(move, remaining_moves)] 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: # mp = {0: [4,6], 1: [6, 8], 2: [9,7], 3: [8,4], 4:[9, 0, 3], 5:[], 6: [1, 7, 0], 7: [6, 2], 8:[3,1], 9: [2,4]} c = 0 mp = [[4,6], [6,8], [9,7], [8,4], [0,9,3], [],[1,7,0],[6,2],[3,1], [2,4]] memo = {} for i in range(0, 10): c += self.gen(i, n, mp, memo) mpow = (10**9) + 7 return c % mpow def gen(self, start, n, mp, memo): if n == 1: return 1 key = (start, n) if key in memo: return memo[key] c = 0 for opt in mp[start]: c += self.gen(opt, n-1, mp, memo) memo[key] = c return c
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: 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: mp = {0: [4,6], 1: [6, 8], 2: [9,7], 3: [8,4], 4:[9, 0, 3], 5:[], 6: [1, 7, 0], 7: [6, 2], 8:[3,1], 9: [2,4] } c = 0 memo = {} for i in range(0, 10): c += self.gen(i, n, mp, memo) mpow = (10**9) + 7 return c % mpow def gen(self, start, n, mp, memo): if n == 1: return 1 key = (start, n) if key in memo: return memo[key] c = 0 for opt in mp[start]: c += self.gen(opt, n-1, mp, memo) memo[key] = c return c
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: 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 distinctNumbers(self, start, moves, validMoves, cache): if moves == 1: return 1 if (start, moves) in cache: return cache[(start,moves)] result = 0 for i in validMoves[start]: result += self.distinctNumbers(i, moves-1, validMoves, cache) cache[(start, moves)] = result return result def knightDialer(self, n: int) -> int: ''' define numpad define validmove (within array and not 'X') use dynamic programming, grid of numbers and length n ''' validMoves = dict() validMoves[1] = [6,8] validMoves[2] = [7,9] validMoves[3] = [4,8] validMoves[4] = [3,9,0] validMoves[5] = [] validMoves[6] = [1,7,0] validMoves[7] = [2,6] validMoves[8] = [1,3] validMoves[9] = [2,4] validMoves[0] = [4,6] cache = {} result = 0 for i in range(10): result += self.distinctNumbers(i, n, validMoves, cache) 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: path={ 0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9], 5:[], 6:[0,1,7], 7:[2,6], 8:[1,3], 9:[2,4] } M=10**9+7 dp={} def paths(i,n): if (i,n) in dp: return dp[(i,n)] #number of ways to jump from i n times if n==1: return len(path[i]) if n==0: return 1 s=0 for v in path[i]: s+=paths(v,n-1) dp[(i,n)]=s%M return dp[(i,n)] ans=0 for k in path: ans+=paths(k,n-1) return ans%(M)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: self.res = 0 mapping = {1:[6,8],2:[9,7],3:[8,4],4:[3,9,0],6:[1,7,0],7:[2,6],8:[1,3],9:[2,4],0:[4,6],5:[]} memo = {} def helper(n,rem): if (n,rem) in memo: return memo[(n,rem)] if rem == 0: return 1 res = 0 for nei in mapping[n]: res += helper(nei,rem-1) memo[(n,rem)] = res return res res = 0 for i in range(10): res += helper(i,n-1) return res % (10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: 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] } def helper(pos, hops_left): if hops_left == 1: return 1 if (pos, hops_left) in cache: return cache[(pos, hops_left)] else: sequences = 0 for ne in neighbors[pos]: sequences += helper(ne, hops_left-1) cache[(pos, hops_left)] = sequences # print(f\"seq {sequences} for (pos {pos}, hops_left {hops_left})\") return sequences cache = {} counts = [0]*10 for start in range(10): counts[start] = helper(start, n) # print(f\"counts[{start}] = {counts[start]}\") return sum(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
class Solution: moves = {0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0,3,9], 5:[], 6:[0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4]} def dialed_Numbers(self, number, moves_left, memoization): if (number, moves_left) in memoization: return memoization[(number, moves_left)] if moves_left == 0: return 1 fin_sum = 0 for next_move in self.moves[number]: fin_sum += self.dialed_Numbers(next_move, moves_left - 1, memoization) memoization[(number, moves_left)] = fin_sum return fin_sum def knightDialer(self, n: int) -> int: memoization = {} fin_sum = 0 for i in range(0, 10): fin_sum += (self.dialed_Numbers(i, n-1, memoization)) return fin_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: def recur_helper(self, num, n, cnt, memo, next_moves): if n == 0: return cnt key = (num, n) if key in memo: return memo[key] cnt = 0 for next_num in next_moves[num]: cnt += self.recur_helper(next_num, n-1, 1, memo, next_moves) memo[key] = cnt return memo[key] def knightDialer(self, n: int) -> int: next_moves = { 0: [4, 6], 1: [8, 6], 2: [7, 9], 3: [4, 8], 4: [9, 3, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4] } memo = dict() cnt = 0 for num in range(10): cnt += self.recur_helper(num, n-1, 1, memo, next_moves) 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: moves = [[4,6], [6,8], [7,9], [4,8], [0,3,9], [], [1,7,0], [2,6], [1,3], [2,4]] dp = [1] * 10 for _ in range(n-1): dp2 = [0] * 10 for i in range(10): dp2[i] = sum(dp[j] for j in moves[i]) % (10**9+7) dp = dp2 return sum(dp) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: results = 0 self.cache = {} for pos in range(10): results += self.traverse(pos, n) results %= 1000000007 return results def traverse(self, pos: int, length: int) -> int: if length == 1: return 1 if (pos, length) in self.cache: return self.cache[(pos, length)] jumps = 0 for neighbor in self.get_neighbors(pos): jumps += self.traverse(neighbor, length - 1) jumps %= 1000000007 self.cache[(pos, length)] = jumps return jumps def get_neighbors(self, n: int) -> Tuple[int]: NEIGHBORS = ( (4, 6), (6, 8), (7, 9), (4, 8), (0, 3, 9), (), (0, 1, 7), (2, 6), (1, 3), (2, 4) ) return NEIGHBORS[n]
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: self.edges = { 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [9, 3, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6] } self.MAX = (10 ** 9) + 7 # at *this* cell, with *this* many steps left, what's the count? self.ledger = [[None]*n for _ in range(10)] dialable = 0 for cell in range(10): dialable += self.knight_dialer(n - 1, cell) return dialable % self.MAX def knight_dialer(self, steps_left: int, position: int) -> None: if not steps_left: return 1 if self.ledger[position][steps_left]: return self.ledger[position][steps_left] dialable = 0 for nbr in self.edges[position]: dialable += self.knight_dialer(steps_left - 1, nbr) % self.MAX self.ledger[position][steps_left] = dialable return dialable % (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 = { 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 helper(pos, hops_left): if hops_left == 1: return 1 if (pos, hops_left) in cache: return cache[(pos, hops_left)] else: sequences = 0 for ne in neighbors[pos]: sequences += helper(ne, hops_left-1) cache[(pos, hops_left)] = sequences # print(f\"seq {sequences} for (pos {pos}, hops_left {hops_left})\") return sequences cache = {} counts = 0 for start in range(10): counts += helper(start, n) return 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
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
#bfs solution #graph problem class Solution: def knightDialer(self, n: int) -> int: neighbours = { 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), } memo={} def helper(position, num_hops): if (position, num_hops) in memo: return memo[(position, num_hops)] if num_hops == 0: return 1 num_sequences = 0 for neighbour in neighbours[position]: num_sequences += helper(neighbour, num_hops - 1) memo[(position, num_hops)] = num_sequences return num_sequences total = 0 for pos in neighbours: total += helper(pos, n-1) return total%(10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: NEIGHBORS = ( (4, 6), (6, 8), (7, 9), (4, 8), (0, 3, 9), (), (0, 1, 7), (2, 6), (1, 3), (2, 4) ) def knightDialer(self, n: int) -> int: results = 0 self.cache = {} for pos in range(10): results += self.traverse(pos, n) results %= 1000000007 return results def traverse(self, pos: int, length: int) -> int: if length == 1: return 1 if (pos, length) in self.cache: return self.cache[(pos, length)] jumps = 0 for neighbor in self.get_neighbors(pos): jumps += self.traverse(neighbor, length - 1) jumps %= 1000000007 self.cache[(pos, length)] = jumps return jumps def get_neighbors(self, n: int) -> Tuple[int]: return self.NEIGHBORS[n]
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: 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 == 1: 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) 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
class Solution: def knightDialer(self, n: int) -> int: dp = [[-1 for i in range(n+1)] for i in range(10)] 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]} out = 0 for i in range(0,10): out+=self.helper(n-1,i,d,dp) return out%1000000007 def helper(self,n,index,d,dp): if(n==0): return 1 if(dp[index][n]!=-1): return dp[index][n] ans = 0 for i in range(len(d[index])): ans+=self.helper(n-1,d[index][i],d,dp) dp[index][n] = 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: mod = 10**9 + 7 def knightDialer(self, N: int) -> int: nMap = { 0: (6, 4), 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) } m = {} # (position, num) def count(start, n): if (start, n) in m: return m[(start, n)] if n == 0: return 1 else: num = 0 for i in nMap[start]: num += count(i, n - 1) m[(start, n)] = num return num ret = 0 for i in range(10): ret = (ret + count(i, N - 1)) % self.mod return int(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 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]] cache = {} def backtrack(pos, hops): if (pos, hops) in cache: return cache[(pos, hops)] if hops == 0: return 1 count = 0 for nbr in moves[pos]: count += backtrack(nbr, hops-1) 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
class Solution: legal_moves = { 0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], # 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4] } def knightDialer(self, n: int) -> int: total = 0 if n != 1 else 1 memo = {} for i in range(0, 10): if i is not 5: total = total + self.num_jumps(i, n - 1, memo) return total % (pow(10, 9) + 7) def num_jumps(self, start, jumps_left, memo): # If we have no moves left we have completed a number if jumps_left == 0: return 1 # Check if we have done this calculation before if (start, jumps_left) not in memo: # Calculate total number of numbers from this starting position total = 0 for legal_move in self.legal_moves[start]: total = total + self.num_jumps(legal_move, jumps_left - 1, memo) # Save result memo[(start, jumps_left)] = total # Return calculated or retreived result return memo[(start, jumps_left)]
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: 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 moveMap = {1:[8, 6], 2:[7, 9], 3:[4, 8], 4:[3, 9, 0], 5:[], 6:[1, 7, 0], 7:[2, 6], 8:[1, 3], 9:[4, 2], 0:[4, 6]} memo = {} def aux(key, rem): if rem == 0: return 1 if (key, rem) in memo: return memo[(key, rem)] count = 0 # Move to each valid key from this key moves = moveMap[key] for m in moves: count += aux(m, rem - 1) # Save the result memo[(key, rem)] = count return count count = 0 for startKey in moveMap: count += aux(startKey, n - 1) return count % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def __init__(self): self.pos_2_pos_map = { 1: (6, 8), 2: (7, 9), 3: (4, 8), 4: (3, 0, 9), 5: tuple(), 6: (7, 1, 0), 7: (2, 6), 8: (1, 3), 9: (4, 2), 0: (4, 6) } def dfs(self, position, n, memo): if n == 0: return 1 if (position, n) in memo: return memo[(position, n)] counts = 0 for nei in self.pos_2_pos_map[position]: counts += self.dfs(nei, n-1, memo) memo[(position, n)] = counts return counts def knightDialer(self, n: int) -> int: memo = {} counts = 0 for position in self.pos_2_pos_map: counts += self.dfs(position, n-1, memo) return 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
class Solution: def knightDialer(self, l: int) -> int: table = defaultdict(lambda: defaultdict(int)) tableEnts = {(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0), (9, 0)} def dp(n, lv) -> int: if lv == 0: return 1 if (n, lv) in tableEnts: return table[n][lv] if n == 0: ret = dp(4, lv - 1) + dp(6, lv - 1) elif n == 1: ret = dp(6, lv - 1) + dp(8, lv - 1) elif n == 2: ret = dp(7, lv - 1) + dp(9, lv - 1) elif n == 3: ret = dp(4, lv - 1) + dp(8, lv - 1) elif n == 4: ret = dp(3, lv - 1) + dp(9, lv - 1) + dp(0, lv - 1) elif n == 6: ret = dp(1, lv - 1) + dp(7, lv - 1) + dp(0, lv - 1) elif n == 7: ret = dp(2, lv - 1) + dp(6, lv - 1) elif n == 8: ret = dp(1, lv - 1) + dp(3, lv - 1) elif n == 9: ret = dp(2, lv - 1) + dp(4, lv - 1) ret %= 1000000007 table[n][lv] = ret tableEnts.add((n, lv)) return ret if l == 1: return 10 else: res = 0 for i in [0, 1, 2, 3, 4, 6, 7, 8, 9]: res += dp(i, l - 1) return res % 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: k = { 0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4], } mem = {} def count(i, j): if j > n: return 0 if j == n: return 1 if (i, j) in mem: return mem[i, j] cc = 0 for l in k[i]: cc += count(l, j + 1) mem[i, j] = cc % (10 ** 9 + 7) return mem[i, j] return sum(count(i, 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: def matmul(A, B): BT = list(zip(*B)) res = [[sum((x*y)%M for x, y in zip(row, col)) for col in BT] for row in A] return res def pow(A, n): l = n.bit_length() res = [[1 if i==j else 0 for j in range(len(A[0]))] for i in range(len(A))] tmp = A for i in range(l): mask = 1 << i if mask & n != 0: res = matmul(res, tmp) tmp = matmul(tmp, tmp) return res M = 10**9+7 A = [[0,0,0,0,1,0,1,0,0,0], [0,0,0,0,0,0,1,0,1,0], [0,0,0,0,0,0,0,1,0,1], [0,0,0,0,1,0,0,0,1,0], [1,0,0,1,0,0,0,0,0,1], [0,0,0,0,0,0,0,0,0,0], [1,1,0,0,0,0,0,1,0,0], [0,0,1,0,0,0,1,0,0,0], [0,1,0,1,0,0,0,0,0,0], [0,0,1,0,1,0,0,0,0,0]] res = matmul([[1,1,1,1,1,1,1,1,1,1]], pow(A,n-1)) return sum(res[0]) % M