description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
import sys sys.setrecursionlimit(10**6) class Solution: def largestSumCycle(self, N, Edge): dp = [(0) for i in range(N)] cx = [(0) for i in range(N)] solve = [-1] def dfs(n, s, c): cx[n] = c dp[n] = s s += n if Edge[n] != -1: w = Edge[n] if cx[w]: if cx[w] == c: solve[0] = max(solve[0], s - dp[w]) else: dfs(w, s, c) c = 1 for i in range(N): if not cx[i]: dfs(i, 0, c) c += 1 return solve[0]
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN VAR NUMBER
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
class Solution: def largestSumCycle(self, N, Edge): done = set() ans = -1 for i in range(N): if i in done: continue path = [i] d2 = set([i]) x = i cycle = False done.add(i) while True: x = Edge[x] if x == -1: break if x in d2: cycle = True break if x in done: break path.append(x) done.add(x) d2.add(x) if cycle: s = 0 while path: y = path.pop() s += y if y == x: break ans = max(ans, s) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
class Solution: def largestSumCycle(self, N, Edge): cells = [] result = 0 covered = {0} for i in range(N): cells = [i] index = i while True: if Edge[index] == -1 or Edge[index] < i: break elif Edge[index] in cells: eleindex = cells.index(Edge[index]) totalSum = sum(cells[eleindex:]) if totalSum > result: result = totalSum break elif Edge[index] in covered: break else: cells.append(Edge[index]) index = Edge[index] covered.add(index) if result == 0: return -1 return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR WHILE NUMBER IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
class Solution: def largestSumCycle(self, N, Edge): visited = [(0) for i in range(N)] ans = -1 s = 0 cur = 0 cycle = False for i in range(N): cur = i s = 0 cycle = 0 while cur != -1: if visited[cur] != 0: cycle = 1 if visited[cur] == i + 1 else 0 break visited[cur] = i + 1 cur = Edge[cur] if cycle: s = cur savecur = cur cur = Edge[cur] while cur != savecur: s += cur cur = Edge[cur] ans = max(ans, s) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
class Solution: def largestSumCycle(self, N, Edge): visited = [(False) for i in range(N)] ans = -1 for i in range(N): if not visited[i] and Edge[i] != -1: sum, v = i, Edge[i] result = {} while Edge[v] != -1 and not visited[v]: visited[v] = True result[v] = sum sum += v v = Edge[v] if v in result: sum -= result[v] ans = max(ans, sum) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR DICT WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
class Solution: def largestSumCycle(self, N, Edge): seen = {} for i, e in enumerate(Edge): if e == -1: seen[i] = -1 else: seen[i] = 0 def dfs(node): cycle_sum = 0 cycle_set = {} while node != -1 and node not in cycle_set and seen[node] == 0: cycle_sum += node cycle_set[node] = [cycle_sum, Edge[node]] node = Edge[node] cycle_sum += node if node in cycle_set: cycle_sum -= cycle_set[node][0] for i, next_node in cycle_set.items(): seen[next_node[1]] = cycle_sum return cycle_sum if seen[node] > 0: for i, next_node in cycle_set.items(): seen[next_node[1]] = seen[node] return seen[node] return -1 ans = -1 for i, e in enumerate(Edge): if seen[i] == 0: ans = max(ans, dfs(i)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR RETURN VAR IF VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR RETURN VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
import sys sys.setrecursionlimit(150000) class Solution: def largestSumCycle(self, N, Edge): visited = [False] * N dp_sum = [-1] * N ans = [-1] def dfs_u(src, cur_sum): if dp_sum[src] != -1: ans[0] = max(ans[0], cur_sum - dp_sum[src] + src) return if visited[src]: return visited[src] = True dp_sum[src] = cur_sum + src if Edge[src] != -1: dfs_u(Edge[src], cur_sum + src) dp_sum[src] = -1 for i in range(N): if not visited[i]: dfs_u(i, 0) return ans[0]
IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR RETURN IF VAR VAR RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
import sys sys.setrecursionlimit(1000000) class Solution: def __init__(self): self.answer = -1 def searchCycle(self, current_node, visit_list, cycle_visit_list, edge): visit_list[current_node] = True cycle_visit_list[current_node] = True adj_node = edge[current_node] if adj_node != -1: if visit_list[adj_node] is False: self.searchCycle(adj_node, visit_list, cycle_visit_list, edge) elif cycle_visit_list[adj_node] is True: sum = 0 curr_tmp_node = adj_node while current_node != curr_tmp_node: sum += curr_tmp_node curr_tmp_node = edge[curr_tmp_node] if current_node == curr_tmp_node: sum += curr_tmp_node self.answer = max(self.answer, sum) cycle_visit_list[current_node] = False def largestSumCycle(self, N, Edge): visit_list = [False] * (N + 2) cycle_visit_list = [False] * (N + 2) for i in range(N): if visit_list[i] is False: self.searchCycle(i, visit_list, cycle_visit_list, Edge) return self.answer
IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
class Solution: def solve(self, p, Edge): su = p s = p p = Edge[s] while p != s: su += p p = Edge[p] return su def largestSumCycle(self, N, Edge): visited = [(False) for i in range(N)] answer = 0 for i in range(len(Edge)): if not visited[i]: mp = {} visited[i] = True mp[i] = 0 p = Edge[i] flag = True while p not in mp.keys(): if p == -1 or visited[p]: flag = False break visited[p] = True mp[p] = 0 p = Edge[p] if flag: answer = max(self.solve(p, Edge), answer) if answer == 0: return -1 return answer
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
class Solution: def largestSumCycle(self, N, Edge): nodes = [] visited = set() for node in range(N): cycle = set() while node != -1 and not node in visited: cycle.add(node) node = Edge[node] if node in cycle: nodes.append(node) break visited.update(cycle) maxi = -1 for item in nodes: summa = item node = Edge[item] while node != item: summa += node node = Edge[node] maxi = max(maxi, summa) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
class Solution: def largestSumCycle(self, N, Edge): ans = -1 vis = [-1] * N S = [0] * N label = 0 for i, x in enumerate(Edge): j = i s = 0 while vis[j] == -1: vis[j] = label S[j] = s s += j k = Edge[j] if 0 <= k < N and vis[k] == label: ans = max(ans, s - S[k]) break else: j = k if j < 0 or j >= N: break label += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER RETURN VAR
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
import sys sys.setrecursionlimit(10**6) class Solution: def dfs(self, graph, visited, u): if visited[u] == 2: return -1 elif visited[u] == 1: res, cur = u, u while graph[cur] != u: cur = graph[cur] res += cur return res elif graph[u] != -1: visited[u] = 1 res = self.dfs(graph, visited, graph[u]) visited[u] = 2 return res else: visited[u] = 2 return -1 def largestSumCycle(self, N, Edge): visited = [0] * N res = -1 for u in range(N): res = max(res, self.dfs(Edge, visited, u)) return res
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
class Solution: def largestSumCycle(self, N, Edge): check = [0] * N ans = -1 for i in range(N): if Edge[i] != -1 and check[i] == 0: check[i] = 1 a = {i: 0} s = i curr = Edge[i] while curr not in a and not check[curr]: a[curr] = s s += curr check[curr] = 1 curr = Edge[curr] if curr == -1: break if curr in a: ans = max(ans, s - a[curr]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given a maze with N cells. Each cell may have multiple entry points but not more than one exit(i.e entry/exit points are unidirectional doors like valves). You are given an array Edge[] of N integers, where Edge[i] contains the cell number that can be reached from of cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit. The task is to find the largest sum of a cycle in the maze(Sum of a cycle is the sum of the cell indexes of all cells present in that cycle). Note:The cells are named with an integer value from 0 to N-1. If there is no cycle in the graph then return -1. Example 1: Input: N = 4 Edge[] = {1, 2, 0, -1} Output: 3 Explanation: There is only one cycle in the graph. (i.e 0->1->2->0) Sum of the cell index in that cycle = 0 + 1 + 2 = 3. Example 2: Input: N = 4 Edge[] = {2, 0, -1, 2} Output: -1 Explanation: 1 -> 0 -> 2 <- 3 There is no cycle in the graph. Your task: You dont need to read input or print anything. Your task is to complete the function largestSumCycle() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the sum of the largest sum cycle in the maze. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -1 < Edge[i] < N Edge[i] != i
import sys sys.setrecursionlimit(10**6) class Solution: def dfs(self, node, p=-1): self.vis[node] = 1 self.par[node] = p self.tmp.append(node) for i in self.v[node]: if self.vis[i] == 0: z = self.dfs(i, node) if z != -1: return z elif self.vis[i] == 1: sum = i while node != i: sum += node node = self.par[node] if node == i: return sum return -1 return -1 def largestSumCycle(self, N, Edge): ans = -1 self.vis = [0] * N self.v = [[] for i in range(N)] self.par = [0] * N self.tmp = [] for i in range(N): if Edge[i] != -1: self.v[i].append(Edge[i]) for i in range(N): if ~self.vis[i]: ans = max(ans, self.dfs(i)) for j in self.tmp: self.vis[j] = 2 self.tmp.clear() return ans
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR IF VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def isValid(first, second, others): if len(first) > 1 and first[0] == "0" or len(second) > 1 and second[0] == "0": return 0 sum_str = str(int(first) + int(second)) if sum_str == others: return 1 elif others.startswith(sum_str): return isValid(second, sum_str, others[len(sum_str) :]) else: return 0 def isAdditiveSequence(num): length = len(num) for i in range(1, length // 2 + 1): for j in range(1, length - i // 2 + 1): first, second, others = num[:i], num[i : i + j], num[i + j :] if isValid(first, second, others): return 1 return 0
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def solve(a, b, s, k, n, start=0): if k == n and start > 0: return 1 for i in range(1, n): if k + i > n: break c = int(s[k : k + i]) if c == a + b: return solve(b, c, s, k + i, n, start + 1) elif c > a + b: return 0 return 0 def isAdditiveSequence(num): n = len(num) for i in range(1, n): for j in range(1, n): a = int(num[0:i]) b = int(num[i : i + j]) if solve(a, b, num, i + j, n): return 1 return 0
FUNC_DEF NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def find_seq(n1, n2, s, found): if s == "" and found: return True n3 = str(n1 + n2) idx = min(len(s), len(n3)) if s[:idx] == n3: return find_seq(n2, int(n3), s[idx:], True) return False def isAdditiveSequence(num): for i in range(1, len(num) - 1): n1 = int(num[:i]) if str(n1) != num[:i]: break for j in range(i + 1, len(num)): n2 = int(num[i:j]) if str(n2) != num[i:j]: break found = find_seq(n1, n2, num[j:], False) if found: return 1 return 0
FUNC_DEF IF VAR STRING VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def isAdditiveSequence(num): for i in range(len(num) // 2): a = int(num[: i + 1]) for j in range(i + 1, len(num) - i - 1): b = int(num[i + 1 : j + 1]) summ = str(a + b) idx, flag = 0, True for k in range(j + 1, len(num)): if idx == len(summ): if isAdditiveSequence(num[i + 1 :]): return 1 flag = False break if summ[idx] == num[k]: idx += 1 else: break if idx == len(summ) and flag == True: return 1 return 0
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def isAdditiveSequence(num): def ne(val, k, s): for i in range(k + 1, len(s) + 1): if int(s[k:i]) == val: return i return -1 def ad(prev1, prev2, s): if s == "": return 1 if prev1 == 0: ret = False for i in range(1, len(s) + 1): for j in range(i + 1, len(s) + 1): tmp = ne(int(s[0:i]) + int(s[i:j]), j, s) if tmp != -1: ret |= ad(s[i:j], s[j:tmp], s[tmp:]) return ret else: tmp = ne(int(prev1) + int(prev2), 0, s) if tmp != -1: return ad(prev2, s[0:tmp], s[tmp:]) return False def isad(s): if ad(0, 0, s): return 1 else: return 0 return isad(num)
FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR NUMBER NUMBER VAR RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def check(num, n, fi, fl, first, si, sl, second): n1 = num[fi : fl + 1 : 1] n2 = num[si : sl + 1 : 1] if n1 and n2 and len(n1) == first and len(n2) == second: sum = int(int(n1) + int(n2)) third = len(str(sum)) ri = sl + 1 rl = ri + third - 1 res = num[ri : rl + 1 : 1] if res and int(res) == sum: return check(num, n, si, sl, second, ri, rl, third) elif not res: return True else: return False def index(num, n, first, second): fi = 0 fl = first - 1 si = fl + 1 sl = si + second - 1 return check(num, n, fi, fl, first, si, sl, second) def isAdditiveSequence(num): n = len(num) first = int(n / 2) for i in range(1, first + 1): second = int((n - i) / 2) for j in range(1, second + 1): if index(num, n, i, j): return 1 return 0
FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def solve(i, nums, s, lst=[]): if i >= len(nums) and s <= 2: return False if i >= len(nums): return True ans = False for j in range(i, len(nums)): if s < 2: lst.append(nums[i : j + 1]) ans = ans or solve(j + 1, nums, s + 1, lst) lst.pop() elif int(nums[i : j + 1]) == int(lst[-1]) + int(lst[-2]): lst.append(nums[i : j + 1]) ans = ans or solve(j + 1, nums, s + 1, lst) lst.pop() return ans def isAdditiveSequence(num): if solve(0, num, 0): return 1 return 0
FUNC_DEF LIST IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
n = 0 def dfs(s, a, l): ans = 0 if a == n: return 1 if len(l) >= 3 else 0 for i in range(a, n): ans = ans * 10 + int(s[i]) if ans == 0: if len(l) < 2: l.append(ans) b = dfs(s, a + 1, l) l.pop() return b elif ans == l[-1] + l[-2]: l.append(ans) b = dfs(s, a + 1, l) l.pop() return b else: return 0 elif len(l) < 2: l.append(ans) if dfs(s, i + 1, l) == 1: return 1 l.pop() elif ans == l[-1] + l[-2]: l.append(ans) if dfs(s, i + 1, l) == 1: return 1 l.pop() elif ans > l[-1] + l[-2]: return 0 return 0 def isAdditiveSequence(s): global n n = len(s) return dfs(s, 0, [])
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER LIST
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def isAdditiveSequence(num): def rec(first, second, rest): if len(rest) == 0: return True total = str(first + second) l = len(total) if total == rest[:l]: return rec(second, int(total), rest[l:]) for i in range(1, len(num) - 1): for j in range(i + 1, len(num)): first = num[:i] second = num[i:j] if ( first[0] == "0" and len(first) > 1 or second[0] == "0" and len(second) > 1 ): break if rec(int(first), int(second), num[j:]): return 1 return 0
FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def checkIsAdditive(nums): if len(nums) < 3: return True for i in range(2, len(nums)): if int(nums[i - 2]) + int(nums[i - 1]) != int(nums[i]): return False return True def isAdditiveSequence(num): additiveSubsequenceFound = False def recur(str, sub): nonlocal additiveSubsequenceFound if len(str) == 0: if len(sub.split(" ")) >= 3 and checkIsAdditive(sub.split(" ")): additiveSubsequenceFound = True return if additiveSubsequenceFound: return if len(sub) != 0 and checkIsAdditive(sub.split(" ")): recur(str[1:], sub + " " + str[0]) recur(str[1:], sub + str[0]) recur(str(num), "") return 1 if additiveSubsequenceFound else 0
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER RETURN IF VAR RETURN IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING RETURN VAR NUMBER NUMBER
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
n = 0 def find(s, a, b, c, d): ans = 0 if a == n: return 1 if d >= 3 else 0 for i in range(a, n): ans = ans * 10 + int(s[i]) if ans == 0: if b == -1 or c == -1: if b == -1: return find(s, a + 1, ans, c, d + 1) else: return find(s, a + 1, b, ans, d + 1) elif b + c == ans: return find(s, a + 1, c, ans, d + 1) else: return 0 elif b == -1 or c == -1: if b == -1: if find(s, i + 1, ans, c, d + 1): return 1 elif find(s, i + 1, b, ans, d + 1): return 1 elif b + c == ans: return find(s, i + 1, c, ans, d + 1) elif ans > b + c: return 0 return 0 def isAdditiveSequence(num): global n n = len(s) return find(num, 0, -1, -1, 0)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR RETURN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def isAdditiveSequence(num): S = num A1 = "" A2 = "" A3 = "" Re = "" F = False for i in range(1, len(S)): for j in range(i + 1, len(S)): A1 = S[:i] A2 = S[i:j] A3 = int(A1) + int(A2) Re = str(int(A1)) + str(int(A2)) + str(int(A3)) if Re == S: F = True break else: while len(Re) <= len(S) and Re in S: A1, A2 = A2, A3 A3 = int(A1) + int(A2) Re = Re + str(int(A3)) if Re == S: F = True break if F: break if F: break if F: return 1 else: return 0
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR IF VAR RETURN NUMBER RETURN NUMBER
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def isAdditiveSequence(num): def f(a, l1, l2): if a == "": return 0 if int(a) == l1 + l2: return 1 for i in range(len(a)): if ( len(a[:i]) > 0 and (int(a[:i]) == l1 + l2 or l2 == 0) and f(a[i:], int(a[:i]), l1) == 1 ): return 1 return 0 return f(str(num), 0, 0)
FUNC_DEF FUNC_DEF IF VAR STRING RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
Given a string, the task is to find whether it contains an additive sequence or not. A string contains an additive sequence if its digits can make a sequence of numbers in which every number is addition of previous two numbers. You are required to complete the function which returns true if the string is a valid sequence else returns false. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow . Each test case contains a string s . Output: For each test case in a new line output will be 1 if it contains an additive sequence and 0 if it doesn't contains an additive sequence. Constraints: 1<=T<=100 1<=length of string<=200 Example: Input 3 1235813 1711 199100199 Output 1 0 1 Explanation: 1. In first test case series will be (1 2 3 5 8 13 ) as 1+2=3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13 . 2. In the second test case there is no such series possible. 3. In the third test case there is the series will be 1 99 100 199 . Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def rec(s, beg, l1, l2): s1 = s[beg : beg + l1] s2 = s[beg + l1 : beg + l1 + l2] s3 = str(int(s1) + int(s2)) l3 = len(s3) if beg + l1 + l2 + l3 > len(s): return False elif s3 == s[beg + l1 + l2 : beg + l1 + l2 + l3]: if beg + l1 + l2 + l3 == len(s): return True return rec(s, beg + l1, l2, l3) return False def isAdditiveSequence(num): S = str(num) for i in range(1, len(S)): for j in range(1, len(S) - i): if rec(S, 0, i, j): return 1 return 0
FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively. Example: Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
class Solution: def solveNQueens(self, n): result = [] visits = [0, 0, 0] board = [["." for j in range(n)] for i in range(n)] self.find(visits, n, 0, board, result) return result + [ [row[::-1] for row in board] for board in result if n & 1 == 0 or n & 1 == 1 and board[0][n >> 1] != "Q" ] def find(self, visits, n, k, board, result): if k >= n: result.append(["".join(x for x in row) for row in board]) return for j in range(n if k > 0 else n + 1 >> 1): if self.isInvalid(visits, k, j, n): continue self.toggleVisit(visits, k, j, n) board[k][j] = "Q" self.find(visits, n, k + 1, board, result) self.toggleVisit(visits, k, j, n) board[k][j] = "." def toggleVisit(self, visits, i, j, n): visits[0] ^= 1 << j visits[1] ^= 1 << i - j + n - 1 visits[2] ^= 1 << i + j def isInvalid(self, visits, i, j, n): return ( visits[0] & 1 << j > 0 or visits[1] & 1 << i - j + n - 1 > 0 or visits[2] & 1 << i + j > 0 )
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER STRING FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING FUNC_DEF VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR NUMBER
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively. Example: Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
class Solution: def solveNQueens(self, n): def dfs(queens, xy_dif, xy_sum): p = len(queens) if p == n: res.append(queens) return for i in range(n): if i not in queens and p - i not in xy_dif and p + i not in xy_sum: dfs(queens + [i], xy_dif + [p - i], xy_sum + [p + i]) res = [] dfs([], [], []) return [[("." * i + "Q" + "." * (n - i - 1)) for i in sol] for sol in res]
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR BIN_OP VAR LIST BIN_OP VAR VAR BIN_OP VAR LIST BIN_OP VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST LIST LIST RETURN BIN_OP BIN_OP BIN_OP STRING VAR STRING BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively. Example: Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
class Solution: def solveNQueens(self, n): def dfs(n, a, b, c, ans, d): if n == 0: ans = ans.append([(i * "." + "Q" + (len(a) - i - 1) * ".") for i in d]) return None for i in range(len(a)): if a[i] or b[n - 1 + i] or c[len(a) - 1 - i + n - 1]: continue a[i], b[n - 1 + i], c[len(a) - 1 - i + n - 1] = True, True, True d[n - 1] = i dfs(n - 1, a, b, c, ans, d) a[i], b[n - 1 + i], c[len(a) - 1 - i + n - 1] = False, False, False return None a = [(False) for i in range(n)] b = [(False) for i in range(n * 2 - 1)] c = [(False) for i in range(n * 2 - 1)] d = [(0) for i in range(n)] ans = [] dfs(n, a, b, c, ans, d) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR STRING STRING BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER STRING VAR VAR RETURN NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER NUMBER RETURN NONE ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively. Example: Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
class Solution: def __init__(self): self.result = [] self.visited = None def solveNQueens(self, n): self.visited = [(0) for i in range(n)] self.__solve_n_queues(n, 0, []) return self.result @staticmethod def __not_diagonal(x, y, curr_answer): for c_x, c_y in enumerate(curr_answer): if abs(c_x - x) == abs(c_y - y): return False return True @staticmethod def visualize(curr_answer, n): answer = [] for pos in curr_answer: answer.append("." * pos + "Q" + "." * (n - pos - 1)) return answer def __solve_n_queues(self, n, level, curr_answer): if level == n: self.result.append(Solution.visualize(curr_answer, n)) return for i in range(n): if not self.visited[i] and Solution.__not_diagonal(level, i, curr_answer): self.visited[i] = 1 curr_answer.append(i) self.__solve_n_queues(n, level + 1, curr_answer) curr_answer.pop() self.visited[i] = 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NONE FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER LIST RETURN VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR STRING BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively. Example: Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
class Solution: def solveNQueens(self, n): queens = [] res = [] def backtracking(n, k, queens): if k > n: return elif len(queens) == n and k < n: return elif len(queens) == n and k == n: temp = queens[:] res.append(temp) else: row = [0] * n temp_q = queens[:] for each in temp_q: x, y = each[0], each[1] row[y] = 1 if y + k - x < n: row[y + k - x] = 1 if y - k + x >= 0: row[y - k + x] = 1 for i in range(n): if row[i] == 0: temp_q.append((k, i)) backtracking(n, k + 1, temp_q) temp_q.pop() backtracking(n, 0, queens) layout = [] for q in res: temp = [] for each in q: _row = "." * each[1] + "Q" + "." * (n - each[1] - 1) temp.append(_row) layout.append(temp) return layout
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN IF FUNC_CALL VAR VAR VAR VAR VAR RETURN IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR NUMBER STRING BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively. Example: Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
class Solution: def __init__(self): self.board = [] self.col_status = [] self.row_status = [] self.dia_status_1 = [] self.dia_status_2 = [] self.result = [] def placeQueen(self, row, n): if row == n: tmp = ["".join(el) for el in self.board] self.result.append(tmp) return for i in range(n): if ( self.col_status[i] and self.dia_status_1[i + row] and self.dia_status_2[row - i] ): self.board[row][i] = "Q" self.col_status[i] = False self.dia_status_1[i + row] = False self.dia_status_2[row - i] = False self.placeQueen(row + 1, n) self.board[row][i] = "." self.col_status[i] = True self.dia_status_1[i + row] = True self.dia_status_2[row - i] = True def solveNQueens(self, n): self.board = [["." for i in range(n)] for j in range(n)] self.col_status = [(True) for i in range(n)] self.row_status = [(True) for i in range(n)] self.dia_status_1 = [(True) for i in range(2 * n - 1)] self.dia_status_2 = [(True) for i in range(2 * n - 1)] self.placeQueen(0, n) return self.result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively. Example: Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
class Solution: def helper(self, n, currentIndex, aux, rowsWithQueen, alll): for i in range(n): if i not in rowsWithQueen: x, y = currentIndex - 1, i - 1 while aux[x][y] != "Q" and x >= 0 and y >= 0: x -= 1 y -= 1 if x != -1 and y != -1: continue x, y = currentIndex - 1, i + 1 while x >= 0 and y < n and aux[x][y] != "Q": x -= 1 y += 1 if x != -1 and y != n: continue aux[currentIndex][i] = "Q" rowsWithQueen.append(i) if currentIndex == n - 1: aw = [[(0) for i in range(n)] for j in range(n)] for a in range(n): for b in range(n): aw[a][b] = aux[a][b] alll.append(aw) else: self.helper(n, currentIndex + 1, aux, rowsWithQueen, alll) aux[currentIndex][i] = "." rowsWithQueen.pop() def solveNQueens(self, n): aux = [["." for i in range(n)] for j in range(n)] rowsWithQueen = [] alll = [] self.helper(n, 0, aux, rowsWithQueen, alll) aux = [] for sol in alll: ax = [] for i in sol: i = "".join(i) ax.append(i) aux.append(ax) return aux
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively. Example: Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
class Solution: def solveNQueens(self, n): row = [x for x in range(n)] table = [] for i in range(n): table.append(row.copy()) result = self.solve(table, 0) result_str = [] for x in result: table_str = [] for y in x: table_str.append("." * y + "Q" + "." * (n - y - 1)) result_str.append(table_str) return result_str def solve(self, table, n): if table[n] == []: return [] elif n == len(table) - 1: table[n] = table[n][0] return [table] else: result = [] for x in table[n]: table1 = [(x if type(x) == int else x.copy()) for x in table] table1[n] = x for row in range(n + 1, len(table)): if x in table1[row]: table1[row].remove(x) if x + row - n in table1[row]: table1[row].remove(x + row - n) if x - (row - n) in table1[row]: table1[row].remove(x - (row - n)) result.extend(self.solve(table1, n + 1)) return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR STRING BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR VAR LIST RETURN LIST IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN LIST VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
N = int(input()) dic = {} for i in range(N): s, v = input().split() v = int(v) dic[s] = v for i in range(int(input())): S = input() m = -20000000009 q = "" for i, j in dic.items(): if i[: len(S)] == S: if j > m: q = i m = j if len(q) == 0: print("NO") else: print(q)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
n = int(input()) a = [] b = [] for _ in range(n): s = input().split() a.append(s[0]) b.append(int(s[1])) q = int(input()) for i in range(q): x = input() m = -(10**10) for k in range(n): y = a[k] if x in y and x == y[0 : len(x)]: m = max(m, b[k]) if m == -(10**10): print("NO") else: j = b.index(m) print(a[j])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
nr = int(input()) d = {} for r in range(nr): s, v = map(str, input().split()) d[int(v)] = s q = int(input()) lis = [] for i in range(q): lis.append(input()) l = list(d.keys()) l.sort(reverse=True) ans = "NO" for j in lis: ans = "NO" for k in l: if len(j) <= len(d[k]): a = d[k] if j == a[0 : len(j)]: ans = a break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
n = int(input()) l = [] while n > 0: n -= 1 k, v = input().split(" ") v = int(v) l.append([k, v]) q = int(input()) while q > 0: q -= 1 s = input() max = -1000000001 ans = "" for k, v in l: if k.startswith(s) and v > max: max = v ans = k if ans == "": print("NO") else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
di = {} li = [] lis = [] for i in range(int(input())): s, n = map(str, input().split()) di[n] = s li.append(int(n)) li.sort(reverse=True) for i in li: lis.append(di[str(i)]) for i in range(int(input())): q = input() for j in range(len(lis)): if q in lis[j] and q == lis[j][0 : len(q)]: print(lis[j]) break else: print("NO")
ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
cookbook = dict() for i in range(int(input())): foo = input().split() priority = int(foo[1]) cookbook[priority] = foo[0] it = [cookbook[i] for i in sorted(cookbook, reverse=True)] for i in range(int(input())): prefix = input().rstrip() for j in it: if j.startswith(prefix): print(j) break else: print("NO")
ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
r = [] for _ in range(int(input())): s, i = input().split() r.append((s, int(i))) r = sorted(r, key=lambda x: x[1], reverse=True) for _ in range(int(input())): inp = input() done = 0 for s, i in r: if s[: len(inp)] == inp: print(s) done = 1 break if not done: print("NO")
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
class Recipe: def __init__(self, text, priority): self.text = text self.priority = priority N = int(input()) recipes = [] for n in range(N): line = input().split() recipes.append(Recipe(line[0], int(line[1]))) recipes.sort(key=lambda a: a.priority, reverse=True) Q = int(input()) output = [] for q in range(Q): recipe_text = input() recipe_index = -1 for r in range(len(recipes)): if recipes[r].text.startswith(recipe_text): recipe_index = r break if recipe_index == -1: output.append("NO") else: output.append(recipes[recipe_index].text) print("\n".join(output))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
def keyFn(tp): return tp[1] n = int(input()) recipes = [] i = 0 while i < n: line = input().split() recipes.append((line[0], int(line[1]))) i += 1 q = int(input()) i = 0 srec = sorted(recipes, key=keyFn, reverse=True) while i < q: name = input() ans = "NO" for items in srec: if items[0].startswith(name): ans = items[0] break print(ans) i += 1
FUNC_DEF RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
class TrieNode: def __init__(self): self.children = [None] * 27 self.ispresent = 0 self.priority = "aa" self.index = -1 class Trie: def __init__(self): self.root = self.getnode() def getnode(self): return TrieNode() def getind(self, ch): if ch == "-": return 26 return ord(ch) - 97 def insert(self, key, pos, pri): curr = self.root l = len(key) for i in range(l): place = self.getind(key[i]) if curr.children[place] == None: curr.children[place] = self.getnode() curr = curr.children[place] curr.index = pos curr.priority = pri else: curr = curr.children[place] if pri > curr.priority: curr.priority = pri curr.index = pos curr.ispresent = 1 def search(self, key): curr = self.root l = len(key) for i in range(l): place = self.getind(key[i]) if curr.children[place] == None: return -1 curr = curr.children[place] return curr.index data = Trie() n = int(input()) strings = [] for i in range(n): string, pri = input().split() pri = int(pri) data.insert(string, i, pri) strings.append(string) q = int(input()) for i in range(q): start = input() ans = data.search(start) if ans == -1: print("NO") else: print(strings[ans])
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF IF VAR STRING RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NONE RETURN NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
n = int(input()) slist = [] plist = [] while n > 0: n = n - 1 s, p = input().split() slist.append(s) plist.append(p) r = int(input()) while r > 0: r = r - 1 c = -10000000000 flag = 0 s1 = input() for i in range(len(slist)): dummy = slist[i] v = dummy.find(s1) if v == 0: if int(c) < int(plist[i]): dummy1 = dummy c = plist[i] flag = 1 if flag == 1: print(dummy1) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
n = int(input()) recipes = {} for i in range(n): recipe, priority = input().split() priority = int(priority) recipes[recipe] = priority q = int(input()) for i in range(q): query = input().strip() max_priority = -(10**9) - 1 max_recipe = "NO" for recipe in recipes: if recipe.startswith(query) and recipes[recipe] > max_priority: max_priority = recipes[recipe] max_recipe = recipe print(max_recipe)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
n = int(input()) d = dict() for _ in range(n): aa = input().split() s = aa[0] v = int(aa[1]) d[s] = v q = int(input()) for _ in range(q): qi = input() value = None recep = None for i in d: if i.startswith(qi): if value is None or value < d[i]: value = d[i] recep = i if recep == None: print("NO") else: print(recep)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR VAR IF FUNC_CALL VAR VAR IF VAR NONE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NONE EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
N = int(input()) temp_list = [] for i in range(N): r, p = input().split() temp_list.append((r, int(p))) rec_list = sorted(temp_list, key=lambda lis: lis[1], reverse=True) Q = int(input()) for i in range(Q): current_rec = "NO" current_rec_pri = -(10**9) sfc = input() sfc_len = len(sfc) for j in range(N): rec = rec_list[j][0] rec_pri = rec_list[j][1] if sfc == rec[:sfc_len] and rec_pri >= current_rec_pri: current_rec = rec current_rec_pri = rec_pri break print(current_rec)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
a = int(input()) b = [] for _ in range(a): aa = input().split() b.append((aa[0], int(aa[1]))) b = sorted(b, key=lambda x: x[1], reverse=True) ab = int(input()) for i in range(ab): cc = input() for i in b: if len(cc) <= len(i[0]) and i[0][: len(cc)] == cc: print(i[0]) break else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
n = int(input()) li = [] for i in range(n): string, pri = input().split() pri = int(pri) li.append([string, pri]) li.sort(key=lambda x: x[1], reverse=True) q = int(input()) for i in range(q): start = input() f = 0 for i in range(n): if li[i][0].startswith(start): f = 1 print(li[i][0]) break if not f: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
import sys t = int(input()) list1 = [] for _ in range(t): s = input() n = len(s) num = "" for i in range(len(s)): if s[n - i - 1] == " ": list1.append([s[0 : n - i - 1], int(num)]) break else: num = s[n - i - 1] + num n = int(input()) for _ in range(n): s = input() x = len(s) temp = 0 s_final = "" j = 0 min_num = -1 * sys.maxsize for j in range(t): y = list1[j][0] for i in range(x): if s[i] == y[i]: temp = temp + 1 else: break if temp == x and min_num < list1[j][1]: min_num = list1[j][1] s_final = y temp = 0 if min_num == -1 * sys.maxsize: print("NO") else: print(s_final)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
n = int(input()) s = [] a = [] for i in range(n): si, vi = input().split() vi = int(vi) a.append(vi) s.append(si) q = int(input()) for j in range(0, q): qi = input() ma = -20000000009 pos = -1 for k in range(0, n): if s[k].startswith(qi): if a[k] > ma: pos = k ma = a[k] if pos == -1: print("NO") else: print(s[pos])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
def twstr(): recipes = dict() for n in range(int(input())): recipe, priority = input().strip().split() priority = int(priority) recipes[priority] = recipe for q in range(int(input())): start = input().strip() recipe = "NO" for prio in sorted(recipes.keys(), reverse=True): if recipes[prio].startswith(start): recipe = recipes[prio] break print(recipe) twstr()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
n = int(input()) recipe = [] for _ in range(n): si, vi = map(str, input().split()) recipe.append([si, int(vi)]) recipe.sort(key=lambda x: x[1], reverse=True) for i in range(int(input())): q = input() for j in range(len(recipe)): if q == recipe[j][0][: len(q)]: print(recipe[j][0]) break else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
res = [] for _ in range(int(input())): a, b = map(str, input().split()) res.append([a, int(b)]) res.sort(key=lambda x: x[-1]) for _ in range(int(input())): check = input().strip() f = 1 for i in range(len(res) - 1, -1, -1): if res[i][0][: len(check)] == check: f = 0 ans = res[i][0] break if f == 1: print("NO") elif f == 0: print(ans)
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
numrec = int(input()) reclst = {} recval = [] for i in range(numrec): p, s = input().split(" ") reclst.update({s: p}) querynum = int(input()) for j in range(querynum): query = input() recval = [int(key) for key, value in reclst.items() if value.startswith(query)] recval.sort(reverse=True) try: value1 = recval[0] print(reclst.get(str(value1))) recval.clear() except: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR DICT VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries. Jessie’s queries are as follows: She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority. Note: Every recipe has a unique priority -----Input----- First line contains an integer N - the number of recipes. Followed by N strings Si along with an integer each Vi. Si stands for the recipe and Vi for the priority. It is followed by an integer Q - the number of queries. Followed by Q strings Qi. Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'. -----Output----- Q – lines, each contain the answer for each of the query. If for a query no recipe matches print "NO". (Without quotes) Constraints: 0 <= N <= 1000 0 <= Q <= 1000 -10^9 <= Vi <= 10^9 1 <= |Si| <= 1000 (length of Si) 1 <= |Qi| <= 1000 (length of Qi) -----Example----- Input: 4 flour-with-eggs 100 chicken-ham -10 flour-without-eggs 200 fish-with-pepper 1100 6 f flour-with flour-with- c fl chik Output: fish-with-pepper flour-without-eggs flour-with-eggs chicken-ham flour-without-eggs NO
S = [] for _ in range(int(input())): s, v = input().split() v = int(v) S.append([v, s]) S.sort(reverse=True) for _ in range(int(input())): s = input() found = False for i in range(len(S)): if S[i][1].find(s) == 0: print(S[i][1]) found = True break if not found: print("NO")
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def isSafeCell(self, M, n, r, c): return r >= 0 and r < n and c >= 0 and c < n and M[r][c] > 0 def findPathUtil(self, matrix, n, res, r, c): if r == n - 1 and c == n - 1: res[r][c] = 1 return True if self.isSafeCell(matrix, n, r, c): res[r][c] = 1 for i in range(1, matrix[r][c] + 1): if self.findPathUtil(matrix, n, res, r, c + i): return True if self.findPathUtil(matrix, n, res, r + i, c): return True res[r][c] = 0 return False return False def ShortestDistance(self, matrix): n = len(matrix) if n == 1: return [[1]] if matrix[0][0] == 0: return [[-1]] res = [([0] * n) for i in range(n)] if self.findPathUtil(matrix, n, res, 0, 0): return res return [[-1]]
CLASS_DEF FUNC_DEF RETURN VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN LIST LIST NUMBER IF VAR NUMBER NUMBER NUMBER RETURN LIST LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def recur(self, i, j, n, matrix, ans): if i > n - 1 or j > n - 1: return False ans[i][j] = 1 if i == n - 1 and j == n - 1: return True for e in range(1, matrix[i][j] + 1): if self.recur(i, j + e, n, matrix, ans): return True if self.recur(i + e, j, n, matrix, ans): return True ans[i][j] = 0 return False def ShortestDistance(self, matrix): n = len(matrix) ans = [[(0) for i in range(n)] for i in range(n)] if self.recur(0, 0, n, matrix, ans): return ans return [[-1]]
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def find_rat_path(self, i, j, matrix, n, found): if found[0]: return if i == n - 1 and j == n - 1: found[0] = True for r in range(n): for c in range(n): if matrix[r][c] != -1: matrix[r][c] = 0 else: matrix[r][c] = 1 matrix[i][j] = 1 return if matrix[i][j] == 0: return tmp = matrix[i][j] if not found[0]: matrix[i][j] = -1 for k in range(1, tmp + 1): if j + k <= n - 1: self.find_rat_path(i, j + k, matrix, n, found) if i + k <= n - 1: self.find_rat_path(i + k, j, matrix, n, found) if not found[0]: matrix[i][j] = tmp return def ShortestDistance(self, matrix): n = len(matrix) found = [False] self.find_rat_path(0, 0, matrix, n, found) if found[0]: return matrix else: return [[-1]]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN IF VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def isSafe(self, i, j, matrix): n = len(matrix) return i >= 0 and j >= 0 and i < n and j < n and matrix[i][j] != 0 def solve(self, matrix, x, y, sol, n): if x == n - 1 and y == n - 1: sol[x][y] = 1 return True if self.isSafe(x, y, matrix): sol[x][y] = 1 for i in range(1, matrix[x][y] + 1): if self.solve(matrix, x, y + i, sol, n): return True if self.solve(matrix, x + i, y, sol, n): return True sol[x][y] = 0 return False return False def ShortestDistance(self, matrix): n = len(matrix) sol = [[(0) for i in range(n)] for j in range(n)] ans = self.solve(matrix, 0, 0, sol, n) if ans: return sol return [[-1]]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR IF VAR RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def __init__(self): self.matrix = None self.final_hops = None self.cur_hops = None self.min_hops = None def ShortestDistance(self, matrix): self.matrix = matrix self.final_hops = [] self.cur_hops = [] self.min_hops = float("inf") if len(matrix) == 1: return [[1]] self.findShortestDistancePath(0, 0, 0) if self.min_hops == float("inf"): return [[-1]] ans = [[(0) for j in range(len(matrix))] for i in range(len(matrix))] for i, j in self.final_hops: ans[i][j] = 1 ans[len(matrix) - 1][len(matrix) - 1] = 1 return ans def findShortestDistancePath(self, i, j, n_hops): if i == j and j == len(self.matrix) - 1: if n_hops < self.min_hops: self.min_hops = n_hops self.final_hops = self.cur_hops.copy() return self.cur_hops.append((i, j)) for k in range(1, self.matrix[i][j] + 1): if self.isSafe(i, j + k) and len(self.final_hops) == 0: self.findShortestDistancePath(i, j + k, n_hops + k) if self.isSafe(i + k, j) and len(self.final_hops) == 0: self.findShortestDistancePath(i + k, j, n_hops + k) self.cur_hops.pop() def isSafe(self, i, j): if i >= len(self.matrix) or j >= len(self.matrix): return False if self.matrix[i][j] == 0: if i != len(self.matrix) - 1 and j != len(self.matrix) - 1: return False return True if __name__ == "__main__": T = int(input()) for i in range(T): n = int(input()) matrix = [] for i in range(n): a = list(map(int, input().split())) matrix.append(a) ob = Solution() ans = ob.ShortestDistance(matrix) for i in ans: for j in i: print(j, end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR STRING RETURN LIST LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def find(self, matrix, i, j, n, m, path): if i >= n or j >= m or i < 0 or j < 0: return False elif i == n - 1 and j == m - 1: path[i][i] = "1" self.res.append([[*row] for row in path]) path[i][j] = "0" return True elif matrix[i][j] == 0: return False else: steps = matrix[i][j] path[i][j] = "1" for step in range(1, steps + 1): if self.find(matrix, i, j + step, n, m, path) or self.find( matrix, i + step, j, n, m, path ): path[i][j] = "0" return True path[i][j] = "0" return False def ShortestDistance(self, matrix): self.res = [] n = len(matrix) m = len(matrix[0]) path = [["0" for _ in range(m)] for _ in range(m)] self.find(matrix, 0, 0, n, m, path) if len(self.res) == 0: return [[-1]] return self.res[0]
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR STRING RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING RETURN NUMBER ASSIGN VAR VAR VAR STRING RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST NUMBER RETURN VAR NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
def isSafe(maze, x, y): if x >= 0 and x < len(maze) and y >= 0 and y < len(maze) and maze[x][y] != 0: return True return False def solveMaze(maze): sol = [[(0) for i in range(len(maze))] for j in range(len(maze))] if solveMazeUtil(maze, 0, 0, sol) == False: return [[-1]] return sol def solveMazeUtil(maze, x, y, sol): if x == len(maze) - 1 and y == len(maze) - 1: sol[x][y] = 1 return True if isSafe(maze, x, y) == True: sol[x][y] = 1 for i in range(1, len(maze)): if i <= maze[x][y]: if solveMazeUtil(maze, x, y + i, sol) == True: return True if solveMazeUtil(maze, x + i, y, sol) == True: return True sol[x][y] = 0 return False return False class Solution: def ShortestDistance(self, matrix): if len(matrix) == 1: return [[1]] if matrix[0][0] == 0: return [[-1]] return solveMaze(matrix)
FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER RETURN LIST LIST NUMBER RETURN VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST NUMBER IF VAR NUMBER NUMBER NUMBER RETURN LIST LIST NUMBER RETURN FUNC_CALL VAR VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: NO_PATH = [[-1]] def __init__(self): self.ans = None def ShortestDistance(self, matrix): m, n = len(matrix), len(matrix[0]) if m == 1 and n == 1: return [[1]] if matrix[0][0] == 0: return self.NO_PATH m, n = len(matrix), len(matrix[0]) self.ans = None self.find_path((0, 0), matrix, m, n, []) return self.ans if self.ans else self.NO_PATH def find_path(self, cur_pos, matrix, m, n, positions: list): x, y = cur_pos if x == m - 1 and y == n - 1: self.ans = self.create_matrix(positions, m, n) return jumps = matrix[x][y] if jumps == 0: return for k in range(1, jumps + 1): new_pos = [(x, y + k), (x + k, y)] for i, j in new_pos: if i < m and j < n: pos = i, j positions.append(pos) self.find_path(pos, matrix, m, n, positions) positions.pop() if self.ans: return @staticmethod def create_matrix(positions, m, n): mat = [([0] * n) for _ in range(m)] mat[0][0] = 1 for i, j in positions: mat[i][j] = 1 return mat
CLASS_DEF ASSIGN VAR LIST LIST NUMBER FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN LIST LIST NUMBER IF VAR NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR LIST RETURN VAR VAR VAR FUNC_DEF VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR IF VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR RETURN FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): m, n = len(matrix), len(matrix[0]) ans = [] for i in range(m): ans.append([]) for j in range(n): ans[i].append(0) def valid(matrix, i, j): if i < len(matrix) and j < len(matrix[0]): if matrix[i][j] != 0: return True return False def ratmaze(matrix, ans, i, j): if i == len(matrix) - 1 and j == len(matrix[0]) - 1: return True if valid(matrix, i, j): ans[i][j] = 1 for w in range(1, matrix[i][j] + 1): if ratmaze(matrix, ans, i, j + w): return True if ratmaze(matrix, ans, i + w, j): return True ans[i][j] = 0 return False if ratmaze(matrix, ans, 0, 0): ans[-1][-1] = 1 return ans return [[-1]]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
import sys class Solution: def min_jumps(self, dp, n, matrix, i, j): if i < 0 or i >= n or j < 0 or j >= n: return 0 if i == n - 1 and j == n - 1: dp[i][j] = 1 return 1 if matrix[i][j] == 0: return 0 k = 1 while dp[i][j] == 0 and k <= matrix[i][j]: dp[i][j] = self.min_jumps(dp, n, matrix, i, j + k) if dp[i][j] == 0: dp[i][j] = self.min_jumps(dp, n, matrix, i + k, j) k += 1 return dp[i][j] def ShortestDistance(self, matrix): n = len(matrix) if n == 1: print(1) return if matrix[0][0] == 0: print(-1) return dp = [[(0) for i in range(n)] for j in range(n)] c = self.min_jumps(dp, n, matrix, 0, 0) if c == 0: print(-1) else: for i in dp: for j in i: print(j, end=" ") print() sys.setrecursionlimit(10**6) if __name__ == "__main__": t = int(input()) for i in range(t): n = int(input()) matrix = [] for j in range(n): matrix.append(list(map(int, input().split()))) ob = Solution() ob.ShortestDistance(matrix) sys.exit()
IMPORT CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): n = len(matrix) ans = [([0] * n) for i in range(n)] def fun(i, j): if i == n - 1 and j == n - 1: ans[i][j] = 1 return 1 if i >= n or j >= n or matrix[i][j] == 0: return 0 hop = matrix[i][j] for x in range(1, hop + 1): if fun(i, j + x): ans[i][j] = 1 return 1 if fun(i + x, j): ans[i][j] = 1 return 1 return 0 if fun(0, 0): return ans return [[-1]]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR NUMBER NUMBER RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
def isSafe(x, y, n, mat): if x < 0 or y < 0 or x > n - 1 or y > n - 1 or mat[x][y] == 0: return False return True class Solution: def ShortestDistance(self, mat): self.res = [[(0) for i in range(len(mat))] for i in range(len(mat))] n = len(mat) if mat[0][0] == 0 and n != 1: return [[-1]] def path(m, i, j, pathh): if i == n - 1 and j == n - 1: self.res[i][j] = 1 return True if isSafe(i, j, n, mat): self.res[i][j] = 1 for x in range(1, m[i][j] + 1): if path(m, i, j + x, pathh): return True if path(m, i + x, j, pathh): return True self.res[i][j] = 0 return False return False path(mat, 0, 0, "") return self.res
FUNC_DEF IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER VAR NUMBER RETURN LIST LIST NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING RETURN VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, a): i = 0 j = 0 def px(m): for i in range(len(m)): for j in range(len(m[0])): print(m[i][j], end=" ") print() print() sol = [] for _ in range(len(a)): temp = [] for _ in range(len(a[0])): temp.append(0) sol.append(temp) def function(a, i, j, sol): if i == len(a) - 1 and j == len(a[0]) - 1: sol[i][j] = 1 return True if i > len(a) - 1 or j > len(a[0]) - 1: return False if a[i][j] == 0: return False sol[i][j] = 1 for p in range(1, a[i][j] + 1): if function(a, i, j + p, sol) == True: return True if function(a, i + p, j, sol) == True: return True sol[i][j] = 0 return False function(a, i, j, sol) if sol[0][0] == 0: sol = [[-1]] return sol
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER RETURN VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): r = len(matrix) c = len(matrix[0]) self.ans = [[(0) for i in range(r)] for j in range(c)] self.ans[r - 1][c - 1] = 1 if r == 1 and c == 1: return [[1]] if matrix[0][0] == 0: return [[-1]] def start(mat, i, j, r, c): if i >= r or j >= r: return 0 a = mat[i][j] if i == r - 1 and j == c - 1: return 1 elif a == 0: return 0 else: flag = 0 for k in range(1, a + 1): self.ans[i][j] = 1 flag = start(mat, i, j + k, r, c) or start(mat, i + k, j, r, c) if flag: return flag self.ans[i][j] = 0 return flag start(matrix, 0, 0, r, c) return self.ans if __name__ == "__main__": T = int(input()) for i in range(T): n = int(input()) matrix = [] for i in range(n): a = list(map(int, input().split())) matrix.append(a) ob = Solution() ans = ob.ShortestDistance(matrix) for i in ans: for j in i: print(j, end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN LIST LIST NUMBER IF VAR NUMBER NUMBER NUMBER RETURN LIST LIST NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR RETURN VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def __init__(self): self.flag = False def ShortestDistance(self, matrix): n = len(matrix) if n == 1: return [[1]] if matrix[0][0] == 0: return [[-1]] res = [[(0) for i in range(n)] for j in range(n)] def rec(i, j): if i == j and i == n - 1: self.flag = True return if i < 0 or j < 0 or j >= n or i >= n or matrix[i][j] == 0: return for k in range(1, matrix[i][j] + 1): res[i][j] = 1 rec(i, j + k) if self.flag: return rec(i + k, j) if self.flag: return res[i][j] = 0 rec(0, 0) c = 0 for i in res: c += i.count(0) if c == 0: return [[-1]] res[n - 1][n - 1] = 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN LIST LIST NUMBER IF VAR NUMBER NUMBER NUMBER RETURN LIST LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN LIST LIST NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, l): n = len(l) def ss(i, j, t): if i == n - 1 and j == n - 1: t[i][j] = 1 return True if i < 0 or j < 0 or i >= n or j >= n or l[i][j] == 0: return False t[i][j] = 1 for k in range(1, l[i][j] + 1): if ss(i, j + k, t): return True if ss(i + k, j, t): return True t[i][j] = 0 return False t = [[(0) for _ in range(n)] for _ in range(n)] if not ss(0, 0, t): return [[-1]] return t
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER NUMBER VAR RETURN LIST LIST NUMBER RETURN VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): def DeapthFirst(position): x, y = position if 0 <= x < len(matrix) and 0 <= y < len(matrix): if x == len(matrix) - 1 and y == len(matrix[0]) - 1: matrix[x][y] = "1" return 1 elif matrix[x][y] == 0 or type(matrix[x][y]) == str: return 0 else: for jump in range(1, matrix[x][y] + 1): val = DeapthFirst((x, y + jump)) if val == 1: matrix[x][y] = "1" return 1 val = DeapthFirst((x + jump, y)) if val == 1: matrix[x][y] = "1" return 1 matrix[x][y] = 0 return 0 DeapthFirst((0, 0)) if matrix[-1][-1] != "1": return [[-1]] for x, line in enumerate(matrix): for y, val in enumerate(line): if val != "1": matrix[x][y] = 0 else: matrix[x][y] = 1 return matrix
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR STRING RETURN NUMBER IF VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR STRING RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING RETURN LIST LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def helper(self, x, y): if not self.valid(x, y): return self.ans[x][y] = 1 if (x, y) == (self.row - 1, self.col - 1): return True for i in range(1, matrix[x][y] + 1): if self.helper(x, y + i) or self.helper(x + i, y): return True self.ans[x][y] = 0 return False def ShortestDistance(self, matrix): self.row, self.col = len(matrix), len(matrix[0]) self.ans = [[(0) for i in range(self.col)] for i in range(self.row)] self.valid = ( lambda x, y: (x, y) == (self.row - 1, self.col - 1) or -1 < x < self.row and -1 < y < self.col and matrix[x][y] ) if self.helper(0, 0): return self.ans return [[-1]]
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR IF FUNC_CALL VAR NUMBER NUMBER RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def helper(self, matrix, v, i, j): n = len(matrix) if i == n - 1 and j == n - 1: v[i][j] = 1 return True if i < 0 or j < 0 or i >= n or j >= n or matrix[i][j] == 0: return False v[i][j] = 1 for k in range(1, matrix[i][j] + 1): if self.helper(matrix, v, i, j + k): return True if self.helper(matrix, v, i + k, j): return True v[i][j] = 0 return False def ShortestDistance(self, matrix): n = len(matrix) v = [] for i in range(n): v.append([]) for j in range(n): v[i].append(0) if self.helper(matrix, v, 0, 0) is False: return [[-1]] else: return v
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER RETURN LIST LIST NUMBER RETURN VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def inbound(self, i, j, n, matrix): if i < n and j < n and matrix[i][j] != 0: return True return False def ratmaze(self, i, j, matrix, res, n): if i == n - 1 and j == n - 1: res[i][j] = 1 return True if self.inbound(i, j, n, matrix): res[i][j] = 1 for steps in range(1, matrix[i][j] + 1): if self.ratmaze(i, j + steps, matrix, res, n): return True if self.ratmaze(i + steps, j, matrix, res, n): return True res[i][j] = 0 return False def ShortestDistance(self, matrix): res = [[(0) for i in range(len(matrix))] for i in range(len(matrix))] if self.ratmaze(0, 0, matrix, res, len(matrix)): return res return [[-1]]
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): self.M = len(matrix) self.N = len(matrix[0]) result = [[(0) for _ in range(self.N)] for _ in range(self.M)] if self.solveMaze(matrix, 0, 0, result): return result else: return [[-1]] def solveMaze(self, matrix, i, j, result): if i == self.M - 1 and j == self.N - 1: result[i][j] = 1 return True if self.isSafe(matrix, i, j): result[i][j] = 1 for k in range(1, matrix[i][j] + 1): if self.solveMaze(matrix, i, j + k, result): return True if self.solveMaze(matrix, i + k, j, result): return True result[i][j] = 0 return False def isSafe(self, matrix, i, j): return i <= self.M - 1 and j <= self.N - 1 and matrix[i][j] != 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR RETURN VAR RETURN LIST LIST NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF RETURN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): n = len(matrix) cond = True ans = [] arr = [[(0) for _ in range(n)] for _ in range(n)] def fun(i, j): nonlocal cond, ans, arr if i == n - 1 and j == n - 1: arr[i][j] = 1 ans = [[l for l in x] for x in arr] cond = False return if cond == False: return for k in range(1, matrix[i][j] + 1): x = k + j if cond and x < n and matrix[i][x] != 0: arr[i][j] = 1 fun(i, x) arr[i][j] = 0 y = k + i if cond and y < n and matrix[y][j] != 0: arr[i][j] = 1 fun(y, j) arr[i][j] = 0 matrix[-1][-1] = 1 fun(0, 0) return ans if ans != [] else [[-1]]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER RETURN IF VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER RETURN VAR LIST VAR LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): n = len(matrix[0]) ret = [[-1]] mat = [[(0) for j in range(n)] for i in range(n)] if self.Solve_maze(matrix, 0, 0, n, mat) == True: return mat return ret def is_block_valid(self, matrix, x, y, n): if x >= 0 and x < n and y >= 0 and y < n and matrix[x][y] >= 1: return True else: return False def print_solution(self, mat): print(mat) def Solve_maze(self, matrix, x, y, n, mat): if x == n - 1 and y == n - 1: mat[x][y] = 1 return True if mat[x][y] == 1: return False if self.is_block_valid(matrix, x, y, n) == True: mat[x][y] = 1 b_x = x b_y = y for k in range(matrix[x][y]): idx = k + 1 if y + idx < n: if self.Solve_maze(matrix, x, y + idx, n, mat) == True: return True if x + idx < n: if self.Solve_maze(matrix, x + idx, y, n, mat) == True: return True if y - 1 > b_y: if self.Solve_maze(matrix, x, y - 1, n, mat) == True: return True if x - 1 >= b_x: if self.Solve_maze(matrix, x - 1, y, n, mat) == True: return True mat[x][y] = 0 return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def distance(self, matrix, res, n, i, j): if i >= n or j >= n: return False res[i][j] = 1 if i == n - 1 and j == n - 1: return True jump = matrix[i][j] for k in range(1, jump + 1): if self.distance(matrix, res, n, i, j + k): return True if self.distance(matrix, res, n, i + k, j): return True res[i][j] = 0 return False def ShortestDistance(self, matrix): n = len(matrix) res = [([0] * n) for i in range(n)] if self.distance(matrix, res, n, 0, 0): return res else: return [[-1]]
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): n, m = len(matrix), len(matrix[0]) ans = [([0] * m) for _ in range(n)] def solve(i, j): if i == n - 1 and j == m - 1: ans[i][j] = 1 return True if i >= n or i < 0 or j >= m or j < 0 or matrix[i][j] == 0: return False ans[i][j] = 1 for k in range(1, matrix[i][j] + 1): if k > n: continue if solve(i, j + k): return True if solve(i + k, j): return True ans[i][j] = 0 return False if matrix[0][0] == 0 and m != 1: return [[-1]] if solve(0, 0) == False: return [[-1]] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER RETURN LIST LIST NUMBER IF FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN LIST LIST NUMBER RETURN VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): n = len(matrix) ans = [([0] * n) for i in range(n)] exists = 0 def dfs(i, j): nonlocal exists if i == n - 1 and j == n - 1: ans[i][j] = 1 exists = 1 return True if 0 <= i < n and 0 <= j < n and ans[i][j] == 0 and matrix[i][j] != 0: ans[i][j] = 1 hop = matrix[i][j] for k in range(1, hop + 1): if dfs(i, j + k): return True if dfs(i + k, j): return True ans[i][j] = 0 return False dfs(0, 0) return [[-1]] if exists == 0 else ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER RETURN VAR NUMBER LIST LIST NUMBER VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, mat): def issafe(i, j, n, mat): if i >= 0 and i < n and j >= 0 and j < n: return True return False def util(n, mat, i, j, res): if i == n - 1 and j == n - 1: res[i][j] = 1 return True if issafe(i, j, n, mat) and mat[i][j] != 0: res[i][j] = 1 for k in range(1, mat[i][j] + 1): if util(n, mat, i, j + k, res): return True if util(n, mat, i + k, j, res): return True res[i][j] = 0 return False return False n = len(mat) if mat[0][0] == 0 and n != 1: return [[-1]] res = [[(0) for i in range(n)] for j in range(n)] util(n, mat, 0, 0, res) return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER VAR NUMBER RETURN LIST LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR RETURN VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): n = len(matrix) a = [[(0) for i in range(n)] for i in range(n)] if self.tFW(0, 0, n, matrix, a): return a return [[-1]] def tFW(self, i, j, n, matrix, a): if i > n - 1 or j > n - 1: return False a[i][j] = 1 if i == n - 1 and j == n - 1: return True for t in range(1, matrix[i][j] + 1): if self.tFW(i, j + t, n, matrix, a): return True if self.tFW(i + t, j, n, matrix, a): return True a[i][j] = 0 return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR RETURN VAR RETURN LIST LIST NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): def findshorted(matrix, visited, i, j, n, path=0): if i == n - 1 and j == n - 1: visited[i][j] = 1 return visited if 0 <= i < n and 0 <= j < n: if matrix[i][j] != 0 and visited[i][j] == 0: visited[i][j] = 1 for step in range(1, matrix[i][j] + 1): k = findshorted(matrix, visited, i, j + step, n, path + 1) if k: return k k = findshorted(matrix, visited, i + step, j, n, path + 1) if k: return k visited[i][j] = 0 n = len(matrix) visited = [[(0) for _ in range(n)] for _ in range(n)] ans = findshorted(matrix, visited, 0, 0, n) return ans if ans else [[-1]]
CLASS_DEF FUNC_DEF FUNC_DEF NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR IF NUMBER VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR RETURN VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR RETURN VAR VAR LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): m, n = len(matrix), len(matrix[0]) valid = ( lambda x, y: (x, y) == (n - 1, m - 1) or 0 <= x < n and 0 <= y < m and matrix[y][x] != 0 ) ans = [([0] * m) for _ in range(n)] def walk(x, y): if not valid(x, y): return False ans[y][x] = 1 if (x, y) == (n - 1, m - 1): return True for step in range(1, matrix[y][x] + 1): if walk(x + step, y): return True if walk(x, y + step): return True ans[y][x] = 0 return False return ans if walk(0, 0) else [[-1]]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): output = [] totalRows = len(matrix) totalCols = len(matrix[0]) arr = [] def recurRat(row, col, arr): if row == totalRows - 1 and col == totalCols - 1: arr.append([row, col]) return True if ( row < 0 or row >= totalRows or col < 0 or col >= totalCols or matrix[row][col] == 0 ): return False previousState = matrix[row][col] matrix[row][col] = 1 for steps in range(1, previousState + 1): arr.append([row, col]) if recurRat(row, col + steps, arr): return True if recurRat(row + steps, col, arr): return True arr.pop() matrix[row][col] = previousState return False if recurRat(0, 0, arr): for row in range(totalRows): for col in range(totalCols): matrix[row][col] = 0 for row, col in arr: matrix[row][col] = 1 return matrix return [[-1]]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def isSafe(self, matrix, i, j): if i >= len(matrix) or j >= len(matrix) or matrix[i][j] == 0: return False return True def solve(self, matrix, i, j, arr): if i == len(matrix) - 1 and j == len(matrix) - 1: arr[i][j] = 1 return True if self.isSafe(matrix, i, j): arr[i][j] = 1 for step in range(1, matrix[i][j] + 1): if self.solve(matrix, i, j + step, arr): return True if self.solve(matrix, i + step, j, arr): return True arr[i][j] = 0 return False def ShortestDistance(self, matrix): n = len(matrix) arr = [[(0) for i in range(n)] for j in range(n)] if not self.solve(matrix, 0, 0, arr): return [[-1]] return arr
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR RETURN LIST LIST NUMBER RETURN VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): def f(i, j): if i == n - 1 and j == n - 1: ans[i][j] = 1 return True if i > n - 1 or j > n - 1: return False if matrix[i][j] == 0: return False ans[i][j] = 1 jmp = matrix[i][j] for x in range(1, jmp + 1): if f(i, j + x): return True if f(i + x, j): return True ans[i][j] = 0 return False n = len(matrix) ans = [([0] * n) for _ in range(n)] if f(0, 0): return ans return [[-1]]
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER NUMBER RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): n = len(matrix) result = [[(0) for i in range(n)] for j in range(n)] result[0][0] = 1 return result if self.walk(matrix, 0, 0, result) else [[-1]] def walk(self, matrix, x, y, result): n = len(matrix) if x == n - 1 and y == n - 1: result[x][y] = 1 return True if x < n and y < n and matrix[x][y] != 0: for i in range(1, matrix[x][y] + 1): if self.walk(matrix, x, y + i, result) or self.walk( matrix, x + i, y, result ): result[x][y] = 1 return True return False return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR LIST LIST NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, m): def rec(m, i, j, ans): if i == n - 1 and j == n - 1: ans[i][j] = 1 return True if i < 0 or j < 0 or i >= n or j >= n or m[i][j] == 0: return False for x in range(m[i][j]): ans[i][j] = 1 if rec(m, i, j + x + 1, ans): return True if rec(m, i + x + 1, j, ans): return True ans[i][j] = 0 return False ans = [[(0) for i in range(len(m[0]))] for i in range(len(m))] if rec(m, 0, 0, ans): return ans return [[-1]]
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): def dfs(i, j): if i == len(matrix) - 1 and j == len(matrix) - 1: l[i][j] = 1 return True if ( i < 0 or j < 0 or i >= len(matrix) or j >= len(matrix) or matrix[i][j] == 0 ): return False l[i][j] = 1 for k in range(1, matrix[i][j] + 1): if dfs(i, j + k) or dfs(i + k, j): return True l[i][j] = 0 return False l = [([0] * len(matrix)) for i in range(len(matrix))] if not dfs(0, 0): return [[-1]] return l
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER NUMBER RETURN LIST LIST NUMBER RETURN VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): def solve(i, j, res): nonlocal matrix if i == len(res) - 1 and j == len(res[0]) - 1: return True if i < 0 or i >= len(res) or j < 0 or j >= len(res[0]) or matrix[i][j] == 0: return False res[i][j] = 1 status = True for d in range(1, matrix[i][j] + 1): if solve(i, j + d, res): status = False return True if solve(i + d, j, res): status = False return True if status: res[i][j] = 0 return False res = [([0] * len(matrix[0])) for _ in range(len(matrix))] res[-1][-1] = 1 if solve(0, 0, res): res[-1][-1] = 1 return res return [[-1]]
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER RETURN NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER RETURN VAR RETURN LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): n = len(matrix) self.res = [([0] * n) for i in range(n)] def travel(i, j, matrix): if (i, j) == (n - 1, n - 1): self.res[i][j] = 1 return True if i > n - 1 or i < 0 or j < 0 or j > n - 1 or matrix[i][j] == 0: return False jumps = matrix[i][j] self.res[i][j] = 1 for k in range(jumps): if travel(i, j + k + 1, matrix): return True if travel(i + k + 1, j, matrix): return True self.res[i][j] = 0 return False return self.res if travel(0, 0, matrix) else [[-1]]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR LIST LIST NUMBER
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): n = len(matrix) visit = [[(0) for i in range(n)] for i in range(n)] def recsol(row, col): if row == n - 1 and col == n - 1: visit[row][col] = 1 return True if row < n and col < n and matrix[row][col] != 0: visit[row][col] = 1 for i in range(1, matrix[row][col] + 1): if recsol(row, col + i): return True if recsol(row + i, col): return True visit[row][col] = 0 return False if not recsol(0, 0): return [[-1]] return visit
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR NUMBER NUMBER RETURN LIST LIST NUMBER RETURN VAR
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def ShortestDistance(self, matrix): n = len(matrix) m = len(matrix[0]) paths = self.gridHop(0, 0, n, m, matrix, []) if not paths: return [[-1]] output = [([0] * m) for _ in range(n)] for i, j in paths[0]: output[i][j] = 1 return output def gridHop(self, x, y, n, m, grid, path): if x == n - 1 and y == m - 1: path.append((x, y)) return [path] if grid[x][y] == 0: return [] path.append((x, y)) for i in range(1, grid[x][y] + 1): if y + i < m: output = self.gridHop(x, y + i, n, m, grid, path.copy()) if output: return output if x + i < n: output = self.gridHop(x + i, y, n, m, grid, path.copy()) if output: return output return []
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR LIST IF VAR RETURN LIST LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN LIST VAR IF VAR VAR VAR NUMBER RETURN LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR IF VAR RETURN VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR IF VAR RETURN VAR RETURN LIST
A Maze is given as n*n matrix of blocks where source block is the upper left most block i.e., matrix[0][0] and destination block is lower rightmost block i.e., matrix[n-1][n-1]. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward. In the maze matrix, 0 means the block is the dead end and non-zero number means the block can be used in the path from source to destination. The non-zero value of mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j]. In this variation, Rat is allowed to jump multiple steps at a time instead of 1. Find a matrix which describes the position the rat to reach at the destination. Example: Input: {{2,1,0,0},{3,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{1,0,0,0},{1,0,0,1},{0,0,0,1}, {0,0,0,1}} Explanation: Rat started with matrix[0][0] and can jump up to 2 steps right/down. First check matrix[0][1] as it is 1, next check matrix[0][2] ,this won't lead to the solution. Then check matrix[1][0], as this is 3(non-zero) ,so we can make 3 jumps to reach matrix[1][3]. From matrix[1][3] we can move downwards taking 1 jump each time to reach destination at matrix[3][3]. Example 2: Input: {{2,1,0,0},{2,0,0,1},{0,1,0,1}, {0,0,0,1}} Output: {{-1}} Explanation: As no path exists so, -1. Your Task: You don't need to read or print anyhting, Your task is to complete the function ShortestDistance() which takes the matrix as input parameter and returns a matrix of size n if path exists otherwise returns a matrix of 1x1 which contains -1. In output matrix, 1 at (i, j) represents the cell is taken into the path otherwise 0 if any path exists. Expected Time Complexity: O(n*n*k) where k is max(matrix[i][j]) Expected Space Complexity: O(1) Constraints: 1 <= n <= 50 1 <= matrix[i][j] <= 20
class Solution: def isSafe(self, i, j, matrix): n = len(matrix) if 0 <= i < n and 0 <= j < n and matrix[i][j] != 0: return True return False def ShortestDistance(self, matrix): n = len(matrix) paths = [([0] * n) for i in range(n)] def recurse(i, j): if i == n - 1 and j == n - 1: paths[i][j] = 1 return True if self.isSafe(i, j, matrix): max_jump = matrix[i][j] paths[i][j] = 1 for jump in range(1, max_jump + 1): if recurse(i, j + jump): return True if recurse(i + jump, j): return True paths[i][j] = 0 return False recurse(0, 0) if sum([sum(r) for r in paths]) > 0: return paths else: return [[-1]]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR RETURN LIST LIST NUMBER