description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: ans = -1 dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] def rec(i, j, path=0): nonlocal ans, xd, yd if i < 0 or j < 0 or i >= n or j >= m: return if mat[i][j] == 0: return if i == xd and j == yd: ans = max(ans, path) return mat[i][j] = 0 for k in range(4): x = i + dx[k] y = j + dy[k] rec(x, y, path + 1) mat[i][j] = 1 rec(xs, ys) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN IF VAR VAR VAR NUMBER RETURN IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def __longestPathHelper(self, matrix, path, n, m, x, y, xd, yd): self.visited.add((x, y)) for x_offset, y_offset in self.dirs: temp_x, temp_y = x + x_offset, y + y_offset if ( 0 <= temp_x < n and 0 <= temp_y < m and (temp_x, temp_y) not in self.visited and matrix[temp_x][temp_y] != 0 ): if temp_x == xd and temp_y == yd: self.ans = max(self.ans, path + 1) else: self.__longestPathHelper( matrix, path + 1, n, m, temp_x, temp_y, xd, yd ) self.visited.discard((x, y)) def longestPath(self, matrix, n, m, xs, ys, xd, yd): if matrix[xs][ys] == 0: return -1 self.visited, self.ans = set(), -1 self.dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)] self.__longestPathHelper(matrix, 0, n, m, xs, ys, xd, yd) return self.ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): def solve(mat, n, m, xs, ys, xd, yd, count=0): if mat[xd][yd] == 0: return -1 if xs == xd and ys == yd: return count if xs < 0 or xs >= n or ys < 0 or ys >= m: return -1 if mat[xs][ys] in [0, 2]: return -1 count += 1 mat[xs][ys] = 2 res = max( solve(mat, n, m, xs + 1, ys, xd, yd, count), solve(mat, n, m, xs - 1, ys, xd, yd, count), solve(mat, n, m, xs, ys + 1, xd, yd, count), solve(mat, n, m, xs, ys - 1, xd, yd, count), ) mat[xs][ys] = 1 return res return solve(mat, n, m, xs, ys, xd, yd)
CLASS_DEF FUNC_DEF FUNC_DEF NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR LIST NUMBER NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): def function(i, j, visited, mat, sum1, sum2): if i < 0 or j < 0 or i >= len(mat) or j >= len(mat[0]) or mat[i][j] == 0: return if visited[i][j] == "T": return if i == xd and j == yd: sum2[0] = max(sum2[0], sum1) return visited[i][j] = "T" function(i + 1, j, visited, mat, sum1 + mat[i][j], sum2) function(i - 1, j, visited, mat, sum1 + mat[i][j], sum2) function(i, j + 1, visited, mat, sum1 + mat[i][j], sum2) function(i, j - 1, visited, mat, sum1 + mat[i][j], sum2) visited[i][j] = "F" return sum1 = 0 sum2 = [0] visited = [["F" for i in range(len(mat[0]))] for j in range(len(mat))] function(xs, ys, visited, mat, sum1, sum2) if sum2[0] == 0: return -1 return sum2[0]
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN IF VAR VAR VAR STRING RETURN IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING RETURN ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER RETURN NUMBER RETURN VAR NUMBER
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int): ans = -1 dir_x = [0, 0, 1, -1] dir_y = [1, -1, 0, 0] def isvalid(mat, n, m, xs, ys): if xs < 0 or ys < 0: return False if xs >= n or ys >= m: return False if mat[xs][ys] != 1: return False return True def solve(mat, n, m, xs, ys, xd, yd, dist): nonlocal ans if xs == xd and ys == yd: ans = max(ans, dist) return for i in range(4): dist += 1 mat[xs][ys] = -1 xs += dir_x[i] ys += dir_y[i] if isvalid(mat, n, m, xs, ys): solve(mat, n, m, xs, ys, xd, yd, dist) xs -= dir_x[i] ys -= dir_y[i] mat[xs][ys] = 1 dist -= 1 if mat[xs][ys] != 1: return -1 solve(mat, n, m, xs, ys, xd, yd, 0) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
def backtracking(xs, ys, n, m, xd, yd, arr): if arr[xs][ys] == 0 or arr[xd][yd] == 0: return -1 vis = set() vis.add((xs, ys)) res = [] helper(xs, ys, n, m, xd, yd, arr, vis, res) if res: return res[-1] - 1 return -1 def helper(i, j, r, c, xd, yd, arr, vis, res): if (i, j) == (xd, yd): if not res: res.append(len(vis)) else: res[-1] = max(res[-1], len(tuple(vis))) for ti, tj in zip([1, -1, 0, 0], [0, 0, 1, -1]): ni, nj = ti + i, tj + j if isValid(ni, nj, r, c, arr) and (ni, nj) not in vis: vis.add((ni, nj)) helper(ni, nj, r, c, xd, yd, arr, vis, res) vis.remove((ni, nj)) def isValid(i, j, r, c, arr): if i >= 0 and i < r and j >= 0 and j < c and arr[i][j]: return True return False class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: return backtracking(xs, ys, n, m, xd, yd, mat)
FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR RETURN BIN_OP VAR NUMBER NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: def check(mat, n, m, i, j, vis): if i >= 0 and j >= 0 and i < n and j < m and vis[i][j] == 0: return True return False def recur(mat, n, m, xs, ys, xd, yd, vis, rh, ch): if mat[xs][ys] == 0: return -1 if xs == xd and ys == yd: return 0 vis[xs][ys] = 1 res = -1 for i in range(4): x = xs + rh[i] y = ys + ch[i] if check(mat, n, m, x, y, vis): mxl = recur(mat, n, m, x, y, xd, yd, vis, rh, ch) if mxl != -1: res = max(res, mxl + 1) vis[xs][ys] = 0 return res vis = [[(0) for i in range(m)] for j in range(n)] rh = [-1, 0, 0, 1] ch = [0, -1, 1, 0] ans = recur(mat, n, m, xs, ys, xd, yd, vis, rh, ch) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): if mat[xs][ys] == 0: return -1 self.mat = mat self.n = n self.m = m self.ans = 0 self.xd = xd self.yd = yd self.move(xs, ys, 1, {(xs, ys): 1}) return self.ans - 1 def move(self, i, j, length, visited): if i == self.xd and j == self.yd: self.ans = max(self.ans, length) return for xdash, ydash in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]: if ( xdash >= 0 and xdash < self.n and ydash >= 0 and ydash < self.m and self.mat[xdash][ydash] == 1 and (xdash, ydash) not in visited ): visited[xdash, ydash] = 1 self.move(xdash, ydash, length + 1, visited) visited.pop((xdash, ydash)) return
CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER DICT VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: if mat[xs][ys] == 0 or mat[xd][yd] == 0: return -1 res = [0] def dfs(r, c, path): if r == xd and c == yd: res[0] = max(res[0], path) return directions = [[1, 0], [-1, 0], [0, -1], [0, 1]] visited[r][c] = 1 for dr, dc in directions: i, j = dr + r, dc + c if ( i >= 0 and i < n and j >= 0 and j < m and mat[i][j] == 1 and visited[i][j] == 0 ): dfs(i, j, path + 1) visited[r][c] = 0 visited = [[(0) for i in range(m)] for j in range(n)] dfs(xs, ys, 0) if res[0] == 0: return -1 return res[0]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN NUMBER RETURN VAR NUMBER VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: visited = [[(0) for i in range(m)] for i in range(n)] ds = "" ans = [-1] xMoves = [1, 0, 0, -1] yMoves = [0, 1, -1, 0] dir = "DLRU" def solve(i, j, ds): if i == xd and j == yd: ans[0] = max(ans[0], len(ds)) return for k in range(4): nI = i + xMoves[k] nJ = j + yMoves[k] if ( nI >= 0 and nJ >= 0 and nI < n and nJ < m and visited[nI][nJ] != 1 and mat[nI][nJ] == 1 ): visited[nI][nJ] = 1 solve(nI, nJ, ds + dir[k]) visited[nI][nJ] = 0 if mat[xs][ys] == 1: visited[xs][ys] = 1 solve(xs, ys, ds) return ans[0]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR STRING FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: ans = -1 def dfs(x, y, visited, dist): nonlocal ans if mat[x][y] == 0 or (x, y) in visited: return if (x, y) == (xd, yd): ans = max(dist, ans) return visited.add((x, y)) for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: nx = x + dx ny = y + dy if 0 <= nx < n and 0 <= ny < m: dfs(nx, ny, visited, dist + 1) visited.remove((x, y)) dfs(xs, ys, set(), 0) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER RETURN VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
def rec(mat, i, j, visited, xd, yd): if ( i < 0 or j < 0 or i >= len(mat) or j >= len(mat[0]) or visited[i][j] or not mat[i][j] ): return -1 if i == xd and j == yd: return 0 visited[i][j] = True x = [0, 0, 1, -1] y = [1, -1, 0, 0] ma = -1 for k in range(4): temp = rec(mat, i + x[k], j + y[k], visited, xd, yd) if temp != -1: ma = max(ma, temp + 1) visited[i][j] = False return ma class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): visited = [[(False) for i in range(m)] for j in range(n)] return rec(mat, xs, ys, visited, xd, yd)
FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: ans = -1 def dfs(x, y, visited, dist): nonlocal ans if mat[x][y] == 0 or (x, y) in visited: return if (x, y) == (xd, yd): ans = max(dist, ans) return visited.add((x, y)) for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: nx = x + dx ny = y + dy if 0 <= nx < n and 0 <= ny < m: dfs(nx, ny, visited, dist + 1) visited.remove((x, y)) dfs(xs, ys, set(), 0) return ans class IntArray: def __init__(self) -> None: pass def Input(self, n): arr = [int(i) for i in input().strip().split()] return arr def Print(self, arr): for i in arr: print(i, end=" ") print() class IntMatrix: def __init__(self) -> None: pass def Input(self, n, m): matrix = [] for _ in range(n): matrix.append([int(i) for i in input().strip().split()]) return matrix def Print(self, arr): for i in arr: for j in i: print(j, end=" ") print() if __name__ == "__main__": t = int(input()) for _ in range(t): a = IntArray().Input(2) b = IntArray().Input(4) mat = IntMatrix().Input(a[0], a[0]) obj = Solution() res = obj.longestPath(mat, a[0], a[1], b[0], b[1], b[2], b[3]) print(res)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER RETURN VAR VAR CLASS_DEF FUNC_DEF NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF NONE FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): if mat[xs][ys] == 0 or mat[xd][yd] == 0: return -1 path = [(1, 0), (-1, 0), (0, 1), (0, -1)] visit = [([0] * m) for i in range(n)] def f(i, j, count): nonlocal maxi if i == xd and j == yd: maxi = max(maxi, count) return for di, dj in path: ni, nj = i + di, j + dj if ( 0 <= ni < n and 0 <= nj < m and visit[ni][nj] == 0 and mat[ni][nj] == 1 ): visit[ni][nj] = 1 f(ni, nj, count + 1) visit[ni][nj] = 0 maxi = -1 visit[xs][ys] = 1 f(xs, ys, 0) return maxi
CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): if mat[xs][ys] == 0 or mat[xd][yd] == 0: return -1 def fun(i, j): if i == xd and j == yd: return 0 if i < 0 or i >= n or j < 0 or j >= m or mat[i][j] == 0: return float("-inf") mat[i][j] = 0 x = 1 + max(fun(i + 1, j), fun(i - 1, j), fun(i, j + 1), fun(i, j - 1)) mat[i][j] = 1 return x x = fun(xs, ys) if x > 0: return x return -1
CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): def haha(arr, i, j, p, q): if i == p and j == q: return 0 if i < 0 or j < 0 or i >= n or j >= m or arr[i][j] == 0: return float("-inf") ma = arr[i][j] arr[i][j] = 0 a = haha(arr, i + 1, j, p, q) b = haha(arr, i, j + 1, p, q) c = haha(arr, i - 1, j, p, q) d = haha(arr, i, j - 1, p, q) arr[i][j] = ma return 1 + max(a, b, c, d) if mat[xs][ys] == 0 or mat[xd][yd] == 0: return -1 x = haha(mat, xs, ys, xd, yd) if x == float("-inf"): return -1 return x class IntArray: def __init__(self) -> None: pass def Input(self, n): arr = [int(i) for i in input().strip().split()] return arr def Print(self, arr): for i in arr: print(i, end=" ") print() class IntMatrix: def __init__(self) -> None: pass def Input(self, n, m): matrix = [] for _ in range(n): matrix.append([int(i) for i in input().strip().split()]) return matrix def Print(self, arr): for i in arr: for j in i: print(j, end=" ") print() if __name__ == "__main__": t = int(input()) for _ in range(t): a = IntArray().Input(2) b = IntArray().Input(4) mat = IntMatrix().Input(a[0], a[0]) obj = Solution() res = obj.longestPath(mat, a[0], a[1], b[0], b[1], b[2], b[3]) print(res)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR CLASS_DEF FUNC_DEF NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF NONE FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath( self, matrix, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: if matrix[xs][ys] == 0 or matrix[xd][yd] == 0: return -1 visited = [[(False) for _ in range(len(matrix[0]))] for _ in range(len(matrix))] self.res = 0 r = len(matrix) c = len(matrix[0]) def dfs(i, j, ans): if ( i < 0 or j < 0 or i >= r or j >= c or matrix[i][j] == 0 or visited[i][j] == True ): return if i == xd and j == yd: self.res = max(self.res, ans) visited[i][j] = True dfs(i + 1, j, ans + 1) dfs(i, j + 1, ans + 1) dfs(i - 1, j, ans + 1) dfs(i, j - 1, ans + 1) visited[i][j] = False dfs(xs, ys, 0) return self.res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER 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 ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): ans = -1 if mat[xs][ys] == 0 or mat[xd][yd] == 0: return -1 def fun(i, j, path): nonlocal ans mat[i][j] = 2 if i == xd and j == yd: ans = max(ans, path) if i - 1 >= 0: if mat[i - 1][j] == 1: fun(i - 1, j, path + 1) if i + 1 < n: if mat[i + 1][j] == 1: fun(i + 1, j, path + 1) if j - 1 >= 0: if mat[i][j - 1] == 1: fun(i, j - 1, path + 1) if j + 1 < m: if mat[i][j + 1] == 1: fun(i, j + 1, path + 1) mat[i][j] = 1 fun(xs, ys, 0) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): def haha(arr, i, j, p, q): if i == p and j == q: return 0 if i < 0 or j < 0 or i >= n or j >= m or arr[i][j] == 0: return float("-inf") ma = arr[i][j] arr[i][j] = 0 a = haha(arr, i + 1, j, p, q) b = haha(arr, i, j + 1, p, q) c = haha(arr, i - 1, j, p, q) d = haha(arr, i, j - 1, p, q) arr[i][j] = ma return 1 + max(a, b, c, d) if mat[xs][ys] == 0 or mat[xd][yd] == 0: return -1 x = haha(mat, xs, ys, xd, yd) if x == float("-inf"): return -1 return x
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: paths = [] path = [] recStack = set() recStack.add((xs, ys)) def dfs(r, c): if mat[r][c] == 0: return if [r, c] == [xd, yd]: paths.append(path.copy()) return if r - 1 >= 0 and mat[r - 1][c] == 1 and (r - 1, c) not in recStack: path.append("U") recStack.add((r - 1, c)) dfs(r - 1, c) recStack.remove((r - 1, c)) path.pop() if r + 1 < n and mat[r + 1][c] == 1 and (r + 1, c) not in recStack: path.append("D") recStack.add((r + 1, c)) dfs(r + 1, c) recStack.remove((r + 1, c)) path.pop() if c - 1 >= 0 and mat[r][c - 1] == 1 and (r, c - 1) not in recStack: path.append("L") recStack.add((r, c - 1)) dfs(r, c - 1) recStack.remove((r, c - 1)) path.pop() if c + 1 < m and mat[r][c + 1] == 1 and (r, c + 1) not in recStack: path.append("R") recStack.add((r, c + 1)) dfs(r, c + 1) recStack.remove((r, c + 1)) path.pop() dfs(xs, ys) mx = -1 for p in paths: mx = max(mx, len(p)) return mx
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN IF LIST VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs: int, ys: int, xd: int, yd: int) -> int: if not mat: return mat if mat[xs][ys] == 0: return -1 visited = set() rows, cols = len(mat), len(mat[0]) ans = [] visited = set() def dfs(i, j, path, visited): if i == xd and j == yd: ans.append(path) return if ( (i + 1, j) not in visited and i + 1 >= 0 and i + 1 < rows and j >= 0 and j < cols and mat[i + 1][j] == 1 ): visited.add((i, j)) dfs(i + 1, j, path + "D", visited) visited.remove((i, j)) if ( (i - 1, j) not in visited and i - 1 >= 0 and i - 1 < rows and j >= 0 and j < cols and mat[i - 1][j] == 1 ): visited.add((i, j)) dfs(i - 1, j, path + "U", visited) visited.remove((i, j)) if ( (i, j - 1) not in visited and i >= 0 and i < rows and j - 1 >= 0 and j - 1 < cols and mat[i][j - 1] == 1 ): visited.add((i, j)) dfs(i, j - 1, path + "L", visited) visited.remove((i, j)) if ( (i, j + 1) not in visited and i >= 0 and i < rows and j + 1 >= 0 and j + 1 < cols and mat[i][j + 1] == 1 ): visited.add((i, j)) dfs(i, j + 1, path + "R", visited) visited.remove((i, j)) dfs(xs, ys, "", visited) if len(ans) == 0: return -1 maxi = len(ans[0]) for i in range(len(ans)): maxi = max(maxi, len(ans[i])) return maxi
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR RETURN VAR IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def utils( self, mat, n, m, src_row, src_col, dest_row, dest_col, max_path, cur_path ): if src_col == dest_col and src_row == dest_row: max_path[0] = max(max_path[0], path) return 0 if ( (src_col >= m or src_row >= n) or (src_row < 0 or src_col < 0) or mat[src_row][src_col] == 0 ): return 0 mat[src_row][src_col] = 0 self.utils( mat, n, m, src_row + 1, src_col, dest_row, dest_col, max_path, cur_path + 1 ) self.utils( mat, n, m, src_row - 1, src_col, dest_row, dest_col, max_path, cur_path + 1 ) self.utils( mat, n, m, src_row, src_col + 1, dest_row, dest_col, max_path, cur_path + 1 ) self.utils( mat, n, m, src_row, src_col - 1, dest_row, dest_col, max_path, cur_path + 1 ) mat[src_row][src_col] = 1 return 0 def longestPath(self, mat, n, m, xs, ys, xd, yd): ans = -1 def dfs(x, y, visited, dist): nonlocal ans if mat[x][y] == 0 or (x, y) in visited: return if (x, y) == (xd, yd): ans = max(dist, ans) return visited.add((x, y)) for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: nx = x + dx ny = y + dy if 0 <= nx < n and 0 <= ny < m: dfs(nx, ny, visited, dist + 1) visited.remove((x, y)) dfs(xs, ys, set(), 0) return ans
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER RETURN VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def is_safe(self, x, y, r, c, mat): global vis if x < 0 or x >= r or y < 0 or y >= c: return False return mat[x][y] == 1 and vis[x][y] == False def bt(self, cur_x, cur_y, xd, yd, r, c, dist, mat): global ans, vis if cur_x == xd and cur_y == yd: ans = max(ans, dist) return vis[cur_x][cur_y] = True x = [0, 0, -1, 1] y = [-1, 1, 0, 0] for i in range(len(x)): new_x = cur_x + x[i] new_y = cur_y + y[i] if self.is_safe(new_x, new_y, r, c, mat): self.bt(new_x, new_y, xd, yd, r, c, dist + 1, mat) vis[cur_x][cur_y] = False def longestPath(self, mat, r, c, xs, ys, xd, yd): if mat[xs][ys] == 0 or mat[xd][yd] == 0: return -1 global ans, vis ans = 0 vis = [[(False) for j in range(c)] for i in range(r)] self.bt(xs, ys, xd, yd, r, c, 0, mat) return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR RETURN VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): self.ans = -1 self.dir_delta = [[0, -1], [-1, 0], [0, 1], [1, 0]] R = n C = m def canGoto(r, c): nonlocal mat, R, C return 0 <= r < R and 0 <= c < C and mat[r][c] == 1 def backtrack(sr, sc, distance=0): nonlocal mat, xd, yd if canGoto(sr, sc): if sr == xd and sc == yd: self.ans = max(self.ans, distance) else: mat[sr][sc] = 2 for dr, dc in self.dir_delta: backtrack(sr + dr, sc + dc, distance + 1) mat[sr][sc] = 1 backtrack(xs, ys) return self.ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER FUNC_DEF NUMBER IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): if mat[xs][ys] == 0: return -1 vis = [[(False) for _ in range(m)] for _ in range(n)] mat[xs][ys] = -1 mx = [-1] self.fn(mat, n, m, xs, ys, xd, yd, 0, vis, mx) return max(mx) def fn(self, mat, n, m, xs, ys, xd, yd, path, vis, mx): if xs == xd and ys == yd: mx.append(path) return for i, j in [(0, 1), (1, 0), (0, -1), (-1, 0)]: if 0 <= xs + i < n and 0 <= ys + j < m and mat[xs + i][ys + j] == 1: mat[xs + i][ys + j] = -1 self.fn(mat, n, m, xs + i, ys + j, xd, yd, path + 1, vis, mx) mat[xs + i][ys + j] = 1
CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def isValid(self, mat, x, y, n, m): if x >= 0 and y >= 0 and x < n and y < m and not vis[x][y] and mat[x][y]: return True return False def solve(self, mat, n, m, x, y, ex, ey, cor, steps): global maxi if not self.isValid(mat, x, y, n, m): return if x == ex and y == ey: maxi = max(maxi, steps) return vis[x][y] = 1 for i in range(4): nx = x + cor[i][0] ny = y + cor[i][1] self.solve(mat, n, m, nx, ny, ex, ey, cor, steps + 1) vis[x][y] = 0 def longestPath(self, mat, n, m, sx, sy, ex, ey): if sx == ex and sy == ey: return 0 if not mat[sx][sy]: return -1 global vis vis = [[(0) for i in range(m)] for j in range(n)] global maxi maxi = float("-Inf") cor = [[1, 0], [-1, 0], [0, 1], [0, -1]] self.solve(mat, n, m, sx, sy, ex, ey, cor, 0) if maxi == float("-Inf"): return -1 return maxi
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: def solve(mat, n, m, xs, ys, xd, yd, temp): if ( xs < 0 or ys < 0 or xs == n or ys == m or mat[xs][ys] == 2 or mat[xs][ys] == 0 ): return nonlocal length if xs == xd and ys == yd: if temp > length: length = temp return mat[xs][ys] = 2 solve(mat, n, m, xs - 1, ys, xd, yd, temp + 1) solve(mat, n, m, xs + 1, ys, xd, yd, temp + 1) solve(mat, n, m, xs, ys + 1, xd, yd, temp + 1) solve(mat, n, m, xs, ys - 1, xd, yd, temp + 1) mat[xs][ys] = 1 length = -1 solve(mat, n, m, xs, ys, xd, yd, 0) return length
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def __init__(self): self.ans = -1 def longestPath(self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int): rows = len(mat) cols = len(mat[0]) v2 = [[(False) for _ in range(cols)] for _ in range(rows)] self.path(xs, ys, xd, yd, mat, v2, 0) return self.ans def path(self, i, j, end_i, end_j, mat, visited, steps): if ( i < 0 or j < 0 or i >= len(mat) or j >= len(mat[0]) or visited[i][j] is True or mat[i][j] == 0 ): return if i == end_i and j == end_j: self.ans = max(self.ans, steps) return visited[i][j] = True self.path(i + 1, j, end_i, end_j, mat, visited, steps + 1), self.path(i, j + 1, end_i, end_j, mat, visited, steps + 1) self.path(i - 1, j, end_i, end_j, mat, visited, steps + 1) self.path(i, j - 1, end_i, end_j, mat, visited, steps + 1) visited[i][j] = False return
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR VAR VAR VAR 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 EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): ans = [] visited = [[(0) for _ in range(m)] for _ in range(n)] def isSafe(x, y): if ( x >= 0 and y >= 0 and x < n and y < m and visited[x][y] != 1 and mat[x][y] == 1 ): return True return False def solve(x, y, path): if x == xd and y == yd: ans.append(path) return visited[x][y] = 1 if isSafe(x + 1, y): solve(x + 1, y, path + 1) if isSafe(x, y - 1): solve(x, y - 1, path + 1) if isSafe(x, y + 1): solve(x, y + 1, path + 1) if isSafe(x - 1, y): solve(x - 1, y, path + 1) visited[x][y] = 0 if mat[xs][ys] == 0: return -1 else: solve(xs, ys, 0) if ans: return max(ans) else: return -1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR RETURN FUNC_CALL VAR VAR RETURN NUMBER
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: return ( self.helper(mat, xs, ys, n - 1, m - 1, (xd, yd), 0) if mat[xs][ys] else -1 ) def helper(self, grid, x, y, n, m, end, count): if (x, y) == end: return count matrix = [row.copy() for row in grid] matrix[x][y] = 0 output = -1 if x > 0 and matrix[x - 1][y]: output = max(output, self.helper(matrix, x - 1, y, n, m, end, count + 1)) if y > 0 and matrix[x][y - 1]: output = max(output, self.helper(matrix, x, y - 1, n, m, end, count + 1)) if x < n and matrix[x + 1][y]: output = max(output, self.helper(matrix, x + 1, y, n, m, end, count + 1)) if y < m and matrix[x][y + 1]: output = max(output, self.helper(matrix, x, y + 1, n, m, end, count + 1)) return output
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): path_lengths = [] visited = set() pos = xs, ys dst = xd, yd length = 0 self.dfs(mat, n, m, pos, dst, visited, length, path_lengths) if len(path_lengths) == 0: return -1 return max(path_lengths) def dfs(self, mat, n, m, pos, dst, visited, length, path_lengths): if mat[pos[0]][pos[1]] == 0: return if pos in visited: return if pos == dst: path_lengths.append(length) return visited.add(pos) if pos[1] < m - 1: right_pos = pos[0], pos[1] + 1 self.dfs( mat, n, m, right_pos, dst, visited.copy(), length + 1, path_lengths ) if pos[0] < n - 1: down_pos = pos[0] + 1, pos[1] self.dfs(mat, n, m, down_pos, dst, visited.copy(), length + 1, path_lengths) if pos[1] > 0: left_pos = pos[0], pos[1] - 1 self.dfs(mat, n, m, left_pos, dst, visited.copy(), length + 1, path_lengths) if pos[0] > 0: up_pos = pos[0] - 1, pos[1] self.dfs(mat, n, m, up_pos, dst, visited.copy(), length + 1, path_lengths)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER VAR NUMBER NUMBER RETURN IF VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: ans = [-1] def f(x, y, xd, yd, mat, n, m, vis, ans, path): if x < 0 or x >= n or y < 0 or y >= m or vis[x][y] or mat[x][y] == 0: return if x == xd and y == yd: ans[0] = max(ans[0], path) return vis[x][y] = 1 f(x + 1, y, xd, yd, mat, n, m, vis, ans, path + 1) f(x - 1, y, xd, yd, mat, n, m, vis, ans, path + 1) f(x, y + 1, xd, yd, mat, n, m, vis, ans, path + 1) f(x, y - 1, xd, yd, mat, n, m, vis, ans, path + 1) vis[x][y] = 0 vis = [([0] * m) for i in range(n)] f(xs, ys, xd, yd, mat, n, m, vis, ans, 0) return ans[0]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR NUMBER VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): if mat[xs][ys] == 0 or mat[xd][yd] == 0: return -1 visited = [([-1] * len(mat[0])) for i in range(len(mat))] def traverse(i, j, xd, yd): if ( i < 0 or j < 0 or i >= len(mat) or j >= len(mat[0]) or mat[i][j] == 0 or visited[i][j] == 1 ): return -999999999 if i == xd and j == yd: return 0 visited[i][j] = 1 val = -999999999 val = max(val, 1 + traverse(i + 1, j, xd, yd)) val = max(val, 1 + traverse(i - 1, j, xd, yd)) val = max(val, 1 + traverse(i, j + 1, xd, yd)) val = max(val, 1 + traverse(i, j - 1, xd, yd)) visited[i][j] = -1 return val val = traverse(xs, ys, xd, yd) if val < 0: return -1 else: return val
CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER 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 FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
def util(mat, m, n, xs, ys, xd, yd, visit, path): if xs == xd and ys == yd: return path if xs < 0 or xs >= m or ys < 0 or ys >= n or mat[xs][ys] == 0 or visit[xs][ys]: return float("inf") visit[xs][ys] = True dx = [0, 0, 1, -1] dy = [1, -1, 0, 0] dist = 0 for i in range(4): x, y = xs + dx[i], ys + dy[i] t = util(mat, m, n, x, y, xd, yd, visit, path + 1) if t != float("inf") and t > dist: dist = t visit[xs][ys] = False return dist class Solution: def longestPath( self, mat, n: int, m: int, xs: int, ys: int, xd: int, yd: int ) -> int: if mat[xd][yd] == 0: return -1 m, n = len(mat), len(mat[0]) visit = [[(False) for c in range(n)] for r in range(m)] ans = util(mat, m, n, xs, ys, xd, yd, visit, 0) if ans == float("inf"): return -1 else: return ans
FUNC_DEF IF VAR VAR VAR VAR RETURN VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER 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 FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1. Example 1: Input: {xs,ys} = {0,0} {xd,yd} = {1,7} matrix = 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 Output: 24 Explanation: Example 2: Input: {xs,ys} = {0,3} {xd,yd} = {2,2} matrix = 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 Output: -1 Explanation: We can see that it is impossible to reach the cell (2,2) from (0,3). Your Task: You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path. Expected Time Complexity: O(2^(N*M)) Expected Auxiliary Space: O(N*M) Constraints: 1 <= N,M <= 10
class Solution: def longestPath(self, mat, n, m, xs, ys, xd, yd): n = len(mat) m = len(mat[0]) ans = -1 def dfs(i, j, visited, dist): nonlocal ans move = [[-1, 0], [1, 0], [0, -1], [0, 1]] if mat[i][j] == 0 or (i, j) in visited: return if (i, j) == (xd, yd): ans = max(ans, dist) return visited.add((i, j)) for x, y in move: newX = x + i newY = y + j if ( newX >= 0 and newX < n and newY >= 0 and newY < m and mat[newX][newY] == 1 ): dfs(newX, newY, visited, dist + 1) visited.remove((i, j)) return ans ans = dfs(xs, ys, set(), 0) if ans == None: return -1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER IF VAR NONE RETURN NUMBER RETURN VAR
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def l_find_bsearch(self, nums, target): l = 0 r = len(nums) - 1 while l <= r: mid = (l + r) // 2 if nums[mid] == target: r = mid - 1 if nums[mid] < target: l = mid + 1 if nums[mid] > target: r = mid - 1 return l def r_find_bsearch(self, nums, target): l = 0 r = len(nums) - 1 while l <= r: mid = (l + r) // 2 if nums[mid] == target: l = mid + 1 if nums[mid] < target: l = mid + 1 if nums[mid] > target: r = mid - 1 return r def count(self, nums, n, target): if nums == []: return 0 l = self.l_find_bsearch(nums, target) if l >= len(nums) or nums[l] != target: return 0 r = self.r_find_bsearch(nums, target) if r < 0 or nums[r] != target: return 0 return r - l + 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR LIST RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): count = 0 i = 0 while i < n: if arr[i] == x: count += 1 i += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count1(self, nums, n, x): count = 0 for num in nums: if num == x: count += 1 return count def count(self, nums, n, x): def binarySearch(left, right, searchDirection): result = -1 while left <= right: mid = (left + right) // 2 val = nums[mid] if val == x: result = mid if searchDirection == "f": right = mid - 1 else: left = mid + 1 elif val > x: right = mid - 1 else: left = mid + 1 return result floor = binarySearch(0, n - 1, "f") ceil = binarySearch(0, n - 1, "c") if floor != -1 and ceil != -1: return ceil - floor + 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): l = 0 r = n - 1 left = -1 while l <= r: mid = (l + r) // 2 if arr[mid] == x: left = mid r = mid - 1 elif arr[mid] > x: r = mid - 1 else: l = mid + 1 l = 0 r = n - 1 right = -1 while l <= r: mid = (l + r) // 2 if arr[mid] == x: right = mid l = mid + 1 elif arr[mid] > x: r = mid - 1 else: l = mid + 1 if right + left == -2: return 0 else: return right - left + 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def binary(self, arr, n, x): s = 0 e = len(arr) - 1 while s <= e: m = (s + e) // 2 if arr[m] == x: return m elif arr[m] < x: s = m + 1 else: e = m - 1 return -1 def count(self, arr, n, x): idx = self.binary(arr, n, x) if idx == -1: return 0 k = 1 left = idx - 1 while left >= 0 and arr[left] == x: k = k + 1 left = left - 1 right = idx + 1 while right < n and arr[right] == x: k += 1 right += 1 return k
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): def first(arr, n, x): low = 0 high = n - 1 s = -1 while low <= high: mid = low + (high - low) // 2 if arr[mid] < x: low = mid + 1 elif arr[mid] > x: high = mid - 1 else: s = mid high = mid - 1 return s def last(arr, n, x): low = 0 high = n - 1 e = -1 while low <= high: mid = low + (high - low) // 2 if arr[mid] < x: low = mid + 1 elif arr[mid] > x: high = mid - 1 else: e = mid low = mid + 1 return e f = first(arr, n, x) z = last(arr, n, x) if z == -1 and f == -1: return 0 return z - f + 1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): def search(last=False): res = -1 low, high = 0, n - 1 while low <= high: mid = (low + high) // 2 if arr[mid] == x: res = mid if last: low = mid + 1 else: high = mid - 1 elif arr[mid] > x: high = mid - 1 else: low = mid + 1 return res firstOccurence = search() if firstOccurence == -1: return 0 lastOccurence = search(True) return lastOccurence - firstOccurence + 1
CLASS_DEF FUNC_DEF FUNC_DEF NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): first = -1 last = -1 count = 0 for i in range(n): if arr[i] == x: first = i break if first == -1: return 0 for i in range(n - 1, -1, -1): if arr[i] == x: last = i break count = last - first + 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): n = len(arr) first = -1 last = -1 low = 0 high = n - 1 while low <= high: mid = (low + high) // 2 if (mid == 0 or x > arr[mid - 1]) and arr[mid] == x: first = mid break elif x > arr[mid]: low = mid + 1 else: high = mid - 1 low = first if first != -1 else 0 high = n - 1 while low <= high: mid = (low + high) // 2 if (mid == high or x < arr[mid + 1]) and arr[mid] == x: last = mid break elif x < arr[mid]: high = mid - 1 else: low = mid + 1 count = last - first + 1 if first != -1 else 0 return count
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): if n <= 0: return -1 elif x not in arr: return 0 else: c = 0 for i in arr: if i == x: c += 1 return c
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): start = 0 end = n - 1 while start <= end: if arr[start] != x: start = start + 1 if arr[end] != x: end = end - 1 if arr[start] == x and arr[end] == x: break if start <= end: return end - start + 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): if x not in arr: return 0 l = 0 r = len(arr) - 1 while arr[l] != x: l = l + 1 while arr[r] != x: r = r - 1 return r - l + 1
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): l = 0 r = n - 1 c = 0 last = -1 first = -1 while l <= r: mid = (l + r) // 2 if arr[mid] == x: first = mid l = mid + 1 elif arr[mid] < x: l = mid + 1 else: r = mid - 1 l = 0 r = n - 1 while l <= r: mid = (l + r) // 2 if arr[mid] == x: r = mid - 1 last = mid elif arr[mid] < x: l = mid + 1 else: r = mid - 1 if first == last and first != -1 and last != -1: return 1 elif first == -1 and last == -1: return 0 return first - last + 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): d = {} for i in arr: if i in d: d[i] += 1 else: d[i] = 1 c = 0 for i in d: if i == x: return d[i] break else: c += 1 if c != 0: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR RETURN VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): left = 0 right = len(arr) - 1 f = 1 w = 1 while left <= right: mid = (left + right) // 2 if arr[mid] == x: w = 0 k = mid while k >= 1 and arr[k] == arr[k - 1]: f += 1 k -= 1 while mid < len(arr) - 1 and arr[mid] == arr[mid + 1]: f += 1 mid += 1 break if arr[mid] == x: f += 1 break if arr[mid] < x: left = mid + 1 if arr[mid] > x: right = mid - 1 if w: return 0 return f
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR RETURN NUMBER RETURN VAR
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): i, j = 0, n - 1 while i <= j: m = (i + j) // 2 if arr[m] >= x: j = m - 1 else: i = m + 1 if i < 0 or i >= n or arr[i] != x: return 0 f = i i, j = 0, n - 1 while i <= j: m = (i + j) // 2 if arr[m] <= x: i = m + 1 else: j = m - 1 return j - f + 1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def bin_search(self, flag, l, r, nums, target): ans = -1 while l <= r: mid = (l + r) // 2 if nums[mid] < target: l = mid + 1 elif nums[mid] > target: r = mid - 1 else: ans = mid if flag: r = mid - 1 else: l = mid + 1 return ans def count(self, nums, n, target): l = 0 r = len(nums) - 1 left = self.bin_search(True, l, r, nums, target) right = self.bin_search(False, l, r, nums, target) if left == -1 and right == -1: return 0 return right - left + 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): s = 0 mid = -1 e = n - 1 while s <= e: mid = s + (e - s) // 2 if arr[mid] == x: break elif arr[mid] > x: e = mid - 1 else: s = mid + 1 if mid == -1: return 0 count = 0 i = mid while i >= 0 and arr[i] == x: count += 1 i -= 1 i = mid + 1 while i < n and arr[i] == x: count += 1 i += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): l, r = 0, n - 1 count = 0 while l <= r: mid = (l + r) // 2 if arr[mid] == x: count += 1 i, j = mid, mid while i - 1 >= 0 and arr[i - 1] == x: count += 1 i -= 1 while j + 1 < n and arr[j + 1] == x: count += 1 j += 1 return count elif arr[mid] > x: r = mid - 1 else: l = mid + 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): mydict = dict() for i in range(n): if arr[i] not in mydict: mydict[arr[i]] = 1 else: mydict[arr[i]] += 1 if x in mydict: return mydict[x] else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR RETURN NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): left = 0 right = n - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == x: result = [mid, mid] while ( result[0] >= 0 and arr[result[0]] == x and result[1] < n and arr[result[1]] == x ): result[0] -= 1 result[1] += 1 while result[0] >= 0 and arr[result[0]] == x: result[0] -= 1 while result[1] < n and arr[result[1]] == x: result[1] += 1 return result[1] - result[0] - 1 elif arr[mid] < x: left = mid + 1 else: right = mid - 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR LIST VAR VAR WHILE VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER WHILE VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): if n == 1: if x == arr[0]: return 1 l = 0 r = n - 1 while l < r: if arr[l] != x: l = l + 1 if arr[r] != x: r = r - 1 if arr[r] == x and arr[l] == x: return r - l + 1 break return 0
CLASS_DEF FUNC_DEF IF VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): res = [] for i in range(n): if arr[i] == x: res.append(i) if len(res) != 0: return len(res) else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN NUMBER
Given a sorted array Arr of size N and a number X, you need to find the number of occurrences of X in Arr. Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation: 4 is not present in the given array. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the array of integers arr, n, and x as parameters and returns an integer denoting the answer. If x is not present in the array (arr) then return 0. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{6} 1 ≤ X ≤ 10^{6}
class Solution: def count(self, arr, n, x): def rhs(arr, x): low = 0 high = len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] > x: high = mid - 1 else: low = mid + 1 return high def lhs(arr, x): low = 0 high = len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] >= x: high = mid - 1 else: low = mid + 1 return low a = rhs(arr, x) b = lhs(arr, x) if arr[a] != x: return 0 return a - b + 1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
for _ in range(int(input())): n = input() if len(n) >= 4: if n[-4:] == "1000": print("YES") else: print("NO") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
adj = [[0, 1], [2, 1], [3, 1], [4, 1], [0, 1]] T = int(input()) for _ in range(T): L = list(input()) M = [int(x) for x in L] initial = 0 for i in range(len(M)): a = int(M[i]) initial = adj[initial][a] if initial == 4: print("YES") else: print("NO")
ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
def Helper(index, S, adj, root): if index == len(S) and root == 4: return True if index >= len(S): return False ans = False for node in adj[root]: if node[1] == S[index]: ans = ans | Helper(index + 1, S, adj, node[0]) else: pass return ans def function(S, adj): if len(S) > 3 and S[len(S) - 4 :] == "1000": return "YES" return "NO" def createADJ(): adj = [[] for i in range(5)] adj[0] = [(0, "0"), (1, "1")] adj[1] = [(1, "1"), (2, "0")] adj[2] = [(1, "1"), (3, "0")] adj[3] = [(4, "0"), (1, "1")] adj[4] = [(0, "0"), (1, "1")] return adj def main(): T = int(input()) for _ in range(T): adj = createADJ() S = input() val = function(S, adj) print(val) main()
FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING RETURN STRING RETURN STRING FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER STRING NUMBER STRING ASSIGN VAR NUMBER LIST NUMBER STRING NUMBER STRING ASSIGN VAR NUMBER LIST NUMBER STRING NUMBER STRING ASSIGN VAR NUMBER LIST NUMBER STRING NUMBER STRING ASSIGN VAR NUMBER LIST NUMBER STRING NUMBER STRING RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
t = int(input()) for i in range(t): s = str(input()) n = len(s) if n < 4: print("NO") elif s[n - 4] != "0" and s[n - 3] == "0" and s[n - 2] == "0" and s[n - 1] == "0": print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
for _ in range(int(input())): map = [[0, 1], [2, 1], [3, 1], [4, 1], [0, 1]] path = input() curr = 0 for p in path: curr = map[curr][int(p)] if curr == 4: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
graph = [ {"0": 0, "1": 1}, {"1": 1, "0": 2}, {"0": 3, "1": 1}, {"1": 1, "0": 4}, {"1": 1, "0": 0}, ] for i in range(0, int(input())): S = input() N = len(S) currentIndex = 0 for i in range(0, N): currentIndex = graph[currentIndex][S[i]] if currentIndex == 4: print("YES") else: print("NO")
ASSIGN VAR LIST DICT STRING STRING NUMBER NUMBER DICT STRING STRING NUMBER NUMBER DICT STRING STRING NUMBER NUMBER DICT STRING STRING NUMBER NUMBER DICT STRING STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
t = int(input()) for _ in range(t): n = input() if len(n) < 4: print("NO") elif n[-1] == "0" and n[-2] == "0" and n[-3] == "0" and n[-4] == "1": print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
def state(country, roa): road = int(roa) if country == "red": if road == 0: return "red" else: return "blue" elif country == "blue": if road == 1: return "blue" else: return "yellow" elif country == "yellow": if road == 0: return "pink" else: return "blue" elif country == "pink": if road == 0: return "green" else: return "blue" elif country == "green": if road == 1: return "blue" else: return "red" for _ in range(int(input())): s = input() c = "red" for i in range(len(s)): c = state(c, s[i]) if c == "green": print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR NUMBER RETURN STRING RETURN STRING IF VAR STRING IF VAR NUMBER RETURN STRING RETURN STRING IF VAR STRING IF VAR NUMBER RETURN STRING RETURN STRING IF VAR STRING IF VAR NUMBER RETURN STRING RETURN STRING IF VAR STRING IF VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
from sys import stdin for _ in range(int(stdin.readline())): s = stdin.readline().strip() cond = True n = len(s) state = 1 for i in range(n): if s[i] == "0": if state != 1 or state != 5: state += 1 elif state == 5: state -= 4 elif state != 2: state = 2 if state == 5: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
def path(a): a = [int(i) for i in a] color = "red" for i in a: if color == "red": if i == 1: color = "blue" elif i == 0: color = "red" elif color == "blue": if i == 1: color = "blue" elif i == 0: color = "yellow" elif color == "yellow": if i == 1: color = "blue" elif i == 0: color = "pink" elif color == "pink": if i == 1: color = "blue" elif i == 0: color = "green" elif color == "green": if i == 1: color = "blue" elif i == 0: color = "red" return color == "green" for i in range(int(input())): if path(input()): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR STRING RETURN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
def next(c, n): if n == "1": return "B" elif c == "R": return "R" elif c == "B": return "Y" elif c == "Y": return "P" elif c == "P": return "G" else: return "R" for i in range(int(input())): string = input() c = "R" for i in range(len(string)): n = string[i] c = next(c, n) if c == "G": print("YES") else: print("NO")
FUNC_DEF IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
T = int(input()) l = [[0, 1], [2, 1], [3, 1], [4, 1], [0, 1]] for _ in range(T): s = input() a = 0 for i in s: x = int(i) a = l[a][x] if a == 4: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
st = dict() st["R"] = ["R", "B"] st["B"] = ["Y", "B"] st["Y"] = ["P", "B"] st["P"] = ["G", "B"] st["G"] = ["R", "B"] tn = int(input()) for _ in range(tn): si = input() cs = "R" for ch in si: cs = st[cs][int(ch)] if cs == "G": print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING LIST STRING STRING ASSIGN VAR STRING LIST STRING STRING ASSIGN VAR STRING LIST STRING STRING ASSIGN VAR STRING LIST STRING STRING ASSIGN VAR STRING LIST STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
t = int(input()) for _ in range(t): s = input() if len(s) >= 4: x = s[-4] + s[-3] + s[-2] + s[-1] if x == "1000": print("YES") else: print("NO") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
graph = [] for i in range(0, 5): l = [] for j in range(0, 2): l.append(0) graph.append(l) graph[0][0] = 0 graph[0][1] = 1 graph[1][0] = 2 graph[1][1] = 1 graph[2][0] = 3 graph[2][1] = 1 graph[3][0] = 4 graph[3][1] = 1 graph[4][0] = 0 graph[4][1] = 1 n = int(input()) while n: string = input() pos = 0 for i in string: pos = graph[pos][int(i)] if pos == 4: print("YES") else: print("NO") n -= 1
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
for _ in range(int(input())): s = input() currnode = "red" for i in s: if currnode == "red": if i == "1": currnode = "blue" elif currnode == "blue": if i == "0": currnode = "yellow" elif currnode == "yellow": if i == "0": currnode = "pink" else: currnode = "blue" elif currnode == "pink": if i == "0": currnode = "green" else: currnode = "blue" elif currnode == "green": if i == "0": currnode = "red" else: currnode = "blue" if currnode == "green": print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR IF VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
n = int(input()) for i in range(n): s = input() j = len(s) - 1 if j > 2: if s[j - 3] == "1" and s[j - 2] == "0" and s[j - 1] == "0" and s[j] == "0": print("YES") else: print("NO") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
def makeAdj(): dic = {} dic[0, 0] = 0 dic[0, 1] = 1 dic[1, 0] = 2 dic[1, 1] = 1 dic[2, 1] = 1 dic[2, 0] = 3 dic[3, 0] = 4 dic[3, 1] = 1 dic[4, 1] = 1 dic[4, 0] = 0 return dic adj = makeAdj() t = int(input()) for i in range(t): s = str(input()) node = 0 for j in s: node = adj[node, int(j)] if node == 4: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
t = int(input()) for i in range(t): string = input() place = "R" for j in range(len(string)): if place == "R": if string[j] == "1": place = "B" elif place == "B": if string[j] == "0": place = "Y" elif place == "Y": if string[j] == "1": place = "B" elif string[j] == "0": place = "P" elif place == "P": if string[j] == "1": place = "B" elif string[j] == "0": place = "G" elif place == "G": if string[j] == "1": place = "B" elif string[j] == "0": place = "R" if place == "G": print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR VAR STRING ASSIGN VAR STRING IF VAR STRING IF VAR VAR STRING ASSIGN VAR STRING IF VAR STRING IF VAR VAR STRING ASSIGN VAR STRING IF VAR VAR STRING ASSIGN VAR STRING IF VAR STRING IF VAR VAR STRING ASSIGN VAR STRING IF VAR VAR STRING ASSIGN VAR STRING IF VAR STRING IF VAR VAR STRING ASSIGN VAR STRING IF VAR VAR STRING ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
for _ in range(int(input())): a = input() d = { "q00": "q0", "q01": "q1", "q11": "q1", "q10": "q2", "q21": "q1", "q20": "q3", "q31": "q1", "q30": "q4", "q41": "q1", "q40": "q0", } g = "q0" for i in a: g += i g = d[g] if g == "q4": print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
d = { "red": {"0": "red", "1": "blue"}, "blue": {"0": "yellow", "1": "blue"}, "yellow": {"0": "violet", "1": "blue"}, "violet": {"0": "green", "1": "blue"}, "green": {"0": "red", "1": "blue"}, } for _ in range(int(input())): s = input() c = "red" f = 0 for i in s: c = d[c][i] if c == "green": print("YES") else: print("NO")
ASSIGN VAR DICT STRING STRING STRING STRING STRING DICT STRING STRING STRING STRING DICT STRING STRING STRING STRING DICT STRING STRING STRING STRING DICT STRING STRING STRING STRING DICT STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
for _ in range(int(input())): s = input() if len(s) < 4: print("NO") elif s[-4:] == "1000": print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
def main(): t = int(input()) for _ in range(t): sx = input() if sx[-4:] == "1000": print("YES") else: print("NO") main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
t = int(input()) for i in range(t): s = input() l = len(s) if l >= 4: if s.endswith("1000"): print("YES") else: print("NO") else: print("NO")
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 IF FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
def change_of_state(state, cur_no): if cur_no == 1: if state == "R": state = "B" elif state == "B": state = "B" elif state == "Y": state = "B" elif state == "P": state = "B" elif state == "G": state = "B" elif state == "R": state = "R" elif state == "B": state = "Y" elif state == "Y": state = "P" elif state == "P": state = "G" elif state == "G": state = "R" return state for _ in range(int(input())): s = str(input()) state = "R" for i in range(len(s)): state = change_of_state(state, int(s[i])) if state == "G": print("YES") else: print("NO")
FUNC_DEF IF VAR NUMBER IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
t = int(input()) while t > 0: t = t - 1 s = input() a = [int(i) for i in s] if len(a) < 4: print("NO") continue sample = ["r0r", "r1b", "b1b", "b0y", "y1b", "y0p", "p0g", "p1b", "g1b", "g0b"] curr_state = "r" next_state = "" k = 0 z = 0 for i in a: z += 1 tran_val = i for j in sample: if curr_state == j[0] and tran_val == int(j[1]): next_state = j[2] break curr_state = next_state if curr_state == "g" and z == len(a): print("YES") k = 1 break if k == 0: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR STRING VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
path = { "R0": "R", "R1": "B", "B1": "B", "B0": "Y", "Y1": "B", "Y0": "P", "P1": "B", "P0": "G", "G1": "B", "G0": "R", } T = int(input()) for _ in range(T): s = input() current = "R" for c in s: current = path[current + c] print("YES" if current == "G" else "NO")
ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
T = int(input()) R = "R" G = "G" B = "B" Y = "Y" P = "P" Z = "0" O = "1" A = {} A[R] = {} A[R][Z] = R A[R][O] = B A[B] = {} A[B][Z] = Y A[B][O] = B A[Y] = {} A[Y][Z] = P A[Y][O] = B A[P] = {} A[P][Z] = G A[P][O] = B A[G] = {} A[G][Z] = R A[G][O] = B for t in range(T): s = input() c = R for i in s: c = A[c][i] if c == G: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR DICT ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There are $5$ cities in the country. The map of the country is given below. The tour starts from the red city. Each road is associated with a character. Initially, there is an empty string. Every time a road has been travelled the character associated gets appended to the string. At the green city either the string can be printed or the tour can be continued. In the problem, you are given a string tell whether it is possible to print the string while following the rules of the country? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, a string $ s $. The string consists only of $0's$ and $1's$. -----Output:----- For each testcase, output "YES" or "NO" depending on the input. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq length of each string \leq 10000$ - $ 1 \leq Summation length \leq 10^5$ -----Sample Input:----- 1 100 -----Sample Output:----- NO -----EXPLANATION:-----
t = int(input()) i = 0 while i < t: s = input() y = s[-4:] if y == "1000": print("YES") else: print("NO") i = i + 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP 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(10**6) class Solution: def largestSumCycle(self, N, Edge): def dfs(v, path={}, sums=0): nonlocal ans, visited, Edge if v in visited or Edge[v] == -1: return if v in path: ans = max(ans, sums - path[v]) return path[v] = sums dfs(Edge[v], path, sums + v) visited.add(v) visited = set() ans = -1 for v in range(N): dfs(v) return ans
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF FUNC_DEF DICT NUMBER IF VAR VAR VAR VAR NUMBER RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR 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(10**7) class Solution: def largestSumCycle(self, N, Edge): def getsum(node): visited = [(False) for i in range(N)] s = node visited[node] = True temp = Edge[node] while visited[temp] != True: s += temp visited[temp] = True temp = Edge[temp] return s def detect(node, found): if found[0] != -2: return vis[node] = True path[node] = True nextnode = Edge[node] if nextnode != -1: if vis[nextnode] == False: detect(nextnode, found) elif path[nextnode] == True and vis[nextnode] == True: found[0] = nextnode path[node] = False vis = [(False) for i in range(0, N)] path = [(False) for i in range(0, N)] ans = -1 found = [-2] for node in range(0, N): if vis[node] == False: detect(node, found) if found[0] != -2: s = getsum(found[0]) ans = max(ans, s) found[0] = -2 return ans
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER NUMBER RETURN 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 IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER 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): vis = [-1] * N dp_arr = [0] * N res = -1 for i in range(N): if vis[i] == -1: temp, val, stack = i, 0, [] while True: vis[temp] = 0 dp_arr[temp] = val stack.append(temp) if edge[temp] != -1: if vis[edge[temp]] == -1: val += temp temp = edge[temp] elif vis[edge[temp]] == 0: res = max(temp + val - dp_arr[edge[temp]], res) break else: break else: break while stack: vis[stack.pop()] = 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER LIST WHILE NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL 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
class Solution: def largestSumCycle(self, N, Edge): visited = [False] * N max_cycle_sum = -1 path = dict() path_keys = [] for u in range(N): if visited[u]: continue while -1 < u < N: if u in path: start = path[u] cycle_sum = sum(path_keys[start:]) max_cycle_sum = max(max_cycle_sum, cycle_sum) break if visited[u]: break visited[u] = True path[u] = len(path_keys) path_keys.append(u) u = Edge[u] path.clear() return max_cycle_sum
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL 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, Edge, i, vis, pathV, ans): vis[i] = True pathV[i] = True if Edge[i] != -1: next = Edge[i] if not vis[next]: self.dfs(Edge, next, vis, pathV, ans) elif pathV[next]: res = next node = Edge[next] while node != next: res += node node = Edge[node] ans[0] = max(ans[0], res) pathV[i] = False def largestSumCycle(self, N, Edge): vis = [False] * N pathV = [False] * N ans = [-1] for i in range(N): if not vis[i]: self.dfs(Edge, i, vis, pathV, ans) return ans[0]
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR 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(10**6) class Solution: def largestSumCycle(self, N, Edge): visited = [0] * N sc = -1 for i in range(N): sc = max(sc, self.nodes(Edge, visited, i)) return sc def nodes(self, Edge, visited, i): if visited[i] == 2: return -1 elif visited[i] == 1: sc = i current = i while Edge[current] != i: current = Edge[current] sc = sc + current return sc elif Edge[i] != -1: visited[i] = 1 sc = self.nodes(Edge, visited, Edge[i]) visited[i] = 2 return sc else: visited[i] = 2 return -1
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF 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 FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP 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
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 if __name__ == "__main__": for _ in range(int(input())): N = int(input()) Edge = [int(i) for i in input().split()] print(Solution().largestSumCycle(N, Edge))
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 IF VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR 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**8) class Solution: def dfs(self, graph, s, cost, localV, globalV): if s in globalV: return -1 if s in localV: return cost - localV[s] localV[s] = cost res = -1 if graph[s] != -1: d = graph[s] res = max(res, self.dfs(graph, d, cost + d, localV, globalV)) return res def largestSumCycle(self, N, Edge): res = -1 globalV = set() for s in range(N): localV = {} cost = self.dfs(Edge, s, s, localV, globalV) res = max(res, cost) globalV.update(set(localV)) return res
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL 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(10**6) class Solution: def largestSumCycle(self, N, Edge): PointScore = [-1] * len(Edge) points = set([i for i in range(N)]) maxscore = [-1] def Walker(point, score): if point in points: points.remove(point) if PointScore[point] == -1: PointScore[point] = score newPoint = Edge[point] if newPoint != -1: Walker(newPoint, score + newPoint) PointScore[point] = -2 elif PointScore[point] != -2: maxscore[0] = max(maxscore[0], score - PointScore[point]) while len(points): Walker(points.pop(), 0) return maxscore[0]
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR WHILE FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL 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): OFFSET = 1 sums = [-1] * N result = -1 for i0 in range(N): if sums[i0] >= 0: continue sums[i0] = OFFSET csum = OFFSET + i0 i = Edge[i0] i1 = i0 while i >= 0 and sums[i] < 0: i1 = i sums[i] = csum csum += i i = Edge[i] if i >= 0 and sums[i] >= OFFSET: result = max(result, csum - sums[i]) else: pass i = i0 while sums[i] > 0: sums[i] = 0 i = Edge[i] return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN 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 = [-1] * N res = -1 for i in range(N): j = i mn = {} sm = 0 while visited[j] == -1: mn[j] = sm sm += j visited[j] = 1 j = Edge[j] if j == -1: break if j in mn: res = max(res, sm - mn[j]) break return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR VAR NUMBER 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 largestSumCycle(self, N, Edge): adjlist = [[] for i in range(N)] for i in range(len(Edge)): if Edge[i] != -1: adjlist[i].append(Edge[i]) visitedarr = [(False) for i in range(N)] ans = 0 for i in range(N): if visitedarr[i] == False: ancestors = set() stack = [] tempans = self.Helper(adjlist, i, visitedarr, ancestors, stack) ans = max(ans, tempans) if ans: return ans return -1 def Helper(self, adjlist, currentnode, visitedarr, ancestors, stack): t = 0 for x in adjlist[currentnode]: if visitedarr[x] == True and x in ancestors: te = currentnode for idx in range(-1, -len(stack) - 1, -1): if stack[idx] == x: te += x break te += stack[idx] return te elif visitedarr[x] == False: ancestors.add(currentnode) visitedarr[currentnode] = True stack.append(currentnode) temp = self.Helper(adjlist, x, visitedarr, ancestors, stack) t = max(t, temp) stack.pop() ancestors.remove(currentnode) return t
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR