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: BASE = 10 ** 9 + 7 def knightDialer(self, N: int) -> int: if N <= 1: return 10 a, b, c, d = 2, 4, 2, 1 for _ in range(N - 1): a, b, c, d = (b, 2 * (a + c) % self.BASE, (b + 2 * d) % self.BASE, c) return (a + b + c + d) % self.BASE
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: dic={0:[4,6],1:[6,8],2:[7,9],3:[4,8],4:[0,3,9],5:[],6:[0,1,7],7:[2,6],8:[1,3],9:[2,4]} dp=[1 for i in range(10)] for i in range(N-1): new_dp=[0 for j in range(10)] for j in range(10): for k in dic[j]: new_dp[j]+=dp[k] new_dp[j]%=(10**9+7) 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],[],[0, 1, 7] ,[2, 6],[1, 3],[2, 4]] dp = [1] * 10 for _ in range(n-1): dp2 = [0] * 10 for i, cnt in enumerate(dp): for nxt in moves[i]: dp2[nxt] += cnt dp = dp2 return sum(dp) % (10**9 + 7) # time: O(n) # space: O(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: total_jumps = [1] * 10 #base case you can be placed ANYWHERE #These are your neighbors where you jumping to with your knight mover 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) } #jumps allowed is n - 1 for jumps_left in range(n - 1): #our next_jumps next_jumps = [0] * 10 for num in range(0, 10): for next_num in neighbors[num]: next_jumps[next_num] = next_jumps[next_num] + total_jumps[num] % (10 ** 9 + 7) total_jumps = next_jumps return sum(total_jumps) % (10 ** 9 + 7) # class Solution(): # def count_sequences(start_position, num_hops): # prior_case = [1] * 10 # current_case = [0] * 10 # current_num_hops = 1 # while current_num_hops <= num_hops: # current_case = [0] * 10 # current_num_hops += 1 # for position in range(0, 10): # for neighbor in neighbors(position): # current_case[position] += prior_case[neighbor] # prior_case = current_case # return current_case[start_position] # class Solution: # def knightDialer(self, n: int) -> int: # #knight -> up or down (2 steps) and move left or right ( 1step) # #knight -> move left or right (2 steps) and move up and down (1 step) # #We have a Knight as shown above # #We also have a numer pad and can ONLY stand on numbers # #Given an int -> n move the knight on any numeric cell and then perform n-1 jumps to dial a number of length n # #Return the answer modulo 10^9 + 7 # 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) # } # cur_numpads = [1] * 10 # for pos in range(n - 1): # next_numpads_num = [0] * 10 # for cur_key in range(10): # for next_key in neighbors[cur_key]: # next_numpads_num[next_key] = next_numpads_num[next_key] + cur_numpads[cur_key] % (10 ** 9 + 7) # cur_numpads = next_numpads_num # return sum(cur_numpads) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: dp = [1 for _ in range(10)] BASE = 10**9+7 data = { 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(n-1): dp_tmp = [] for j in range(10): sum_tmp = 0 for k in data[j]: sum_tmp += (dp[k] % BASE) dp_tmp.append(sum_tmp % BASE) dp = dp_tmp return sum(dp) % BASE
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n): dmap = [[4, 6], [8, 6], [7, 9], [8, 4], [3, 9, 0], [], [1, 7, 0], [2, 6], [1, 3], [2, 4],[4, 6]] mod_val = 10 ** 9 + 7 ans = 0 dp1 = [1]*10 dp2 = [0]*10 for _ in range(2, n+1): for j in range(0, 10): tv = dmap[j] for t in tv: dp2[j] += dp1[t] % mod_val dp1 = dp2[:] dp2 = [0]*10 #print(dp) ans = sum(dp1) % mod_val 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 __init__(self): self.paths = {1: [8, 6], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6]} def knightDialer(self, n: int) -> int: # 3 constant = 10**9+7 if n == 0: return 0 # Stream prev_cache = [1 for i in range(10)] cache = [0 for i in range(10)] for i in range(n-1): # print(prev_cache) for j in range(10): total_sum = 0 for adj_num in self.paths[j]: total_sum += prev_cache[adj_num] cache[j] = total_sum # print(j, total_sum) # print(cache) prev_cache, cache = cache, prev_cache return sum(prev_cache) % constant # Below is recur # ways(0, 3) # ways(4, 2) # ways(3, 1) # ways(4, 0) # return 0 # ways(8, 0) # 0 # ways(9, 1) # ways(0, 1) # ways(6, 2) # cache = {} # # @lru_cache # def ways(number, turns): # if turns < 2: # return turns # key = (number, turns) # if key in cache: # return cache[key] # total_ways = 0 # for new_number in self.paths[number]: # total_ways += ways(new_number, turns-1) # cache[key] = total_ways % (10**9+7) # return cache[key] # total_ways = 0 # 0 # for i in range(10): # total_ways += ways(i, n) # # return total_ways % (10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: mod = 10**9 + 7 d = dict() d[0] = {4, 6} d[1] = {6, 8} d[2] = {7, 9} d[3] = {4, 8} d[4] = {0, 3, 9} d[6] = {0, 1, 7} d[7] = {2, 6} d[8] = {1, 3} d[9] = {2, 4} # memo = [[0]*10 for i in range(n+1)] # memo[1] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] # for i in range(2, n+1): # for key, val in d.items(): # for v in val: # memo[i][key] += memo[i-1][v] # memo = [[0]*10 for i in range(n+1)] memo = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] memo2 = [0]*10 for i in range(2, n+1): for key, val in list(d.items()): for v in val: memo2[key] += memo[v] memo = memo2 memo2 = [0]*10 return sum(memo)%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 = [[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[0,1,7],[2,6],[1,3],[2,4]] prior_case = [1] * 10 current_case = [0] * 10 current_num_hops = 1 while current_num_hops < n: current_case = [0] * 10 current_num_hops += 1 for position in range(0, 10): for neighbor in neighbors[position]: current_case[position] += prior_case[neighbor] prior_case = current_case # print(sum(prior_case)) return sum(prior_case) % (10**9 + 7) ''' moves = [[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[0,1,7],[2,6],[1,3],[2,4]] # down up dp appraoch prev_level = [1] * 10 # base curr_level = [0] * 10 level = 1 while level < n: for num in range(10): for reachable in moves[num]: curr_level[reachable] += prev_level[reachable] level += 1 prev_level = curr_level curr_evel = [0] * 10 print(sum(prev_level)) return sum(prev_level) '''
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: 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], [], [0, 1, 7], [2, 6], [1, 3], [2, 4]] cur_count = [1] * 10 for _ in range(n-1): next_count = [0] * 10 for src_key in range(10): for dst_key in moves[src_key]: next_count[dst_key] += cur_count[src_key] cur_count = next_count return sum(cur_count) % 1000000007
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: MOD = 1000000007 if N == 1: return 10 a, b, c, d = 1, 1, 1, 1 for _ in range(N-1): a, b, c, d = (c + b) % MOD, (2*a + d) % MOD, (2*a) % MOD, (2*b) % MOD return (4 * a + 2 * b + 2 * c + d) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution(object): def knightDialer(self, N): MOD = 10**9 + 7 moves = [[4, 6], [6, 8], [7, 9], [4, 8], [3, 9, 0], [], [1, 7, 0], [2, 6], [1, 3], [2, 4]] dp = [1] * 10 for hops in range(N - 1): dp2 = [0] * 10 for 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: if n == 0: return 0 if n == 1: return 10 pad = { 0: (4,6), 1: (6, 8), 2: (7,9), 3: (4,8), 4: (0, 3, 9), 5: tuple(), 6: (0,1,7), 7: (2,6), 8: (1,3), 9: (2,4), } current = [0]*10 prev = [1]*10 for _ in range(n-1): current = [0]*10 for key in range(10): for bro in pad[key]: current[key] += prev[bro] prev = current return sum(current) %(10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: jump = {1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9], 5:[],6:[0,1,7], 7:[6,2],8:[1,3],9:[4,2],0:[6,4]} dp = [1]*10 for _ in range(n-1): new_dp = [0]*10 for u in range(10): for v in jump[u]: new_dp[v] += dp[u] 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: jump = {1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9], 5:[],6:[0,1,7], 7:[6,2],8:[1,3],9:[4,2],0:[6,4]} dp = [1]*10 for _ in range(n-1): new_dp = [0]*10 for v in range(10): for u in jump[v]: new_dp[v] += dp[u] 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: if n == 1: return 10 transition = { 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] } vec = [1 for _ in range(10)] for n in range(1, n): new_vec = [0 for _ in range(10)] for i in range(0, 10): for j in transition[i]: new_vec[j] = (new_vec[j] + vec[i]) % (1_000_000_000 + 7) # print(new_vec, vec) vec = new_vec return sum(vec) % (1_000_000_000 + 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(object): def knightDialer(self, N): MOD = 10**9 + 7 moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] dp = [1] * 10 for hops in range(N-1): dp2 = [0] * 10 for 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: move = {0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9],5:[],6:[1,7,0],7:[2,6],8:[1,3],9:[4,2]} pre = [1 for _ in range(10)] for i in range(n-1): new = [0 for _ in range(10)] for j in range(10): for k in move[j]: new[k] += pre[j]%(10**9+7) pre = new return sum(pre)%(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], [0,3,9], [], [0,1,7], [2,6], [1,3], [2,4]] dp = [1]*10 for i in range(n-1): new_dp = [0]*10 for c, val in enumerate(dp): for j in moves[c]: new_dp[j] += val new_dp[j] %= mod dp = new_dp return (sum(dp))%mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: MOD = 10**9 + 7 # moves: each item is numbers which can move to idx number # e.g., 0 can move to 4 and 6 moves = [(4,6),(6,8),(7,9),(4,8),(3,9,0),(), (1,7,0),(2,6),(1,3),(2,4)] # dp[start] = f(start, n), n as idx in dp # f(start, n): starting from start and dial n times dp = [1] * 10 for hops in range(n-1): dp2 = [0] * 10 for node, count in enumerate(dp): # node is number 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: 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 neighbor in moves[node]: dp2[neighbor] += count dp2[neighbor] %= 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: 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: dct = {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(n - 1): nxt = [0] * 10 for i in range(10): for j in dct[i]: nxt[j] += dp[i] dp = nxt 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: MOD = 10**9 + 7 if n == 1: return 10 next_square = [[4,6], [6,8], [7,9], [4,8], [3, 9, 0], [], [0, 1, 7], [2,6], [1,3], [2,4], [4,6]] values = [1, 1, 1, 1, 1, 0, 1, 1, 1, 1] for i in range(1, n): next_values = [0] * 10 for num in range(10): for square in next_square[num]: next_values[square] += values[num] next_values[square] %= MOD values = next_values[:] return sum(values) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: 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: jump = {1:[6,8], 2:[7,9], 3:[8,4], 4:[0,9,3], 5:[], 6:[7,1,0], 7:[6,2], 8:[3,1], 9:[4,2], 0:[6,4]} dp = [1]*10 for _ in range(n-1): nxt = [0]*10 for i in range(10): for j in jump[i]: nxt[j] += dp[i] dp = nxt return sum(dp)%(10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] MOD = 10**9+7 dp = [1]*10 for jumps in range(n-1): temp = [0]*10 for i in range(10): for nei in moves[i]: temp[nei] += dp[i] temp[nei] %= 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: # # starting points have 10 possible locations # grid = [ # [1,2,3], # [4,5,6], # [7,8,9], # [None,0,None] # ] # # DFS to search through all possibilities # X = len(grid) # Y = len(grid[0]) # def dfs(x,y,remain,X=X,Y=Y,grid=grid): # if x < 0 or x >= X or y < 0 or y >= Y or not grid[x][y] or remain == 0: # return -1 # temp_step = 8 # temp_step += dfs(x-2,y+1,remain-1) # temp_step += dfs(x-1,y+2,remain-1) # temp_step += dfs(x+1,y+2,remain-1) # temp_step += dfs(x+2,y+1,remain-1) # temp_step += dfs(x+2,y-1,remain-1) # temp_step += dfs(x+1,y-2,remain-1) # temp_step += dfs(x-1,y-2,remain-1) # temp_step += dfs(x-2,y-1,remain-1) # self.step *= temp_step # print(temp_step) # return temp_step # total = 0 # for i in range(X): # for j in range(Y): # if grid[i][j]: # self.step = 0 # dfs(i,j,n) # total += self.step # return total % (10**9+7) MOD = 10**9 + 7 moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] dp = [1] * 10 for hops in range(n-1): dp2 = [0] * 10 for node, count in enumerate(dp): for nei in moves[node]: dp2[nei] += count dp2[nei] %= MOD dp = dp2 return sum(dp) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: dp = [1] * 10 states = { 0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2,6], 8: [1,3], 9: [2,4] } mod = int(1e9) + 7 for i in range(n-1): dp1 = [0] * 10 for j, st in list(states.items()): for k in st: dp1[k] += dp[j] dp1[k] %= mod dp = dp1 return sum(dp) % mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: MOD = 10**9 + 7 if n == 1: return 10 next_square = [[4,6], [6,8], [7,9], [4,8], [3, 9, 0], [], [0, 1, 7], [2,6], [1,3], [2,4], [4,6]] values = [1, 1, 1, 1, 1, 0, 1, 1, 1, 1] for i in range(1, n): next_values = [0] * 10 for num in range(10): for square in next_square[num]: next_values[square] += values[num] next_values[square] %= MOD values = next_values[:] return sum(values) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
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: if n == 1: return 10 old_row = [1 for _ in move_map] new_row = [0 for _ in move_map] for N in range(2,n+1): new_row = [0 for _ in move_map] for key in move_map: res = 0 for new_key in move_map[key]: res += old_row[new_key] new_row[key] = res old_row = new_row return sum(new_row) % (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: nxt = [[4, 6], [6, 8], [7, 9], [4, 8], [0, 3, 9], [], [0, 1, 7], [2, 6], [1, 3], [2, 4]] dp0 = [1] * 10 for _ in range(1, n): dp1 = [0]*10 for i in range(10): for j in nxt[i]: dp1[j] += dp0[i] dp0[:] = dp1[:] return sum(dp0)%(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: dialer_map = {0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4]} level = [1 for i in range(10)] for i in range(1, n): next_level = [0 for i in range(10)] for l in range(10): for d in dialer_map[l]: next_level[d]+=level[l] level = next_level return sum(level)%(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
#27 class Solution: def knightDialer(self, n: int) -> int: prev = [1] * 10 fromMap = {1: {8,6}, 2: {7,9}, 3:{4,8}, 4: {0,3,9}, 5:{}, 6:{0,1,7}, 7:{2,6}, 8:{1,3}, 9:{2,4}, 0:{4,6}} for i in range(1,n): dp = [0] * 10 for k in range(10): for j in fromMap[k]: dp[k] += prev[j] dp[k] %= 10**9 + 7 prev = dp return sum(prev) % (10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: prev = [0] * 10 dp = [1] * 10 m = { 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 parents(x): return m[x] for l in range(1, n + 1): for x in range(10): for prevX in parents(x): dp[x] = (dp[x] + prev[prevX]) % (10 ** 9 + 7) prev, dp = dp, [0] * 10 return sum(prev) % (10 ** 9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: if not n: return 0 if n == 1: return 10 steps = [1] * 10 mapping = {1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5:[], 6:[1,7, 0], 7:[2, 6], 8:[1,3], 9:[2,4], 0:[4,6]} for i in range(2, n+1): newSteps = [0] * 10 for i in range(10): for j in mapping[i]: newSteps[i] += steps[j] newSteps[i] %= 10**9 + 7 steps = newSteps return sum(steps) % (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 dc = {(x,1): 1 for x in range(10)} for i in range(1,n): dc[(0,i+1)] = (dc[(4,i)] + dc[(6,i)]) % mod dc[(1,i+1)] = (dc[(6,i)] + dc[(8,i)]) % mod dc[(2,i+1)] = (dc[(7,i)] + dc[(9,i)]) % mod dc[(3,i+1)] = (dc[(4,i)] + dc[(8,i)]) % mod dc[(4,i+1)] = (dc[(0,i)] + dc[(3,i)] + dc[(9,i)]) % mod dc[(5,i+1)] = 0 dc[(6,i+1)] = (dc[(0,i)] + dc[(1,i)] + dc[(7,i)]) % mod dc[(7,i+1)] = (dc[(2,i)] + dc[(6,i)]) % mod dc[(8,i+1)] = (dc[(1,i)] + dc[(3,i)]) % mod dc[(9,i+1)] = (dc[(2,i)] + dc[(4,i)]) % mod res = 0 for j in range(10): res = (res + dc[(j,n)]) % mod return res
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: dirs = {(-2, -1), (-1, -2), (1, -2), (2, -1), (2, 1), (1, 2), (-1, 2), (-2, 1)} prohibited = {(0, 3), (2, 3)} graph = {} result = [0] * (3 * 4) for i in range(3 * 4): x1 = i % 3 y1 = i // 3 graph[i] = [] if (x1, y1) in prohibited: continue for j in range(3 * 4): x2 = j % 3 y2 = j // 3 if (x2 - x1, y2 - y1) in dirs and (x2, y2) not in prohibited: graph[i].append(j) result[i] = 1 # for k, v in graph.items(): # print(k, v) M = 10 ** 9 + 7 for i in range(1, n): res_next = [0] * (3 * 4) for x in range(3 * 4): for adj in graph.get(x, []): res_next[x] = (res_next[x] + result[adj]) % M result = res_next # print(result) return sum(result) % 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: KMOD = 10 ** 9 + 7 moves = [[4,6], [8,6], [7,9], [4,8], [3,9,0], [], [1,7,0], [2,6], [1,3], [2,4]] # moves represet 4 and 6 can jump to 1 (index position) dp = [1 for _ in range(10)] # 10 keys for k in range(1, n): cur = [0 for _ in range(10)] for i in range(10): # for each key for from_key in moves[i]: cur[from_key] += dp[i] dp = cur result = 0 for i in range(10): result += dp[i] return result % KMOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: lis = [10, 20, 46, 104, 240, 544] if(n <= 6): return lis[n-1] for i in range(7, n+1): lis.append(6*lis[-2] - 4*lis[-4]) return lis[-1]%(10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: ''' 0: 4,6 1: 6,8 2: 7,9 3: 4,8 4: 0,3,9 5: None 6: 1,7,0 7: 2,6 8: 1,3 9: 2,4 ''' mod = 10**9+7 nxt_pos = [[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[1,7,0],[2,6],[1,3],[2,4]] dp = [1 for _ in range(10)] # dp[i] is the number of times of i be the last dialed number at step 1 res = 10 for _ in range(1,n): cur_dp = [0 for _ in range(10)] for val, times in enumerate(dp): for nxt in nxt_pos[val]: cur_dp[nxt] += times cur_dp[nxt] %= mod dp = cur_dp return sum(dp)%mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: numMoves = {} for i in range(10): numMoves[1, i] = 1 for i in range(2,n+1): numMoves[i,1] = numMoves[i-1, 6] + numMoves[i-1, 8] % (10**9+7) numMoves[i,2] = numMoves[i-1, 7] + numMoves[i-1, 9] % (10**9+7) numMoves[i,3] = numMoves[i-1, 4] + numMoves[i-1, 8] % (10**9+7) numMoves[i,4] = numMoves[i-1, 3] + numMoves[i-1, 9] + numMoves[i-1,0] % (10**9+7) numMoves[i,5] = 0 numMoves[i,6] = numMoves[i-1, 1] + numMoves[i-1, 7] + numMoves[i-1,0] % (10**9+7) numMoves[i,7] = numMoves[i-1, 2] + numMoves[i-1, 6] % (10**9+7) numMoves[i,8] = numMoves[i-1, 1] + numMoves[i-1, 3] % (10**9+7) numMoves[i,9] = numMoves[i-1, 2] + numMoves[i-1, 4] % (10**9+7) numMoves[i,0] = numMoves[i-1, 4] + numMoves[i-1, 6] % (10**9+7) totalMoves = 0 for i in range(10): totalMoves += numMoves[n, i] return totalMoves % (10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
move_map = { 0: {4,6}, 1: {6,8}, 2: {7,9}, 3: {4,8}, 4: {3, 9, 0}, 5: {}, 6: {1, 0, 7}, 7: {2, 6}, 8: {1, 3}, 9: {2, 4} } class Solution: def knightDialer(self, n: int) -> int: if n == 1: return 10 old_row = [1 for _ in move_map] new_row = [0 for _ in move_map] for N in range(2,n+1): for key in move_map: res = 0 for new_key in move_map[key]: res += old_row[new_key] new_row[key] = res for idx, n in enumerate(new_row): old_row[idx] = n return sum(new_row) % (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], [], [0, 1, 7], [2, 6], [1, 3], [2, 4]] result = [1] * 10 hops = 0 while hops < n - 1: nextResult = [0] * 10 for idx in range(len(result)): for step in moves[idx]: nextResult[idx] += result[step] result = nextResult hops += 1 return sum(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: dp = [[1] * 10] + [[0] * 10 for _ in range(n-1)] for i in range(1, n): dp[i][1] += dp[i-1][8] + dp[i-1][6] dp[i][3] = dp[i][1] dp[i][2] += dp[i-1][7] + dp[i-1][9] dp[i][4] += dp[i-1][9] + dp[i-1][3] + dp[i-1][0] dp[i][6] = dp[i][4] dp[i][7] += dp[i-1][6] + dp[i-1][2] dp[i][9] = dp[i][7] dp[i][8] += dp[i-1][1] + dp[i-1][3] dp[i][0] += dp[i-1][4] + dp[i-1][6] return sum(dp[-1]) % (10 ** 9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: MOD = 7 + 10**9 moves = [[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[0,1,7],[2,6],[1,3],[2,4]] dp = [1] * 10 for i in range(n-1): dp2 = [0] * 10 for node, count in enumerate(dp): for neighbor in moves[node]: dp2[neighbor] += count dp2[neighbor] %= MOD dp = dp2 return sum(dp) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: dp=[1]*10 dic={0:[4,6],1:[8,6],2:[7,9],3:[4,8],4:[3,9,0],6:[1,7,0],7:[2,6],8:[1,3],9:[2,4]} for i in range(1,n): temp=dp dp=[0]*10 for j in range(10): if j in dic: for k in dic[j]: dp[k]+=temp[j] dp[k]%=(10**9 + 7) 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: NEIGHBORS = ( (4, 6), (6, 8), (7, 9), (4, 8), (0, 3, 9), (), (0, 1, 7), (2, 6), (1, 3), (2, 4) ) cache = {} def get_neighbors(self, n: int) -> Tuple[int]: return self.NEIGHBORS[n] def knightDialer(self, n: int) -> int: results = 0 prior = [1] * 10 for _ in range(n - 1): current = [0] * 10 for pos in range(10): for neighbor in self.get_neighbors(pos): current[pos] += prior[neighbor] % 1000000007 prior = current return sum(prior) % 1000000007 # 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
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: 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],[],[0,1,7],[2,6],[1,3],[2,4]] memo = [1]*10 for i in range(n-1): memonext = [0]*10 for j in range(10): for m in moves[j]: memonext[m] += memo[j] #print(memonext,m) for j in range(10): memo[j] = memonext[j] #print(memo) return sum(memo) % (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
''' 0. 1. 2. 3. 4. 5. 6. 7. 8. 9 [2, 2, 2, 2, 3, 0, 3, 2, 2, 2] [6, 5, 0, 0, 0, 0, 0, 0, 0, 0] [6, 5, 4, 5, 6, 0, 6, 5, 4, 5] ''' class Solution(object): def knightDialer(self, N): MOD = 10**9 + 7 moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] dp = [1] * 10 for hops in range(N-1): dp2 = [0] * 10 for node, count in enumerate(dp): for nei in moves[node]: dp2[nei] += count dp = dp2 print(dp) return sum(dp) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: mod = 10**9+7 nxt_pos = [[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[1,7,0],[2,6],[1,3],[2,4]] dp = [1 for _ in range(10)] for _ in range(1,n): new_dp = [0 for _ in range(10)] for val,times in enumerate(dp): for i in nxt_pos[val]: new_dp[i] += times new_dp[i] %= mod dp = new_dp return sum(dp)%mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: moves = {1: {6,8}, 2: {9,7}, 3: {8,4}, 4: {3,9,0}, 5: {}, 6: {1,7,0}, 7: {2,6}, 8: {1,3}, 9:{2,4}, 0: {4,6}} # N = 2 dp = [1] * 10 for i in range(2, N+1): newDp = [0] * 10 for j in range(10): for conn in moves[j]: newDp[conn] += dp[j] % (10**9 + 7) dp = newDp print(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: res = 0 path = {0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[3,9,0], 5:[], 6:[1,7,0], 7:[2,6], 8:[1,3], 9:[2,4]} dp = [1]*10 MOD = 10**9 + 7 for i in range(2,n+1): dp2 = [0]*10 for j in range(10): for p in path[j]: dp2[j] += dp[p] # dp[j] %= MOD dp = dp2 res = sum(dp) return res%MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: reachFrom = {1: [8,6], 2: [7,9], 3: [4,8], 4: [0,9,3], 5:[], 6: [0,1,7], 7: [2,6], 8: [1,3], 9: [2, 4], 0: [4,6]} prevdp = [1 for _ in range(10)] curdp = [0 for _ in range(10)] for iteration in range(1, n): curdp = [0 for _ in range(10)] for i in range(10): for nbor in reachFrom[i]: curdp[i] += prevdp[nbor] prevdp = curdp.copy() ans = 0 for i in range(10): ans += prevdp[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: MOD = 10**9 + 7 if n == 1: return 10 next_square = [[4,6], [6,8], [7,9], [4,8], [3, 9, 0], [], [0, 1, 7], [2,6], [1,3], [2,4], [4,6]] values = [1, 1, 1, 1, 1, 0, 1, 1, 1, 1] for i in range(1, n): next_values = [0] * 10 for num in range(10): for square in next_square[num]: next_values[square] %= MOD next_values[square] += values[num] values = next_values[:] return sum(values) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: steps = {1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0],5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6]} ret = set() def DFS(cur, i): if i == N: n = 0 for c in cur: n = n * 10 +c ret.add(n) print(n) return for n in steps[cur[-1]]: DFS(cur+[n], i+1) return for k in list(steps.keys()): DFS([k], 1) return len(ret) class Solution: def knightDialer(self, N: int) -> int: steps = {1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6]} dp = [1 for _ in range(10)] for _ in range(N-1): cur = [0 for _ in range(10)] for i in range(10): for j in steps[i]: cur[j] += dp[i] dp = cur return sum(dp) % (10**9 +7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: ''' 0: 4,6 1: 6,8 2: 7,9 3: 4,8 4: 0,3,9 5: None 6: 1,7,0 7: 2,6 8: 1,3 9: 2,4 ''' mod = 10**9+7 nxt_pos = [[4,6],[6,8],[7,9],[4,8],[0,3,9],[],[1,7,0],[2,6],[1,3],[2,4]] dp = [1 for _ in range(10)] for _ in range(1,n): new_dp = [0 for _ in range(10)] for val,times in enumerate(dp): for i in nxt_pos[val]: new_dp[i] += times new_dp[i] %= mod dp = new_dp return sum(dp)%mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: # mappings = [[4,6], [6,8], [7,9], [4,8], [0,3,9], [], [1,7,0], [2,6], [1,3], [2,4]] # total = 0 # MOD = 10**9 + 7 # for s in range(0, 10): # last = collections.deque([s]) # for i in range(2, n+1): # length = len(last) # for j in range(length): # number = last.pop() # last.extendleft([] + mappings[number]) # total+=len(last) # return total % MOD mappings = [[4,6], [6,8], [7,9], [4,8], [0,3,9], [], [1,7,0], [2,6], [1,3], [2,4]] mod = 10**9 + 7 dp = [1] * 10 for digit in range(2, n+1): dp2 = [0] * 10 for startNumber, counter in enumerate(dp2): for x in mappings[startNumber]: dp2[startNumber] += dp[x] dp2[startNumber] 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: pos_map = {0:[4,6], 1: [6,8], 2:[7,9], 3:[4,8], 4:[0,3,9],5:[], 6:[0,1,7], 7:[2,6], 8:[1,3], 9:[2,4]} num_vals = [1 if i != 5 else 0 for i in range(10)] mod_val = 10**9+7 if n == 1: return 10 else: for num in range(1,n): temp = [0 for i in range(10)] for i, count in enumerate(num_vals): for val in pos_map[i]: temp[val] += count num_vals = temp return sum(num_vals)%mod_val
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
from collections import deque class Solution: def knightDialer(self, n: int) -> int: neighborMap = { 1: (6,8), 2: (7,9), 3: (4,8), 4: (3,9,0), 5: (), 6: (1,7,0), 7: (2,6), 8: (3,1), 9: (2,4), 0: (4,6) } def neighbors(pos): return neighborMap[pos] def countSeq(): prevMoves = [1] * 10 curMoves = [0] * 10 curN = 1 while curN < n: curMoves = [0] * 10 curN += 1 for pos in range(10): for neighbor in neighbors(pos): curMoves[pos] += prevMoves[neighbor] prevMoves = curMoves curMoves = prevMoves # print(curMoves) return sum(curMoves) # totalSeq = 0 # for curPos in range(10): # totalSeq += countSeq(curPos) return countSeq() % (10**9 + 7) # Prev implementation # def knightDialer(self, n: int) -> int: # def inPad(row, col): # if row >= 0 and row < 3 and col >= 0 and col < 3 or (row == 3 and col == 1): # return True # return False # dRows = [1,-1,1,-1,2,-2,2,-2] # dCols = [2,2,-2,-2,1,-1,-1,1] # q = deque() # q.append((3,1,n-1)) # for i in range(3): # for j in range(3): # q.append((i,j, n-1)) # distinctNums = 0 # while q: # cur = q.popleft() # if cur[2] == 0: # distinctNums += 1 # continue # for dRow, dCol in zip(dRows, dCols): # newRow, newCol = cur[0] + dRow, cur[1] + dCol # if inPad(newRow, newCol): # q.append((newRow, newCol, cur[2] - 1)) # return distinctNums % (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: prev = [0] * 10 dp = [1] * 10 m = { 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 parents(x): return m[x] for l in range(1, n + 1): for x in range(10): for prevX in parents(x): dp[x] += prev[prevX] prev, dp = dp, [0] * 10 return sum(prev) % (10 ** 9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: # mod = 10**9 + 7 # d = dict() # d[0] = {4, 6} # d[1] = {6, 8} # d[2] = {7, 9} # d[3] = {4, 8} # d[4] = {0, 3, 9} # d[6] = {0, 1, 7} # d[7] = {2, 6} # d[8] = {1, 3} # d[9] = {2, 4} # memo = [[0]*10 for i in range(n+1)] # memo[1] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] # for i in range(2, n+1): # for key, val in d.items(): # for v in val: # memo[i][key] += memo[i-1][v] # return sum(memo[-1])%mod 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
# deem the jumps as path from a dummy starting node to each level of a tree. Count the number of nodes in each level # method 1 # dynamic programming # time complexity: O(n) class Solution: def knightDialer(self, n: int) -> int: d = {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)} # start from a dummy node, from which we can reach every node 0-9 # count the number of nodes in each level # if prev_level[i] = freq, then there {freq} nodes that equals {i} in that level prev_level = {i:1 for i in range(10)} # level 1 for j in range(1,n): curr_level = defaultdict(int) for i,freq in list(prev_level.items()): for child in d[i]: curr_level[child] += freq prev_level = curr_level return sum(prev_level.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: possible_paths = {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 i in range(n-1): temp = [0] * 10 for start_num in range(10): for target_num in possible_paths[start_num]: temp[target_num] += dp[start_num] dp = temp return sum(dp) % (7 + 10**9)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: 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 = {0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4]} MOD = 1000000007 prev = [1,1,1,1,1,0,1,1,1,1] for _ in range(n-1): val = [0] * 10 for i in range(10): for x in graph[i]: val[x] += prev[i] prev = val return sum(prev) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n): dmap = [[4, 6], [8, 6], [7, 9], [8, 4], [3, 9, 0], [], [1, 7, 0], [2, 6], [1, 3], [2, 4],[4, 6]] mod_val = 10 ** 9 + 7 ans = 0 dp = [[0]*10 for _ in range(n+1)] for i in range(10): dp[1][i] = 1 for i in range(2, n+1): for j in range(0, 10): tv = dmap[j] for t in tv: dp[i][j] += dp[i-1][t] % mod_val #print(dp) ans = sum(dp[n]) % mod_val 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 = 1000000007 if n == 0: return 0 dict = {} dict[1] = [6, 8] dict[2] = [7, 9] dict[3] = [4, 8] dict[4] = [3, 9, 0] dict[5] = [] dict[6] = [1, 7, 0] dict[7] = [2, 6] dict[8] = [1, 3] dict[9] = [2,4] dict[0] = [4,6] dp = [0] * 10 for i in range(10): dp[i] = [0] * (n + 1) for i in range(10): dp[i][0] = 0 dp[i][1] = 1 sum = 0 for j in range(2, n+1): for i in range(10): for k in dict[i]: dp[i][j] = (dp[i][j] + dp[k][j - 1]) % mod for i in range(0, 10): sum = (sum + dp[i][n]) % mod return sum % mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
import numpy as np class Solution: def knightDialer(self, N: int) -> int: mod = 10**9 + 7 moves = np.matrix([[0, 0, 0, 0, 1, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 1, 0, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 1, 0, 0, 0], [0, 1, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0, 0, 0, 0]]) res = [[1]*10] N -= 1 while N: if N % 2 != 0: res = res * moves % mod N -= 1 else: moves = moves * moves % mod N //= 2 return int(np.sum(res)) % mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: # dp[i, j] = # of distinct phone numbers of length j that can be dialed starting at i # dp[i, 1] = 1 # dp[i, j] = sum(dp[i-1, k]) where k are all the possible numbers the knight can jump to if n == 1: return 10 dp_old = [1 for _ in range(10)] dp_new = [0 for _ in range(10)] possible_next_digits = { 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6] } for _ in range(n-1): for j in range(10): dp_new[j] = 0 for digit in possible_next_digits[j]: dp_new[j] += dp_old[digit] for j in range(10): dp_old[j] = dp_new[j] total = 0 for num in dp_new: total += num return total % (10**9 + 7) # 1 1 1 1 1 1 1 1 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): # Neighbors maps K: starting_key -> V: list of possible destination_keys 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) } current_counts = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] for _ in range(N-1): next_counts = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for src_key in range(10): for dst_key in neighbors[src_key]: next_counts[dst_key] = (next_counts[dst_key] + current_counts[src_key]) % (10**9 + 7) current_counts = next_counts return sum(current_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: # dp[i, j] = # of distinct phone numbers of length j that can be dialed starting at i # dp[i, 1] = 1 # dp[i, j] = sum(dp[i-1, k]) where k are all the possible numbers the knight can jump to if n == 1: return 10 possible_next_digits = { 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 for _ in range(10)] dp_next = [0 for _ in range(10)] for _ in range(n-1): for j in range(10): dp_next[j] = 0 for digit in possible_next_digits[j]: dp_next[j] += dp[digit] for j in range(10): dp[j] = dp_next[j] total = 0 for num in dp: total += num return total % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: if n == 1: return 10 mod = 10**9+7 dc = { (0,1): 1, (1,1): 1, (2,1): 1, (3,1): 1, (4,1): 1, (5,1): 1, (6,1): 1, (7,1): 1, (8,1): 1, (9,1): 1, } for i in range(n-1): dc[(0,i+2)] = (dc[(4,i+1)] + dc[(6,i+1)]) % mod dc[(1,i+2)] = (dc[(6,i+1)] + dc[(8,i+1)]) % mod dc[(2,i+2)] = (dc[(7,i+1)] + dc[(9,i+1)]) % mod dc[(3,i+2)] = (dc[(4,i+1)] + dc[(8,i+1)]) % mod dc[(4,i+2)] = (dc[(0,i+1)] + dc[(3,i+1)] + dc[(9,i+1)]) % mod dc[(5,i+2)] = 0 dc[(6,i+2)] = (dc[(0,i+1)] + dc[(1,i+1)] + dc[(7,i+1)]) % mod dc[(7,i+2)] = (dc[(2,i+1)] + dc[(6,i+1)]) % mod dc[(8,i+2)] = (dc[(1,i+1)] + dc[(3,i+1)]) % mod dc[(9,i+2)] = (dc[(2,i+1)] + dc[(4,i+1)]) % mod res = 0 for j in range(10): res = (res + dc[(j,n)]) % mod return res
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: prev = [0] * 10 dp = [1] * 10 m = { 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 parents(x): return m[x] for l in range(1, n + 1): for x in range(10): for prevX in parents(x): dp[x] += prev[prevX] dp[x] = dp[x] % (10 ** 9 + 7) prev, dp = dp, [0] * 10 return sum(prev) % (10 ** 9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: moveMap = { 0: [4, 6], 1: [8,6], 2: [7,9], 3: [8,4], 4: [3,9,0], 5: [], 6: [1,7,0], 7: [2,6], 8: [1,3], 9: [2,4] } def nextMoves(self, n: int): return self.moveMap[n] def knightDialer(self, n: int) -> int: modulo = 1000000007 if n<0: return 0 counts = [1]*10 moves = len(self.moveMap) while n>1: newCounts = [0]*10 for i in range(moves): for m in self.nextMoves(i): newCounts[m] += counts[i] counts = newCounts n -= 1 return sum(counts) % 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: num_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] } table = [[0 for _ in range(10)] for _ in range(2)] for j in range(10): table[0][j] = 1 for i in range(1, n): for j in range(10): sum_d = 0 for d in num_map[j]: sum_d += table[(i-1)%2][d] table[i%2][j] = sum_d return sum(table[(n-1)%2]) % (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: # 5 can only show up as the last number def knightDialer(self, n: int) -> int: from collections import defaultdict dct = {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:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1, 0:1} # number: curr_sum n -= 1 while n > 0: new_dp = defaultdict(int) for num1 in dp: for num2 in dct[num1]: new_dp[num2] += dp[num1] dp = new_dp n -= 1 ans = 0 for key in dp: ans += dp[key] 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: M = 10**9 + 7 moves = {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]} # f(start, n) = number of ways to build an n-digit number starting # at number start # prior_n[i] = number of ways to build a 1-digit no. starting at i prior_n = [1] * 10 # repeat process to generate array that gives number of # ways to build 2, 3, ... , n digit numbers starting at i for hops in range(2, n + 1): curr_n = [0] * 10 # build on prior n results for num, count in enumerate(prior_n): # looking at numbers generated when we move nbr --> num --> ... for nbr in moves[num]: curr_n[nbr] += count curr_n[nbr] %= M # curr n becomes prior n prior_n = curr_n # return the sum of all n digit phone nums that can be created # by starting at num 0, 1, ... , 9 return sum(prior_n) % 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: validJumps = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] memo = [[0 for _ in range(n + 1)] for _ in range(10)] for i in range(len(memo)): memo[i][1] = 1 for j in range(2, n + 1): for i in range(len(memo)): for k in validJumps[i]: memo[i][j] = (memo[i][j] + memo[k][j - 1]) % (10**9 + 7) ans = 0 for i in range(10): ans = (ans + memo[i][n]) % (10**9 + 7) return ans
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
from functools import lru_cache mapping = [ [4, 6], # 0 [6, 8], # 1 [7, 9], # 2 [4, 8], # 3 [0, 3, 9], # 4 [], # 5 [0, 1, 7], # 6 [2, 6], # 7 [1, 3], # 8 [2, 4] # 9 ] mod = 1000_000_007 @lru_cache(None) def f(n: int, d: int) -> int: if n == 1: return 1 s = 0 for d1 in mapping[d]: s = (s + f(n - 1, d1)) % mod return s class Solution: def knightDialer(self, N: int) -> int: tot = 0 for d in range(10): tot = (tot + f(N, d)) % mod return tot
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: 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, j] = # of distinct phone numbers of length j that can be dialed starting at i # dp[i, 1] = 1 # dp[i, j] = sum(dp[i-1, k]) where k are all the possible numbers the knight can jump to if n == 1: return 10 dp = [1 for _ in range(10)] possible_next_digits = { 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6] } for _ in range(n-1): dp_next = [0] * 10 for j in range(10): dp_next[j] = 0 for digit in possible_next_digits[j]: dp_next[j] += dp[digit] for j in range(10): dp[j] = dp_next[j] total = 0 for num in dp: total += num 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: adj_lists = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],[1,7,0],[2,6],[1,3],[2,4]] #adj_lists[i] are the neighbors of digit i table = [[1]*10 for i in range(n)] #table[n][digit] is the number of solutions for length `n+1` starting at digit #table is already initialized for n=1 for n0 in range(1,n): for digit in range(10): acc = 0 for adj in adj_lists[digit]: acc += table[n0-1][adj] table[n0][digit] = acc return sum(table[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
from collections import defaultdict class Solution: def knightDialer(self, n: int) -> int: _nextMove = {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]} moves = {i : 1 for i in range(10)} while n > 1: newmoves = defaultdict(int) for k, v in moves.items(): for _next in _nextMove[k]: newmoves[_next] += v moves = newmoves n -= 1 return sum(moves.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: positions = [ [4, 6], [6, 8], [7, 9], [4, 8], [0, 3, 9], [], [0, 1, 7], [2, 6], [1, 3], [2, 4] ] dp = [[0 for i in range(n + 1)] for i in range(10)] for i in range(10): dp[i][1] = 1 for j in range(2, n + 1): for i in range(10): for pos in positions[i]: dp[i][j] = (dp[i][j] + dp[pos][j - 1]) % 1000000007 result = 0 for i in range(10): result = (result + dp[i][n]) % 1000000007 return result
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: if n==0: return 0 dic = {0: (4, 6), 1: (8, 6), 2: (7, 9), 3: (4, 8), 4: (3, 9, 0), 5: (), 6: (1, 7, 0), 7: (2, 6), 8: (1, 3), 9: (2, 4)} dp = [1]*10 for _ in range(n-1): new = [0]*10 for i in range(10): for nei in dic[i]: new[i] += dp[nei] new[i] %= (10**9+7) dp = new return sum(dp)%(10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
MOD = 10**9 + 7 num_to_next = { 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [3, 9, 0], 5: [], 6: [1, 7, 0], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6], } class Solution: def knightDialer(self, n: int) -> int: if n == 0: return 0 dp = [1 for _ in range(10)] for _ in range(n-1): next_dp = [0 for _ in range(10)] for current in range(10): for next_pos in num_to_next[current]: next_dp[next_pos] += dp[current] next_dp[next_pos] = next_dp[next_pos] % MOD dp = next_dp return sum(dp) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: 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 ''' 1 2 3 4 5 6 7 8 9 -1 0 -1 def dfs(d, x, y): if d == n: ans.append() # check x, y x + 2, y + 1, y - 1 x - 2, y + 1, y - 1 y + 2, x + 1, x - 1 y - 2, x + 1, x - 1 (2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (-1, 2), (1, -2), (-1, -2) dfs(d + 1, nx, ny) ''' # def dfs(d, x, y): # nonlocal ans # if d == n: # ans += 1 # return # for dx, dy in directions: # nx, ny = x + dx, y + dy # if nx < 0 or nx > M - 1 or ny < 0 or ny > N - 1 or matrix[nx][ny] == -1: # continue # dfs(d + 1, nx, ny) # matrix = [[1, 2, 3], # [4, 5, 6], # [7, 8, 9], # [-1, 0, -1]] # M, N = len(matrix), len(matrix[0]) # directions = [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (-1, 2), (1, -2), (-1, -2)] # ans = 0 # for i in range(M): # for j in range(N): # if matrix[i][j] != -1: # dfs(1, i, j) # return ans % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: MOD = 10**9 + 7 dp = [1] * 10 moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] for hop in range(n - 1): dp2 = [0] * 10 for i, move in enumerate(moves): count = dp[i] for pos in move: dp2[pos] += count dp2[pos] %= 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: if n==1: return 10 numpad = {'0':['4','6'],'1':['6','8'],'2':['7','9'],'3':['4','8'],'4':['0','3','9'],'6':['0','1','7'],'7':['2','6'],'8':['1','3'],'9':['4','2']} dp={} for i in ['0','1','2','3','4','6','7','8','9']: dp[i]=[] dp[i].append(1) for i in range(1,n): for j in ['0','1','2','3','4','6','7','8','9']: dp[j].append(0) for k in numpad[j]: dp[j][i]+=dp[k][i-1] dp[j][i]%=(1000000007) res=0 for i in ['0','1','2','3','4','6','7','8','9']: res+=dp[i][n-1] res%=(1000000007) return res
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
import collections class Solution: def knightDialer(self, m: int) -> int: MOD=10**9+7 n={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]} end={} for ab in range(10): end[ab]=1 for a in range(1,m): end2={} for ab in range(10): end2[ab]=0 for key in range(10): val=n[key] # print(val) for c in val: end2[key]+=end[c] # print(end2) end=end2 return (sum(end.values()))%MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
import logging LOG = logging.getLogger(__name__) MODULO = 10**9 + 7 class Solution: def knightDialer(self, n: int) -> int: if n == 1: return 10 counts = [2, 2, 3, 2] for _ in range(n-2): next_counts = [ (counts[1] + counts[2]), (2 * counts[0]), (2 * counts[0] + counts[3]), (2 * counts[2]) ] counts= next_counts return (4 * counts[0] + 2 * counts[1] + 2 * counts[2] + counts[3]) % 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 if n==1: return 10 neighbors={ 0: (4,6), 1: (6,8), 2: (7,9), 3: (8,4), 4: (3,9,0), 5: tuple(), 6: (7,1,0), 7: (2,6), 8: (1,3), 9: (4,2), } prev_jump=[1]*10 curr_jump=[0]*10 jump=0 for jump in range(0,n-1): curr_jump=[0]*10 for num in range(0,10): for i in neighbors[num]: curr_jump[i]+=prev_jump[num] curr_jump[i]%=MOD prev_jump=curr_jump return sum(curr_jump)%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: num_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] } table = [[0 for _ in range(10)] for _ in range(n)] for j in range(10): table[0][j] = 1 for i in range(1, n): for j in range(10): sum_d = 0 for d in num_map[j]: sum_d += table[i-1][d] table[i][j] = sum_d return sum(table[n-1]) % (1000000007)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[], [1,7,0],[2,6],[1,3],[2,4]] # dp[i] denotes the number of combinations starting from i dp = [1] * 10 for n in range(N-1): newdp = [0] * 10 for i in range(10): for j in moves[i]: newdp[i] += dp[j] newdp[i] %= (10**9 + 7) dp = newdp return sum(dp) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: if n == 1: return 10 d = {0:[4, 6], 1:[6, 8], 2:[7, 9], 3:[4, 8], 4:[0, 3, 9], 5:[], 6:[0, 1, 7], 7:[2, 6], 8:[1, 3], 9:[2, 4] } dp = [1] * 10 for i in range(1, n): dp_l = [0] * 10 for sq, count in enumerate(dp): for neigh in d[sq]: dp_l[neigh] += count dp_l[neigh] %= (10**9 + 7) dp = dp_l return sum(dp) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: d = { 0: [4,6], 1: [6,8], 2: [9,7], 3: [4,8], 4: [3,9,0], 5: [], 6: [1,7,0], 7: [6,2], 8: [3,1], 9: [4,2] } res = 0 dp = [1]*10 for x in range(1,n): temp = [0]*10 for i in range(10): for j in d[i]: temp[j] += dp[i] dp = temp 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 = [[] 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(0): xmoves += dialer(i,n) print(('i ',i,'moves',dialer(i,n),'tot ',xmoves)) #return xmoves % (10**9+7) prev = [1] * 10 for hop in range(2,n+1): #print('zzzzzzzzzz') #print('prev ',prev) #print('curr ',curr) curr = [0] * 10 for i in range(10): for move in moves[i]: curr[i] += prev[move] #print('i ',i,'curr[i] ',curr[i],'prev ',prev[i],'move') #print('prev ',prev) #print('curr ',curr) prev = curr return sum(prev) % (10**9 +7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knight(self, n, current, memo): knight_mp = {1: {8, 6}, 2: {9, 7}, 3: {4, 8}, 4: {3, 9, 0}, # 0 also 5: {}, 6: {7, 1, 0}, # 0 also 7: {2, 6}, 8: {3, 1}, 9: {2, 4}, 0: {6, 4}} if n==1: return 1 if (current, n) in memo: return memo[(current, n)] res = 0 for item in knight_mp.get(current, set()): res+=self.knight(n-1, item, memo) memo[(current, n)] = res return res def knightDialer(self, n: int) -> int: # res = 0 # mod = 10**9+7 # memo = {} # for i in range(0, 10): # res+=self.knight(n, i, memo)%mod # return res%mod knight_mp = {1: {8, 6}, 2: {9, 7}, 3: {4, 8}, 4: {3, 9, 0}, # 0 also 5: {}, 6: {7, 1, 0}, # 0 also 7: {2, 6}, 8: {3, 1}, 9: {2, 4}, 0: {6, 4}} # dp = [[0 if i!=0 else 1 for _ in range(0, 10)] for i in range(n)] # mod = 10**9+7 # for step in range(1, n): # for cell in range(0, 10): # for neighbor in knight_mp.get(cell, set()): # dp[step][cell] += dp[step-1][neighbor] # return sum(dp[n-1])%mod prev = [1 for _ in range(0, 10)] mod = 10**9+7 for step in range(1, n): curr = [0 for _ in range(0, 10)] for cell in range(0, 10): for neighbor in knight_mp.get(cell, set()): curr[cell] += prev[neighbor] prev = curr return sum(prev)%mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: 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]] dp = [1] * 10 for i in range(1, n): new = [0] * 10 for j in range(10): new[j] = sum(dp[k] for k in jump[j]) % MOD dp = new 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
#https://leetcode-cn.com/problems/knight-dialer/solution/4zhuang-tai-dong-tai-gui-hua-pythonjie-kong-jian-f/ markov chain, 这个解法神了 class Solution: def knightDialer(self, N: int) -> int: if N ==1 : return 10 nums = [1,1,1,1] for _ in range(N-1): nums = [nums[1] + nums[2], 2 * nums[0], 2 * nums[0] + nums[3], 2 * nums[2]] return (4 * nums[0] + 2 * nums[1] + 2 * nums[2] + nums[3]) % (10**9+7)