description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
def dynamic(n, m, grid): if grid[0][0] == 0: return 0 dp = [[(0) for _ in range(m)] for _ in range(n)] for i in range(n): if grid[i][0] == 0: ti = i while ti < n: grid[ti][0] = 0 ti += 1 break else: dp[i][0] = 1 for j in range(m): if grid[0][j] == 0: tj = j while tj < m: grid[0][tj] = 0 tj += 1 break else: dp[0][j] = 1 for i in range(1, n): for j in range(1, m): if grid[i][j]: dp[i][j] = dp[i - 1][j] + dp[i][j - 1] return dp[n - 1][m - 1] class Solution: def uniquePaths(self, n, m, grid): return dynamic(n, m, grid) % (10**9 + 7)
FUNC_DEF IF VAR NUMBER NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def uniquePaths(self, n, m, grid): if grid[0][0] == 0 or grid[n - 1][m - 1] == 0: return 0 path = [([0] * m) for _ in range(n)] path[0][0] = 1 for r in range(n): for c in range(m): if grid[r][c] == 0: continue if r - 1 >= 0: path[r][c] += path[r - 1][c] if c - 1 >= 0: path[r][c] += path[r][c - 1] return path[n - 1][m - 1] % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def uniquePaths(self, n, m, grid): dp = [[(0) for _ in range(m)] for _ in range(n)] if grid[0][0] == 0: return 0 else: for row in range(n): for col in range(m): if row == 0 and col == 0: dp[row][col] = 1 elif row == 0 and grid[row][col] != 0: dp[row][col] = dp[row][col - 1] elif col == 0 and grid[row][col] != 0: dp[row][col] = dp[row - 1][col] elif grid[row][col] != 0: dp[row][col] = dp[row - 1][col] + dp[row][col - 1] return dp[n - 1][m - 1] % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def uniquePaths(self, n, m, grid): if grid[0][0] == 0 or grid[n - 1][m - 1] == 0: return 0 ans = [[(0) for _ in range(m)] for _ in range(n)] ans[0][0] = 1 for r in range(n): for c in range(m): if not (r == 0 and c == 0): if c == 0: left = 0 else: left = ans[r][c - 1] if r == 0: up = 0 else: up = ans[r - 1][c] if grid[r][c] == 0: ans[r][c] = 0 else: ans[r][c] = (left + up) % (10**9 + 7) return ans[n - 1][m - 1]
CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def uniquePaths(self, n, m, grid): mod = 10**9 + 7 if grid[0][0] == 0 or grid[n - 1][m - 1] == 0: return 0 for i in range(n): for j in range(m): if grid[i][j]: if i > 0 and j > 0: grid[i][j] = (grid[i - 1][j] + grid[i][j - 1]) % mod if j == 0 and i > 0: grid[i][j] = grid[i - 1][j] if i == 0 and j > 0: grid[i][j] = grid[i][j - 1] return grid[n - 1][m - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
mod = 1000000007 class Solution: def uniquePaths(self, n, m, grid): memo = [] for i in range(n): memo.append([]) for j in range(m): memo[i].append(0) for i in range(n): if i == 0 and grid[i][0] == 1: memo[i][0] = 1 elif grid[i][0] == 0: memo[i][0] = 0 else: memo[i][0] = memo[i - 1][0] % mod for j in range(m): if j == 0 and grid[0][j] == 1: memo[0][j] = 1 elif grid[0][j] == 0: memo[0][j] = 0 else: memo[0][j] = memo[0][j - 1] % mod for i in range(1, n): for j in range(1, m): if grid[i][j] == 0: memo[i][j] = 0 else: memo[i][j] = (memo[i - 1][j] + memo[i][j - 1]) % mod return memo[n - 1][m - 1]
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def uniquePaths(self, n, m, grid): prev = [0] * m curr = [0] * m mod = 10**9 + 7 for i in range(n): for j in range(m): if i == 0 and j == 0 and grid[i][j] == 1: curr[j] = 1 else: up = 0 left = 0 if i > 0 and grid[i][j] == 1: up = prev[j] if j > 0 and grid[i][j] == 1: left = curr[j - 1] curr[j] = (up + left) % mod prev = curr return prev[m - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP VAR NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def uniquePaths(self, m, n, grid): def dfs(i, j, m, n, grid, dp): if grid[i][j] != 0: dp[0][0] = 1 for i in range(m): for j in range(n): if i == 0 and j == 0: continue elif i - 1 >= 0 and j - 1 >= 0 and grid[i][j] != 0: dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % (10**9 + 7) elif j - 1 >= 0 and grid[i][j] != 0: dp[i][j] = dp[i][j - 1] % (10**9 + 7) elif i - 1 >= 0 and grid[i][j] != 0: dp[i][j] = dp[i - 1][j] % (10**9 + 7) return dp[-1][-1] % (10**9 + 7) dp = [] if grid[m - 1][n - 1] == 0: return 0 for i in range(m): dp.append([0] * n) return dfs(0, 0, m, n, grid, dp) % (10**9 + 7)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def fun(self, i, j, grid, dp): if i == 0 and j == 0: return 1 elif i < 0 or j < 0: return 0 elif i >= 0 and j >= 0 and grid[i][j] == 0: return 0 if dp[i][j] != -1: return dp[i][j] up = self.fun(i - 1, j, grid, dp) left = self.fun(i, j - 1, grid, dp) dp[i][j] = (up + left) % (10**9 + 7) return dp[i][j] def uniquePaths(self, n, m, grid): if grid[0][0] == 0 or grid[n - 1][m - 1] == 0: return 0 dp = [[(-1) for i in range(m)] for j in range(n)] for i in range(n): for j in range(m): if i == 0 and j == 0: dp[i][j] = 1 elif grid[i][j] == 0: dp[i][j] = 0 else: up, left = 0, 0 if i > 0: up = dp[i - 1][j] if j > 0: left = dp[i][j - 1] dp[i][j] = (up + left) % (10**9 + 7) return dp[n - 1][m - 1]
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR VAR FUNC_DEF IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
import sys sys.setrecursionlimit(10**6) class Solution: def uniquePaths(self, n, m, g): mod = 10**9 + 7 def f(i, j): if g[0][0] == 0: return 0 if i == 0 and j == 0: return 1 if i < 0 or j < 0 or g[i][j] == 0: return 0 if dp[i][j] != -1: return dp[i][j] l = f(i - 1, j) r = f(i, j - 1) dp[i][j] = l + r % mod return dp[i][j] dp = [([-1] * m) for i in range(n)] return f(n - 1, m - 1) % mod
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER NUMBER NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
from sys import setrecursionlimit setrecursionlimit(1000000) class Solution: def getPossibleWays(self, n, m, dp, grid): mod = 10**9 + 7 if n == 0 and m == 0: return 1 if n < 0 or m < 0: return 0 if grid[n][m] == 0: return 0 if dp[n][m] != -1: return dp[n][m] up = self.getPossibleWays(n - 1, m, dp, grid) left = self.getPossibleWays(n, m - 1, dp, grid) dp[n][m] = (up + left) % mod return dp[n][m] def uniquePaths(self, n, m, grid): mod = 10**9 + 7 if grid[0][0] == 0 or grid[n - 1][m - 1] == 0: return 0 dp = [[(-1) for i in range(m)] for j in range(n)] return self.getPossibleWays(n - 1, m - 1, dp, grid)
EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def uniquePaths(self, n, m, grid): if grid[0][0] == 0 or grid[n - 1][m - 1] == 0: return 0 vss = [0] * m vss[0] = 1 for r in range(n): vss[0] = 0 if grid[r][0] == 0 else vss[0] for c in range(1, m): if grid[r][c]: vss[c] += vss[c - 1] else: vss[c] = 0 return vss[m - 1] % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def uniquePaths(self, n, m, grid): if grid[0][0] == 0 or grid[n - 1][m - 1] == 0: return 0 if n < m: return self.uniquePaths(m, n, list(zip(*grid))) dp = [(0) for _ in range(m)] dp[0] = 1 mod = 1000000000 + 7 for i in range(n): for j in range(m): if grid[i][j] == 0: dp[j] = 0 elif j > 0: dp[j] += dp[j - 1] dp[j] %= mod return dp[m - 1]
CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
mod = 1000000007 class Solution: def uniquePaths(self, n, m, grid): path = [[(0) for x in range(m)] for y in range(n)] queue = [(0, 0)] if grid[0][0] == 1: path[0][0] = 1 for i in range(1, n): if grid[i][0] == 1: path[i][0] = path[i - 1][0] for i in range(1, m): if grid[0][i] == 1: path[0][i] = path[0][i - 1] for i in range(1, n): for j in range(1, m): if grid[i][j] == 1: path[i][j] = (path[i - 1][j] + path[i][j - 1]) % mod return int(path[n - 1][m - 1] % mod)
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
import sys class Solution: def uniquePaths(self, n, m, grid): def fun(i, j, dp): if i >= 0 and j >= 0 and grid[i][j] == 0: return 0 if i == 0 and j == 0: return 1 if i < 0 or j < 0: return 0 if dp[i][j] != -1: return dp[i][j] up = fun(i - 1, j, dp) left = fun(i, j - 1, dp) dp[i][j] = (up + left) % (10**9 + 7) return dp[i][j] import sys sys.setrecursionlimit(10**9) dp = [[(-1) for i in range(m)] for j in range(n)] ans = fun(n - 1, m - 1, dp) return ans
IMPORT CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR VAR IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
import sys class Solution: def uniquePaths(self, n, m, grid): import sys sys.setrecursionlimit(10**9) dp = [[(0) for j in range(m)] for i in range(n)] if grid[0][0] == 0 or grid[n - 1][m - 1] == 0: return 0 dp[0][0] = 1 for i in range(0, n): for j in range(0, m): if i == 0 and j == 0: continue if grid[i][j] == 0: dp[i][j] = 0 else: left = 0 up = 0 if j > 0: left = dp[i][j - 1] if i > 0: up = dp[i - 1][j] dp[i][j] = (left + up) % (10**9 + 7) return dp[n - 1][m - 1] def solve(self, i, j, grid, dp): if i == 0 and j == 0: dp[i][j] = 1 return 1 if i < 0 or j < 0: return 0 if grid[i][j] == 0: dp[i][j] = 0 return 0 if dp[i][j] != -1: return dp[i][j] left = self.solve(i, j - 1, grid, dp) right = self.solve(i - 1, j, grid, dp) dp[i][j] = (left + right) % (10**9 + 7) return (left + right) % (10**9 + 7)
IMPORT CLASS_DEF FUNC_DEF IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
import sys sys.setrecursionlimit(100000000) class Solution: def uniquePaths(self, n, m, a): if a[0][0] == 0 or a[n - 1][m - 1] == 0: return 0 t = [[(-1) for i in range(m)] for j in range(n)] return self.solve(0, 0, n, m, a, t) % (1000000000 + 7) def solve(self, i, j, n, m, a, t): if i == n - 1 and j == m - 1: return 1 if i >= n or j >= m: return 0 if t[i][j] != -1: return t[i][j] count = 0 mod = 1000000000 + 7 if i + 1 < n and a[i + 1][j] == 1: count += self.solve(i + 1, j, n, m, a, t) % mod if j + 1 < m and a[i][j + 1] == 1: count += self.solve(i, j + 1, n, m, a, t) % mod t[i][j] = count % mod return t[i][j]
IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def uniquePaths(self, n, m, grid): if not grid[0][0]: return 0 result = [([0] * m) for _ in range(n)] for row in range(n): result[row][0] = grid[row][0] if not grid[row][0]: break for col in range(m): result[0][col] = grid[0][col] if not grid[0][col]: break for row in range(1, n): for col in range(1, m): if grid[row][col]: result[row][col] += result[row - 1][col] + result[row][col - 1] return result[-1][-1] % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def uniquePaths(self, n, m, grid): mod = 1000000007 paths = [([0] * m) for i in range(n)] if grid[0][0] == 1: paths[0][0] = 1 for i in range(1, n): if grid[i][0] == 1: paths[i][0] = paths[i - 1][0] for j in range(1, m): if grid[0][j] == 1: paths[0][j] = paths[0][j - 1] for i in range(1, n): for j in range(1, m): if grid[i][j] == 1: paths[i][j] = (paths[i - 1][j] + paths[i][j - 1]) % mod return paths[-1][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: def uniquePaths(self, n, m, d): x = 10**9 + 7 for i in range(1, n): if d[i][0] == 1: d[i][0] = d[i - 1][0] for i in range(1, m): if d[0][i] == 1: d[0][i] = d[0][i - 1] for i in range(1, n): for j in range(1, m): if d[i][j]: if d[i][j - 1] > 0 or d[i - 1][j] > 0: d[i][j] = (d[i - 1][j] + d[i][j - 1]) % x else: d[i][j] = 0 return d[n - 1][m - 1] % x
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed. You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell. Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7). Note: The first (1, 1) and last cell (n, m) of the grid can also be 0 Example 1: Input: n = 3, m = 3 grid[][] = {{1, 1, 1}; {1, 0, 1}; {1, 1, 1}} Output: 2 Explanation: 1 1 1 1 0 1 1 1 1 This is one possible path. 1 1 1 1 0 1 1 1 1 This is another possible path. Example 2: Input: n = 1, m = 3 grid = {{1, 0, 1}} Output : 0 Explanation: There is no possible path to reach the end. Your Task: You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7) Expected Time Complexity: O(n*m) Expected Auxiliary Space: O(n*m) Constraints: 1 ≤ n*m ≤ 10^{6}
class Solution: mod = 10**9 + 7 def uniquePaths(self, n, m, grid): dp = [([0] * m) for _ in range(n)] for j in range(m): if grid[0][j] == 0: break dp[0][j] = 1 for i in range(n): if grid[i][0] == 0: break dp[i][0] = 1 for i in range(1, n): for j in range(1, m): if grid[i][j] == 1: dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % self.mod return dp[n - 1][m - 1] % self.mod
CLASS_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
class Solution: def minInsAndDel(self, nums, nums2, M, N): dp = [-1] * M dp[-1] = 1 def LIS(nums): tails = [0] * len(nums) size = 0 for x in nums: i, j = 0, size while i != j: m = (i + j) // 2 if tails[m] < x: i = m + 1 else: j = m tails[i] = x size = max(i + 1, size) return size s = [] temp = set(nums2) for i in nums: if i in temp: s.append(i) res = LIS(s) return M + N - 2 * res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR 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 ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
class Solution: def minInsAndDel(self, x, y, n, m): def binse(e): i = 0 j = len(z) - 1 ans = j + 1 while i <= j: m = (i + j) // 2 if z[m] >= e: ans = m j = m - 1 else: i = m + 1 return ans se = set(y) q = [] for i in range(n): if x[i] in se: q.append(x[i]) x = q z = [] nn = n n = len(x) for i in range(n): if not z or z[-1] < x[i]: z.append(x[i]) else: z[binse(x[i])] = x[i] return m - len(z) + nn - len(z)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR 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 VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
class Solution: def ceil(self, arr, x): left = 0 right = len(arr) - 1 ans = -1 while left <= right: mid = (left + right) // 2 if arr[mid] >= x: ans = mid right = mid - 1 else: left = mid + 1 return ans def minInsAndDel(self, A, B, N, M): has = set(B) arr = [] for i in A: if i in has: arr.append(i) res = [] for i in range(len(arr)): if not res: res.append(arr[i]) elif res[-1] < arr[i]: res.append(arr[i]) elif res[0] > arr[i]: res[0] = arr[i] else: res[self.ceil(res, arr[i])] = arr[i] return N - len(res) + M - len(res) if __name__ == "__main__": t = int(input()) for _ in range(t): N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) ob = Solution() print(ob.minInsAndDel(A, B, N, M))
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR 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 ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
class Solution: def minInsAndDel(self, A, B, N, M): a = [] b = set(B) steps = 0 for x in A: if x in b: a.append(x) else: steps += 1 lis = [] for x in a: if not lis or lis[-1] < x: lis.append(x) else: idx = self.binary_search(lis, x) lis[idx] = x return steps + len(a) - len(lis) + M - len(lis) def binary_search(self, arr, x): lo = 0 hi = len(arr) - 1 while lo <= hi: mid = lo + (hi - lo) // 2 if arr[mid] == x: return mid elif arr[mid] > x: hi = mid - 1 else: lo = mid + 1 return lo
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP 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 VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
class Solution: def lcs(self, nums): def binary(temp, l, r, target): while r >= l: mid = (l + r) // 2 if temp[mid] == target: return mid elif temp[mid] > target: r = mid - 1 else: l = mid + 1 return l temp = [nums[0]] for i in range(1, len(nums)): if nums[i] > temp[-1]: temp.append(nums[i]) else: indx = binary(temp, 0, len(temp) - 1, nums[i]) temp[indx] = nums[i] return len(temp) def minInsAndDel(self, a, b, N, M): res = {} for i in range(M): res[b[i]] = i ans = [] count = 0 for j in range(N): if a[j] in res: ans.append(a[j]) else: count += 1 if len(ans) == 0: return count + M indx = self.lcs(ans) return count + (len(ans) - indx) + (M - indx)
CLASS_DEF FUNC_DEF FUNC_DEF 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 VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
class Solution: def minInsAndDel(self, A, B, N, M): b_set = set() for b in B: b_set.add(b) a_updated = [] steps = 0 for i in range(N): if A[i] in b_set: a_updated.append(A[i]) else: steps += 1 lis_size = self.LIS(a_updated) return steps + (len(a_updated) - lis_size) + (M - lis_size) def LIS(self, nums): def binarySearch(sub, val): lo, hi = 0, len(sub) - 1 while lo <= hi: mid = lo + (hi - lo) // 2 if sub[mid] < val: lo = mid + 1 elif val < sub[mid]: hi = mid - 1 else: return mid return lo sub = [] for val in nums: pos = binarySearch(sub, val) if pos == len(sub): sub.append(val) else: sub[pos] = val return len(sub)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR 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 RETURN VAR RETURN VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
class Solution: def minInsAndDel(self, A, B, N, M): def lower_bound(arr, n, k): l = 0 h = n - 1 while l < h: mid = (l + h) // 2 if k <= arr[mid]: h = mid else: l = mid + 1 if l < n and arr[l] < k: l += 1 return l def LIS(arr, n): if n <= 1: return n ans = [arr[0]] ans_len = 1 for i in range(1, n): if arr[i] > ans[ans_len - 1]: ans.append(arr[i]) ans_len += 1 else: index = lower_bound(ans, ans_len, arr[i]) ans[index] = arr[i] return ans_len s = set() arr = [] for i in range(M): s.add(B[i]) for i in range(N): if A[i] in s: arr.append(A[i]) lis = LIS(arr, len(arr)) return N + M - 2 * lis if __name__ == "__main__": t = int(input()) for _ in range(t): N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) ob = Solution() print(ob.minInsAndDel(A, B, N, M))
CLASS_DEF FUNC_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 VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
class Solution: def minInsAndDel(self, arr, brr, n, m): def lis(arr, n): if n == 0: return 0 t = [arr[0]] def ind(t, val, n): i = 0 j = n - 1 while i <= j: m = (i + j) // 2 if t[m] == val: return m elif t[m] < val: i = m + 1 else: j = m - 1 return i c = 1 for i in range(1, n): if arr[i] > t[-1]: t.append(arr[i]) c += 1 else: te = ind(t, arr[i], c) t[te] = arr[i] return c d = [(-1) for i in range(10**5 + 1)] for i in range(m): d[brr[i]] = i t = [] c = 0 for val in arr: if d[val] != -1: t.append(d[val]) c += 1 return n + m - 2 * lis(t, c)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST VAR NUMBER 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 RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
class Solution: def minInsAndDel(self, A, B, N, M): c = [] s = set(B) for i in range(len(A)): if A[i] in s: c.append(A[i]) nums = c if len(c) == 0: return M + N a = [] a.append(nums[0]) for i in range(1, len(nums)): if nums[i] > a[-1]: a.append(nums[i]) else: low = 0 high = len(a) - 1 while low <= high: mid = (low + high) // 2 if a[mid] < nums[i]: low = mid + 1 else: high = mid - 1 a[low] = nums[i] return len(A) + len(B) - 2 * len(a)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR 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 VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
class Solution: def minInsAndDel(self, A, B, N, M): chk = set(B) lst = [] ans = 0 for i in A: if i in chk: lst.append(i) else: ans += 1 lis = [0] * len(lst) size = 0 for x in lst: i, j = 0, size while i < j: mid = (i + j) // 2 if lis[mid] < x: i = mid + 1 else: j = mid lis[i] = x size = max(size, i + 1) ans += len(lst) - size + len(B) - size return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR 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 ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
def bs(A, key): l = 0 r = len(A) - 1 ans = r + 1 while l <= r: mid = l + (r - l >> 1) if A[mid] > key: r = mid - 1 ans = mid elif A[mid] == key: return mid else: l = mid + 1 return ans def lis(A): N = len(A) if N == 0: return 0 dp = [A[0]] for i in range(1, N): ix = bs(dp, A[i]) if ix == len(dp): dp.append(A[i]) else: dp[ix] = A[i] return len(dp) class Solution: def minInsAndDel(self, A, B, N, M): mp = {} for i in range(M): mp[B[i]] = i res = [] for i in range(N): if A[i] in mp: res.append(mp[A[i]]) c = lis(res) return N - c + M - c
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR 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 ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
class Solution: def minInsAndDel(self, A, B, N, M): t = [] B = set(B) for a in A: if a in B: if len(t) == 0 or t[-1] < a: t.append(a) else: i = self.binary_search(t, a, 0, len(t) - 1) t[i + 1] = a return N + M - 2 * len(t) def binary_search(self, t, a, l, h): if l > h: return h mid = (l + h) // 2 if t[mid] < a: return self.binary_search(t, a, mid + 1, h) else: return self.binary_search(t, a, l, mid - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER
Given two Arrays A[] and B[] of length N and M respectively. Find the minimum number of insertions and deletions on the array A[], required to make both the arrays identical. Note: Array B[] is sorted and all its elements are distinct, operations can be performed at any index not necessarily at end. Example 1: Input: N = 5, M = 3 A[] = {1, 2, 5, 3, 1} B[] = {1, 3, 5} Output: 4 Explanation: We need to delete 2 and replace it with 3. This costs 2 steps. Further, we will have to delete the last two elements from A to obtain an identical array to B. Overall, it results in 4 steps. Example 2: Input: N = 2, M = 2 A[] = {1, 4} B[] = {1, 4} Output : 0 Explanation: Both the Arrays are already identical. Your Task: You don't need to read input or print anything. Your task is to complete the function minInsAndDel() which takes two integers N and M, and two arrays A of size N and B of size M respectively as input and returns the minimum insertions and deletions required. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[i], B[i] ≤ 10^{5}
def ceil(arr, left, right, ele): c = -1 while left <= right: mid = (left + right) // 2 if arr[mid] > ele: c = mid right = mid - 1 elif arr[mid] < ele: left = mid + 1 else: return mid return c def LIS(arr): res = [] res.append(arr[0]) for i in range(1, len(arr)): if res[-1] < arr[i]: res.append(arr[i]) else: k = ceil(res, 0, len(res), arr[i]) res[k] = arr[i] return len(res) class Solution: def minInsAndDel(self, A, B, N, M): b_set = {i: (1) for i in B} a = [] for i in A: if i in b_set.keys(): a.append(i) if len(a) == 0: return N + M lis = LIS(a) return N + M - 2 * lis
FUNC_DEF 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 RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR
Given an undirected connected graph with $n$ vertices and $m$ edges. The graph contains no loops (edges from a vertex to itself) and multiple edges (i.e. no more than one edge between each pair of vertices). The vertices of the graph are numbered from $1$ to $n$. Find the number of paths from a vertex $s$ to $t$ whose length differs from the shortest path from $s$ to $t$ by no more than $1$. It is necessary to consider all suitable paths, even if they pass through the same vertex or edge more than once (i.e. they are not simple). Graph consisting of $6$ of vertices and $8$ of edges For example, let $n = 6$, $m = 8$, $s = 6$ and $t = 1$, and let the graph look like the figure above. Then the length of the shortest path from $s$ to $t$ is $1$. Consider all paths whose length is at most $1 + 1 = 2$. $6 \rightarrow 1$. The length of the path is $1$. $6 \rightarrow 4 \rightarrow 1$. Path length is $2$. $6 \rightarrow 2 \rightarrow 1$. Path length is $2$. $6 \rightarrow 5 \rightarrow 1$. Path length is $2$. There is a total of $4$ of matching paths. -----Input----- The first line of test contains the number $t$ ($1 \le t \le 10^4$) —the number of test cases in the test. Before each test case, there is a blank line. The first line of test case contains two numbers $n, m$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m \le 2 \cdot 10^5$) —the number of vertices and edges in the graph. The second line contains two numbers $s$ and $t$ ($1 \le s, t \le n$, $s \neq t$) —the numbers of the start and end vertices of the path. The following $m$ lines contain descriptions of edges: the $i$th line contains two integers $u_i$, $v_i$ ($1 \le u_i,v_i \le n$) — the numbers of vertices that connect the $i$th edge. It is guaranteed that the graph is connected and does not contain loops and multiple edges. It is guaranteed that the sum of values $n$ on all test cases of input data does not exceed $2 \cdot 10^5$. Similarly, it is guaranteed that the sum of values $m$ on all test cases of input data does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single number — the number of paths from $s$ to $t$ such that their length differs from the length of the shortest path by no more than $1$. Since this number may be too large, output it modulo $10^9 + 7$. -----Examples----- Input 4 4 4 1 4 1 2 3 4 2 3 2 4 6 8 6 1 1 4 1 6 1 5 1 2 5 6 4 6 6 3 2 6 5 6 1 3 3 5 5 4 3 1 4 2 2 1 1 4 8 18 5 1 2 1 3 1 4 2 5 2 6 5 7 3 8 4 6 4 8 7 1 4 4 7 1 6 6 7 3 8 8 5 4 5 4 3 8 2 Output 2 4 1 11 -----Note----- None
import sys I = lambda: [*map(int, sys.stdin.readline().split())] (t,) = I() for _ in range(t): I() n, m = I() s, t = I() M = 10**9 + 7 s -= 1 t -= 1 g = [[] for i in range(n)] for i in range(m): u, v = I() g[u - 1].append(v - 1) g[v - 1].append(u - 1) distances = [M] * n distances[s] = 0 i = 0 paths = {s: 1} tot = 0 while i <= distances[t]: i += 1 newpaths = {} for v in paths: for w in g[v]: if distances[w] >= i - 1: distances[w] = min(distances[w], i) if w in newpaths: newpaths[w] = (newpaths[w] + paths[v]) % M else: newpaths[w] = paths[v] paths = newpaths if t in paths: tot += paths[t] print(tot % M)
IMPORT ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
MAXN = 1000000 dp = [0] * (MAXN + 2) dp1 = [0] * (MAXN + 2) next0 = [0] * MAXN next1 = [0] * MAXN def solve(): s = input() n = len(s) last_pos = -1 for i in range(n): if s[i] == "0": for j in range(last_pos + 1, i + 1): next0[j] = i last_pos = i for i in range(last_pos + 1, n): next0[i] = n if next0[0] == n: print("0") return last_pos = -1 for i in range(n): if s[i] == "1": for j in range(last_pos + 1, i + 1): next1[j] = i last_pos = i for i in range(last_pos + 1, n): next1[i] = n dp[n] = dp[n + 1] = 0 dp1[n] = dp[n + 1] = 0 for i in range(n - 1, -1, -1): dp[i] = dp[i + 1] if s[i] == "0" and next1[i] < n: dp[i] = max(dp[i], dp[next1[i] + 1] + 1) if s[i] == "1" and next0[i] < n: dp[i] = max(dp[i], dp[next0[i] + 1] + 1) dp1[i] = dp1[i + 1] if next1[i] < n: dp1[i] = max(dp1[i], dp[next1[i] + 1] + 1) len1 = dp1[0] + 1 curind = next1[0] + 1 ans = "1" for i in range(1, len1): if curind >= n: ans = ans + "0" continue if next0[curind] >= n: ans = ans + "0" curind = next0[curind] + 1 continue if dp[next0[curind] + 1] < len1 - i - 1: ans = ans + "0" curind = next0[curind] + 1 else: ans = ans + "1" curind = next1[curind] + 1 print(ans) return t = int(input()) for i in range(t): solve()
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR STRING IF VAR VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
for t in range(int(input())): a = input() if "0" not in a: print(0) continue if "1" not in a: print(1) continue n = len(a) for i in range(n): if a[i] == "1": a = a[i + 1 :] break if a == "": print(10) continue n = len(a) next0 = [(-1) for i in range(n)] next1 = [(-1) for i in range(n)] b = [(0) for i in range(n)] p0 = -1 p1 = -1 s = set() c = 0 for i in range(n - 1, -1, -1): b[i] = c next0[i] = p0 next1[i] = p1 if a[i] == "1": p1 = i else: p0 = i s.add(a[i]) if "0" in s and "1" in s: c += 1 s = set() ans = "1" while 1: if p0 == -1: ans += "0" break elif p1 == -1: ans += "1" break if b[p0] <= b[p1]: ans += "0" p1 = next1[p0] p0 = next0[p0] else: ans += "1" p0 = next0[p1] p1 = next1[p1] print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR IF STRING VAR STRING VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING WHILE NUMBER IF VAR NUMBER VAR STRING IF VAR NUMBER VAR STRING IF VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
for i in range(int(input())): s = input() n = len(s) i = 0 zero = [0] * n one = [0] * n dp0 = [0] * (n + 2) dp1 = [0] * (n + 2) pos = -1 for i in range(n): if s[i] == "0": for j in range(pos + 1, i + 1): zero[j] = i pos = i for i in range(pos + 1, n): zero[i] = n if zero[0] == n: print(0) else: pos = -1 for i in range(n): if s[i] == "1": for j in range(pos + 1, i + 1): one[j] = i pos = i for i in range(pos + 1, n): one[i] = n for i in range(n - 1, -1, -1): dp0[i] = dp0[i + 1] if s[i] == "0" and one[i] < n: dp0[i] = max(dp0[i], dp0[one[i] + 1] + 1) if s[i] == "1" and zero[i] < n: dp0[i] = max(dp0[i], dp0[zero[i] + 1] + 1) dp1[i] = dp1[i + 1] if one[i] < n: dp1[i] = max(dp1[i], dp0[one[i] + 1] + 1) x = dp1[0] + 1 ans = "1" count = one[0] + 1 for i in range(1, x): if count >= n: ans += "0" continue if zero[count] >= n: ans += "0" count = zero[count] + 1 continue if dp0[zero[count] + 1] < x - i - 1: ans += "0" count = zero[count] + 1 else: ans += "1" count = one[count] + 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
size = 10**6 pos0 = [0] * size pos1 = [0] * size ary1 = [0] * (size + 2) ary2 = [0] * (size + 2) for _ in range(int(input())): s = str(input()) s_len = len(s) last_pos_0 = -1 last_pos_1 = -1 for i in range(s_len): if s[i] == "0": for j in range(last_pos_0 + 1, i + 1): pos0[j] = i last_pos_0 = i else: for k in range(last_pos_1 + 1, i + 1): pos1[k] = i last_pos_1 = i for i in range(last_pos_0 + 1, s_len): pos0[i] = s_len if pos0[0] == s_len: print("0") continue for i in range(last_pos_1 + 1, s_len): pos1[i] = s_len ary1[s_len] = 0 ary1[s_len + 1] = 0 ary2[s_len] = 0 ary2[s_len + 1] = 0 for i in range(s_len - 1, -1, -1): ary1[i] = ary1[i + 1] ary2[i] = ary2[i + 1] if s[i] == "0" and pos1[i] < s_len: ary1[i] = max(ary1[i], ary1[pos1[i] + 1] + 1) if s[i] == "1" and pos0[i] < s_len: ary1[i] = max(ary1[i], ary1[pos0[i] + 1] + 1) if pos1[i] < s_len: ary2[i] = max(ary2[i], ary1[pos1[i] + 1] + 1) lenght = ary2[0] + 1 index = pos1[0] + 1 ans = "1" for i in range(1, lenght): if index >= s_len: ans += "0" elif pos0[index] >= s_len or ary1[pos0[index] + 1] < lenght - i - 1: ans += "0" index = pos0[index] + 1 else: ans += "1" index = pos1[index] + 1 print(ans)
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
t = int(input()) for i in range(0, t): s = input() l = len(s) next0 = [(-1) for x in range(0, l)] next1 = [(-1) for x in range(0, l)] lens = [(0) for x in range(0, l)] j = l - 1 z = o = 0 c = 1 n0 = -1 n1 = -1 while j >= 0: lens[j] = c next0[j] = n0 next1[j] = n1 if s[j] == "0": z = 1 n0 = j else: o = 1 n1 = j if z == 1 & o == 1: c = c + 1 z = o = 0 j = j - 1 if n0 == -1: print("0") continue if n1 == -1: print("1") continue str = "" j = n1 while True: str = str + s[j] if next0[j] == -1: str = str + "0" break elif next1[j] == -1: str = str + "1" break elif lens[next0[j]] <= lens[next1[j]]: j = next0[j] else: j = next1[j] print(str)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
for _ in range(int(input())): s = input() inf = 10**6 n = len(s) next0 = [inf] * n next1 = [inf] * n n0 = inf n1 = inf for i in range(n - 1, -1, -1): next0[i] = n0 next1[i] = n1 if s[i] == "1": n1 = i else: n0 = i if n1 == inf: print("1") continue if n0 == inf: print("0") continue len1 = [0] * n one = False zero = False cnt = 0 for i in range(n - 1, -1, -1): len1[i] = cnt if s[i] == "1": one = True if s[i] == "0": zero = True if one and zero: cnt += 1 one = False zero = False ans = [] curr = n1 while True: ans.append(str(s[curr])) p0 = next0[curr] p1 = next1[curr] if p0 == inf: ans.append("0") break elif p1 == inf: ans.append("1") break elif len1[p0] <= len1[p1]: curr = p0 else: curr = p1 print("".join(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
m = 1000000 a, b = [0] * m, [0] * m d0, d1 = [0] * (m + 2), [0] * (m + 2) def mac(): s = input() n = len(s) pos = -1 for i in range(n): if s[i] == "0": for j in range(pos + 1, i + 1): a[j] = i pos = i for i in range(pos + 1, n): a[i] = n if a[0] == n: return "0" pos = -1 for i in range(n): if s[i] == "1": for j in range(pos + 1, i + 1): b[j] = i pos = i if s[i] == "1" and a[i] < n: d0[i] = max(d0[i], d0[a[i] + 1] + 1) d1[i] = d1[i + 1] if b[i] < n: d1[i] = max(d1[i], d0[b[i] + 1] + 1) for i in range(pos + 1, n): b[i] = n d0[n + 1] = d0[n] = 0 d1[n + 1] = d1[n] = 0 for i in range(n - 1, -1, -1): d0[i] = d0[i + 1] if s[i] == "0" and b[i] < n: d0[i] = max(d0[i], d0[b[i] + 1] + 1) if s[i] == "1" and a[i] < n: d0[i] = max(d0[i], d0[a[i] + 1] + 1) d1[i] = d1[i + 1] if b[i] < n: d1[i] = max(d1[i], d0[b[i] + 1] + 1) x = d1[0] + 1 count = b[0] + 1 ans = "1" for i in range(1, x): if count >= n: ans += "0" continue if a[count] >= n: ans += "0" count = a[count] + 1 continue if d0[a[count] + 1] < x - i - 1: ans += "0" count = a[count] + 1 else: ans += "1" count = b[count] + 1 return ans for _ in range(int(input())): print(mac())
ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR RETURN STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
for t in range(int(input())): inputString = input() if inputString == "0" * len(inputString): print("1") else: some = 0 while inputString[some] == "0": some += 1 s = inputString[some:] n = len(s) next01 = [[-1, -1] for i in range(n)] queueForZero = [] queueForOne = [] for i in range(n): if s[i] == "0": for j in range(len(queueForZero)): next01[queueForZero[j]][0] = i queueForZero.clear() elif s[i] == "1": for j in range(len(queueForOne)): next01[queueForOne[j]][1] = i queueForOne.clear() queueForOne.append(i) queueForZero.append(i) if inputString == "1" * n: print("0") else: ans = "1" i = 0 if next01[i][0] == -1: print("10") elif next01[i][1] == -1: print("11") else: x0 = next01[i][0] x1 = next01[i][1] while True: x0 = next01[i][0] x1 = next01[i][1] if x0 == -1: ans += "0" break if x1 == -1: ans += "1" break if x0 < x1: if next01[x0][0] == -1: ans += "00" break if next01[x1][0] == -1: ans += "10" break if next01[x1][1] == -1: ans += "11" break else: if next01[x0][0] == -1: ans += "00" break if next01[x0][1] == -1: ans += "01" break if next01[x1][1] == -1: ans += "11" break if ( x0 > 0 and x1 > 0 and next01[x0][0] > 0 and next01[x0][1] > 0 and next01[x1][0] > 0 and next01[1][1] > 0 ): if x0 > x1: ans += "0" i = x0 elif x1 - x0 > 1: ans += "1" i = x1 else: j = i + 1 while j + 1 < n and s[j] == "0" and s[j + 1] == "1": j += 2 if j == n: rem = (n - (i + 1)) // 2 ans += "0" * rem ans += "0" break if s[j] == "1": rem = 1 + (j - (i + 1)) // 2 ans += "0" * rem i = next01[j][0] if i == -1: break else: rem = (j - (i + 1)) // 2 ans += "1" * rem i = j - 1 continue print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR STRING IF VAR NUMBER VAR STRING IF VAR VAR IF VAR VAR NUMBER NUMBER VAR STRING IF VAR VAR NUMBER NUMBER VAR STRING IF VAR VAR NUMBER NUMBER VAR STRING IF VAR VAR NUMBER NUMBER VAR STRING IF VAR VAR NUMBER NUMBER VAR STRING IF VAR VAR NUMBER NUMBER VAR STRING IF VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP STRING VAR VAR STRING IF VAR VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP STRING VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
t = int(input()) for _ in range(t): s = input() s_len = len(s) next0 = [-1] * s_len lastpos = -1 for i in range(s_len): if s[i] == "0": for j in range(lastpos + 1, i + 1): next0[j] = i lastpos = i for i in range(lastpos + 1, s_len): next0[i] = s_len if next0[0] == s_len: print("0") continue next1 = [-1] * s_len lastpos = -1 for i in range(s_len): if s[i] == "1": for j in range(lastpos + 1, i + 1): next1[j] = i lastpos = i for i in range(lastpos + 1, s_len): next1[i] = s_len dp = [-1] * (s_len + 2) dp1 = [-1] * (s_len + 2) dp[s_len] = dp[s_len + 1] = 0 dp1[s_len] = dp1[s_len + 1] = 0 for i in range(s_len - 1, -1, -1): dp[i] = dp[i + 1] if s[i] == "0" and next1[i] < s_len: dp[i] = max(dp[i], dp[next1[i] + 1] + 1) if s[i] == "1" and next0[i] < s_len: dp[i] = max(dp[i], dp[next0[i] + 1] + 1) dp1[i] = dp1[i + 1] if next1[i] < s_len: dp1[i] = max(dp1[i], dp[next1[i] + 1] + 1) a_len = dp1[0] + 1 curr_id = next1[0] + 1 ans = "1" for i in range(1, a_len): if curr_id >= s_len: ans += "0" continue if next0[curr_id] >= s_len: ans += "0" curr_id = next0[curr_id] + 1 continue if dp[next0[curr_id] + 1] < a_len - i - 1: ans += "0" curr_id = next0[curr_id] + 1 else: ans += "1" curr_id = next1[curr_id] + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
T = int(input()) for _ in range(T): S = input() n = len(S) lenA = [(0) for _ in range(n)] count = 0 one = zero = False for i in range(n - 1, -1, -1): lenA[i] = count if S[i] == "0": zero = True else: one = True if one and zero: count += 1 one = zero = False next1 = [None] * n next0 = [None] * n one = zero = None for i in range(n - 1, -1, -1): next1[i] = one next0[i] = zero if S[i] == "0": zero = i else: one = i if one == None: print(1) elif zero == None: print(0) else: result = ["1"] i = one while True: if next0[i] == None: result.append("0") break elif next1[i] == None: result.append("1") break if lenA[next1[i]] >= lenA[next0[i]]: i = next0[i] result.append("0") else: i = next1[i] result.append("1") print("".join(result))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE EXPR FUNC_CALL VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST STRING ASSIGN VAR VAR WHILE NUMBER IF VAR VAR NONE EXPR FUNC_CALL VAR STRING IF VAR VAR NONE EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
test = int(input()) def solve(s): arr = "1" if "0" not in s: print(0) else: strl = len(s) li1 = [0] * strl li2 = [0] * strl mem1 = [0] * (strl + 1) mem2 = [0] * (strl + 1) zero = -1 for i in range(0, strl): if s[i] == "0": for j in range(zero + 1, i + 1): li1[j] = i zero = i one = -1 for i in range(0, strl): if s[i] == "1": for j in range(one + 1, i + 1): li2[j] = i one = i for i in range(zero + 1, strl): li1[i] = strl for i in range(one + 1, strl): li2[i] = strl for i in range(strl - 1, -1, -1): mem1[i] = mem1[i + 1] if s[i] == "0" and li2[i] < strl: mem1[i] = max(mem1[i], mem1[li2[i] + 1] + 1) elif s[i] == "1" and li1[i] < strl: mem1[i] = max(mem1[i], mem1[li1[i] + 1] + 1) mem2[i] = mem2[i + 1] if li2[i] < strl: mem2[i] = max(mem2[i], mem1[li2[i] + 1] + 1) iter = li2[0] iter += 1 z = 1 + mem2[0] for i in range(1, z): y = 1 + mem2[0] - i - 1 if iter >= strl: arr += "0" elif li1[iter] >= strl: arr += "0" iter = li1[iter] + 1 elif mem1[li1[iter] + 1] < y: arr += "0" iter = li1[iter] + 1 else: arr += "1" iter = li2[iter] + 1 print(arr) for i in range(0, test): s = input() solve(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR STRING IF STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
n0 = [-1] * 10000000 n1 = [-1] * 10000000 arr = [-1] * 10000000 arr1 = [-1] * 10000000 for _ in range(int(input())): s = input() l = len(s) lp = -1 for i in range(0, l): if s[i] == "0": for j in range(lp + 1, i + 1): n0[j] = i lp = i for i in range(lp + 1, l): n0[i] = l if n0[0] == l: print(0) continue lp = -1 for i in range(0, l): if s[i] == "1": for j in range(lp + 1, i + 1): n1[j] = i lp = i for i in range(lp + 1, l): n1[i] = l arr[l + 1] = 0 arr[l] = 0 arr1[l + 1] = 0 arr1[l] = 0 i = l - 1 while i >= 0: arr[i] = arr[i + 1] if s[i] == "0" and n1[i] < l: arr[i] = max(arr[i], arr[n1[i] + 1] + 1) if s[i] == "1" and n0[i] < l: arr[i] = max(arr[i], arr[n0[i] + 1] + 1) arr1[i] = arr1[i + 1] if n1[i] < l: arr1[i] = max(arr1[i], arr[n1[i] + 1] + 1) i -= 1 ans = "1" curr = n1[0] + 1 le = arr1[0] + 1 for i in range(1, le): if curr >= l: ans += "0" continue if n0[curr] >= l: ans += "0" curr = n0[curr] + 1 continue if arr[n0[curr] + 1] < le - i - 1: ans += "0" curr = n0[curr] + 1 else: ans += "1" curr = n1[curr] + 1 print(ans)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR 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 IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
t = int(input()) for _ in range(t): s = input() if s.count("0") == 0: print(0) elif s.count("1") == 0: print(1) else: n = len(s) l = [0] * n cnt = 0 one = 0 zero = 0 for i in range(n - 1, -1, -1): l[i] = cnt if s[i] == "1": one = 1 if s[i] == "0": zero = 1 if zero and one: cnt += 1 one = 0 zero = 0 s1 = "" ind = 0 for i in range(n): if s[i] == "1": s1 += "1" ind = i break zero = 0 one = 0 a1 = b1 = 0 a2 = b2 = 0 i = ind + 1 while i < n: if zero == 0 and s[i] == "0": zero = 1 a1 = i b1 = l[i] if one == 0 and s[i] == "1": one = 1 a2 = i b2 = l[i] if one and zero: if b1 < b2: s1 += "0" i = a1 + 1 elif b1 > b2: s1 += "1" i = a2 + 1 else: s1 += "0" i += 1 one = 0 zero = 0 else: i += 1 if one == 0 and zero == 1: s1 += "1" elif zero == 0 and one == 1: s1 += "0" else: s1 += "0" print(s1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR STRING IF VAR NUMBER VAR NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
for _ in range(int(input())): s = input() n = len(s) m = n z_pos = n o_pos = n i = n - 1 z = [0] * n o = [0] * n while i >= 0: if s[i] == "1": o_pos = i else: z_pos = i z[i] = z_pos o[i] = o_pos i = i - 1 if z[0] == n: print("0") continue if o[0] == n: print("1") continue res = "1" dp = [0] * (n + 2) k = [0] * (n + 2) i = n - 1 while i >= 0: dp[i] = dp[i + 1] k[i] = k[i + 1] if s[i] == "0": if o[i] < n: dp[i] = max(dp[i], dp[o[i] + 1] + 1) elif z[i] < n: dp[i] = max(dp[i], dp[z[i] + 1] + 1) if o[i] < n: k[i] = max(k[i], dp[o[i] + 1] + 1) if i == 0: m = k[i] + 1 pos = o[i] + 1 i = i - 1 for i in range(1, m): if pos >= n: res = res + "0" continue if z[pos] >= n: pos = z[pos] + 1 res = res + "0" continue if dp[z[pos] + 1] >= m - i - 1: pos = o[pos] + 1 res = res + "1" else: pos = z[pos] + 1 res = res + "0" print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR STRING IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
import sys for _ in range(int(input())): s = input() n = len(s) next0 = [n] * n next1 = [n] * n n0, n1 = n, n for i in range(n - 1, -1, -1): next0[i] = n0 next1[i] = n1 if s[i] == "1": n1 = i else: n0 = i length = [0] * n zero, one = False, False cnt = 0 if n0 == n: print("0") continue if n1 == n: print("1") continue for i in range(n - 1, -1, -1): if s[i] == "0": zero = True if s[i] == "1": one = True length[i] = cnt if one and zero: cnt += 1 zero, one = False, False ans = "" curr = n1 while True: ans += s[curr] p0, p1 = next0[curr], next1[curr] if p0 == n: ans += "0" break if p1 == n: ans += "1" break if length[p0] <= length[p1]: curr = p0 else: curr = p1 print(ans)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR VAR WHILE NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR STRING IF VAR VAR VAR STRING IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
def solve(): string = input() le = len(string) first_list = [] second_list = [] dp0 = [0, 0] dp1 = [0, 0] prev0 = 0 prev1 = 0 for i in range(le): if string[i] == "0": for j in range(prev0, i + 1): first_list.append(i) dp0.append(0) prev0 = i + 1 elif string[i] == "1": for k in range(prev1, i + 1): second_list.append(i) dp1.append(0) prev1 = i + 1 if len(first_list) == 0: print(0) return if len(second_list) == 0: print(1) return if len(first_list) > len(second_list): for i in range(len(second_list), len(first_list)): second_list.append(le) dp1.append(0) elif len(first_list) < len(second_list): for i in range(len(first_list), len(second_list)): first_list.append(le) dp0.append(0) for i in reversed(range(le)): dp0[i] = dp0[i + 1] if string[i] == "0" and second_list[i] < le: dp0[i] = max(dp0[i], dp0[second_list[i] + 1] + 1) if string[i] == "1" and first_list[i] < le: dp0[i] = max(dp0[i], dp0[first_list[i] + 1] + 1) dp1[i] = dp1[i + 1] if second_list[i] < le: dp1[i] = max(dp1[i], dp0[second_list[i] + 1] + 1) l = dp1[0] + 1 x = second_list[0] + 1 final_ans = "1" for i in range(1, l): if x >= le: final_ans += "0" continue if first_list[x] >= le: final_ans += "0" x = first_list[x] + 1 continue if dp0[first_list[x] + 1] < l - i - 1: final_ans += "0" x = first_list[x] + 1 else: final_ans += "1" x = second_list[x] + 1 print(final_ans) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
def solve(s): n = len(s) next0 = [mx] * n next1 = [mx] * n n0, n1 = mx, mx for i in range(n - 1, -1, -1): next0[i] = n0 next1[i] = n1 if s[i] == "1": n1 = i else: n0 = i if n0 == mx: return 0 if n1 == mx: return 1 dplen = [0] * n one, zero = False, False count = 0 for i in range(n - 1, -1, -1): dplen[i] = count if s[i] == "0": zero = True if s[i] == "1": one = True if one and zero: count += 1 one, zero = False, False ans = "" curr = n1 while True: ans += s[curr] p0 = next0[curr] p1 = next1[curr] if p0 == mx: ans += "0" break if p1 == mx: ans += "1" break if dplen[p0] <= dplen[p1]: curr = p0 else: curr = p1 return ans mx = 1000005 for t in range(int(input())): s = input() print(solve(s))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR VAR WHILE NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR STRING IF VAR VAR VAR STRING IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
MAX = 10**6 zero = [0] * MAX ones = [0] * MAX change = [0] * (MAX + 2) group = [0] * (MAX + 2) def solve(): s = input() l = len(s) i0, i1, j = 0, 0, 0 for bit in s: if int(bit): for k in range(i1, j + 1): ones[k] = j i1 = j + 1 else: for k in range(i0, j + 1): zero[k] = j i0 = j + 1 j += 1 for k in range(i0, l): zero[k] = l if zero[0] == l: print(0) return for k in range(i1, l): ones[k] = l change[l], change[l + 1], group[l], group[l + 1] = 0, 0, 0, 0 for k in range(l - 1, -1, -1): change[k] = change[k + 1] if int(s[k]) and zero[k] < l: change[k] = max(change[k], change[zero[k] + 1] + 1) elif not int(s[k]) and ones[k] < l: change[k] = max(change[k], change[ones[k] + 1] + 1) group[k] = group[k + 1] if ones[k] < l: group[k] = max(group[k], change[ones[k] + 1] + 1) l2 = group[0] + 1 curr = ones[0] + 1 mex = "1" for k in range(1, l2): if curr >= l: mex += "0" elif change[zero[curr] + 1] < l2 - k - 1 or zero[curr] >= l: mex += "0" curr = zero[curr] + 1 else: mex += "1" curr = ones[curr] + 1 print(mex) for _ in range(int(input())): solve()
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
for _ in range(int(input())): s = input() ccc0 = 0 ccc1 = 0 c = [] g = -1 for zz in range(len(s) - 2, -1, -1): if s[zz] == "1": ccc1 += 1 if s[zz + 1] == "1": g = "0" c.append([g, ccc1 + 1, ccc0]) else: c.append([g, ccc1, ccc0 + 1]) else: ccc0 += 1 if s[zz + 1] == "0": g = "1" c.append([g, ccc1, ccc0 + 1]) else: c.append([g, ccc1 + 1, ccc0]) if s[-1] == "0": ccc0 += 1 else: ccc1 += 1 if ccc0 == 0: print(0) elif ccc1 == 0: print(1) else: while True: if s[0] == "0": s = s[:0] + s[1:] else: break b = "1" pos0 = -1 pos1 = -1 i = 1 j = 1 start = 1 while i < len(s) and j < len(s): while i < len(s): flag = 0 if s[i] == "0": flag = 1 pos0 = i break i += 1 while j < len(s): ff = 0 if s[j] == "1": ff = 1 pos1 = j break j += 1 if flag == 0: b += "0" break elif ff == 0: b += "1" break elif abs(pos1 - pos0) == 1: if pos0 > pos1: b += "0" i = pos0 + 1 j = pos0 + 1 elif pos1 == len(s) - 1: b += "0" i = pos0 + 1 j = pos0 + 1 else: qq0 = 0 qq1 = 0 if c[len(s) - 2 - pos0][0] == "1": b += "1" i = pos1 + 1 j = pos1 + 1 elif c[len(s) - pos0 - 2][0] == "0": b += "0" i = pos0 + 1 j = pos0 + 1 elif c[len(s) - pos0 - 2][0] == -1: if c[len(s) - pos0 - 2][1] <= c[len(s) - pos0 - 2][2]: b += "0" i = pos0 + 1 j = pos0 + 1 else: b += "1" i = pos1 + 1 j = pos1 + 1 elif pos0 > pos1: b += "0" i = pos0 + 1 j = pos0 + 1 else: b += "1" i = pos1 + 1 j = pos1 + 1 if i == len(s) and j == len(s): b += "0" print(b)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE NUMBER IF VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR STRING IF VAR NUMBER VAR STRING IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER STRING VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
for t in range(int(input())): s = input() if "0" not in s: print("0") continue n = len(s) a, b = [0] * n, [0] * n c0, c1 = n, n for i in range(n - 1, -1, -1): if s[i] == "0": c0 = i else: c1 = i a[i] = c0 b[i] = c1 dp0, dp1 = [0] * (n + 1), [0] * (n + 1) for i in range(n - 1, -1, -1): dp0[i] = dp0[i + 1] if s[i] == "0" and b[i] < n: dp0[i] = max(dp0[i], dp0[b[i] + 1] + 1) if s[i] == "1" and a[i] < n: dp0[i] = max(dp0[i], dp0[a[i] + 1] + 1) dp1[i] = dp1[i + 1] if b[i] < n: dp1[i] = max(dp1[i], dp0[b[i] + 1] + 1) l, x = dp1[0] + 1, b[0] + 1 res = "1" for i in range(1, l): if x >= n: res += "0" elif a[x] >= n: res += "0" x = a[x] + 1 elif dp0[a[x] + 1] < l - i - 1: res += "0" x = a[x] + 1 else: res += "1" x = b[x] + 1 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
q = int(input()) for i in range(q): w = list(map(str, input())) num0 = [] num1 = [] k = 0 count = 0 l = 0 for j in range(len(w)): if w[j] == "0": num0.append(j) count = count + 1 for m in range(k): num0.append(j) k = 0 else: k = k + 1 for m in range(k): num0.append(len(w)) for j in range(len(w)): if w[j] == "1": num1.append(j) for m in range(l): num1.append(j) l = 0 else: l = l + 1 for m in range(l): num1.append(len(w)) dp = [None] * (len(w) + 2) dp1 = [None] * (len(w) + 2) dp[len(w)] = dp1[len(w)] = dp[len(w) + 1] = dp1[len(w) + 1] = 0 for j in range(len(w) - 1, -1, -1): dp[j] = dp[j + 1] if w[j] == "0" and num1[j] < len(w): dp[j] = max(dp[j], dp[num1[j] + 1] + 1) if w[j] == "1" and num0[j] < len(w): dp[j] = max(dp[j], dp[num0[j] + 1] + 1) dp1[j] = dp1[j + 1] if num1[j] < len(w): dp1[j] = max(dp1[j], dp[num1[j] + 1] + 1) lo = dp1[0] + 1 x = num1[0] + 1 ans = "1" for j in range(1, lo): if x >= len(w): ans = ans + "0" continue if num0[x] >= len(w): ans = ans + "0" continue if dp[num0[x] + 1] < lo - j - 1: ans = ans + "0" x = num0[x] + 1 else: ans = ans + "1" x = num1[x] + 1 if count == 0: print("0") else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
def cal(l): n = len(l) pos = -1 for i in range(n): if l[i] == "0": for j in range(pos + 1, i + 1): arr1[j] = i pos = i for i in range(pos + 1, n): arr1[i] = n if arr1[0] == n: return 0 pos = -1 for i in range(n): if l[i] == "1": for j in range(pos + 1, i + 1): arr2[j] = i pos = i for i in range(pos + 1, n): arr2[i] = n temp1[n] = 0 temp1[n + 1] = 0 temp2[n] = 0 temp2[n + 1] = 0 for i in range(n - 1, -1, -1): temp1[i] = temp1[i + 1] if l[i] == "0" and arr2[i] < n: temp1[i] = max(temp1[i], temp1[arr2[i] + 1] + 1) if l[i] == "1" and arr1[i] < n: temp1[i] = max(temp1[i], temp1[arr1[i] + 1] + 1) temp2[i] = temp2[i + 1] if arr2[i] < n: temp2[i] = max(temp2[i], temp1[arr2[i] + 1] + 1) x = temp2[0] + 1 coun = arr2[0] + 1 ans = "1" for i in range(1, x): if coun >= n: ans += "0" continue if arr1[coun] >= n: ans += "0" coun = arr1[coun] + 1 continue if temp1[arr1[coun] + 1] < x - i - 1: ans += "0" coun = arr1[coun] + 1 else: ans += "1" coun = arr2[coun] + 1 return ans for _ in range(int(input())): t = input() maxy = len(t) + 10 arr1 = [(0) for i in range(maxy)] arr2 = [(0) for i in range(maxy)] temp1 = [(0) for i in range(maxy + 2)] temp2 = [(0) for i in range(maxy + 2)] print(cal(t))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
import sys input_ = lambda: sys.stdin.readline().strip("\r\n") t = int(input_()) for _ in range(t): s = input_() n = len(s) inf = n n0 = inf n1 = inf next0 = [0] * n next1 = [0] * n for i in range(n - 1, -1, -1): next0[i] = n0 next1[i] = n1 if s[i] == "1": n1 = i else: n0 = i if n1 == inf: print("1") continue if n0 == inf: print("0") continue length = [0] * n one = False two = False cnt = 0 for i in range(n - 1, -1, -1): length[i] = cnt if s[i] == "1": one = True else: two = True if one and two: cnt += 1 one, two = False, False ans = "" cur = n1 while True: ans += s[cur] p0, p1 = next0[cur], next1[cur] if p0 == n: ans += "0" break if p1 == n: ans += "1" break if length[p0] <= length[p1]: cur = p0 else: cur = p1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR VAR WHILE NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR STRING IF VAR VAR VAR STRING IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
def spar(s, n): if "0" not in s: print("0") return last_pos = -1 dp, dp1 = [0] * (n + 2), [0] * (n + 2) next0, next1 = [0] * (n + 2), [0] * (n + 2) for i in range(n): if s[i] == "0": for j in range(last_pos + 1, i + 1): next0[j] = i last_pos = i for i in range(last_pos + 1, n): next0[i] = n last_pos = -1 for i in range(n): if s[i] == "1": for j in range(last_pos + 1, i + 1): next1[j] = i last_pos = i for i in range(last_pos + 1, n): next1[i] = n dp[n] = dp[n + 1] = 0 dp1[n] = dp1[n + 1] = 0 for i in range(n - 1, -1, -1): dp[i] = dp[i + 1] if s[i] == "0" and next1[i] < n: dp[i] = max(dp[i], dp[next1[i] + 1] + 1) if s[i] == "1" and next0[i] < n: dp[i] = max(dp[i], dp[next0[i] + 1] + 1) dp1[i] = dp1[i + 1] if next1[i] < n: dp1[i] = max(dp1[i], dp[next1[i] + 1] + 1) length = dp1[0] + 1 curind = next1[0] + 1 ans = ["1"] for i in range(1, length): if curind >= n: ans.append("0") continue if next0[curind] >= n: ans.append("0") curind = next0[curind] + 1 continue if dp[next0[curind] + 1] < length - i - 1: ans.append("0") curind = next0[curind] + 1 else: ans.append("1") curind = next1[curind] + 1 print("".join(map(str, ans))) try: for _ in range(int(input())): s = input() n = len(s) spar(s, n) except: pass
FUNC_DEF IF STRING VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
t = int(input()) for _ in range(t): s = input() dp = [0] * len(s) zero = one = False flag = 0 for i in range(len(s) - 1, -1, -1): if zero and one: zero = one = False dp[i] = dp[i + 1] + 1 flag = 1 elif flag == 1: dp[i] = dp[i + 1] if s[i] == "0": zero = True if s[i] == "1": one = True next0 = [0] * len(s) next1 = [0] * len(s) n0 = n1 = float("inf") for i in range(len(s) - 1, -1, -1): next0[i] = n0 next1[i] = n1 if s[i] == "0": n0 = i if s[i] == "1": n1 = i if n1 == float("inf"): print("1") continue if n0 == float("inf"): print("0") continue result = "1" i = n1 while True: n0 = next0[i] n1 = next1[i] if n0 == float("inf"): result += "0" break elif n1 == float("inf"): result += "1" break elif dp[n0] <= dp[n1]: result += "0" i = n0 else: result += "1" i = n1 print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR STRING VAR STRING IF VAR FUNC_CALL VAR STRING VAR STRING IF VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
t = int(input()) for _ in range(t): s = input() first1 = s.find("1") first0 = s.find("0") n = len(s) if first1 == -1: print("1") continue if first0 == -1: print("0") continue zeros = [0] * n ones = [0] * n ind0 = n ind1 = n for i in range(n - 1, first1 - 1, -1): zeros[i] = ind0 ones[i] = ind1 if s[i] == "0": ind0 = i else: ind1 = i s += "0" s = list(s) dplen = [1111111] * (n + 1) dpind = [0] * (n + 1) dplen[first1] = 1 dpind[first1] = -1 for i in range(first1, n): next0 = zeros[i] next1 = ones[i] if dplen[next0] > dplen[i] + 1: dplen[next0] = dplen[i] + 1 dpind[next0] = i if next0 == n: s[n] = "0" elif dplen[next0] == dplen[i] + 1: if s[i] == "0" and s[dpind[next0]] == "1": dpind[next0] = i if next0 == n: s[n] = "0" if dplen[next1] > dplen[i] + 1: dplen[next1] = dplen[i] + 1 dpind[next1] = i if next1 == n: s[n] = "1" elif dplen[next1] == dplen[i] + 1: if s[i] == "0" and s[dpind[next1]] == "1": dpind[next1] = i ans = "" ans += s[n] index = dpind[n] while index != -1: ans = s[index] + ans index = dpind[index] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR STRING IF VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR STRING VAR VAR VAR STRING ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR STRING IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR STRING IF VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR STRING VAR VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR STRING VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
n1 = int(input()) for i in range(n1): s = input() if "0" not in s: print(0) elif "1" not in s: print(1) else: n = len(s) l = [0] * n cnt = 0 one = False zero = False for j in range(n - 1, -1, -1): l[j] = cnt if s[j] == "1": one = True if s[j] == "0": zero = True if one and zero: cnt += 1 zero = False one = False p = s.find("1") f = "" while True: f += s[p] p0 = s.find("0", p + 1) p1 = s.find("1", p + 1) if p0 == -1: f += "0" break if p1 == -1: f += "1" break if l[p0] <= l[p1]: p = p0 else: p = p1 print(f)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR STRING WHILE NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER IF VAR NUMBER VAR STRING IF VAR NUMBER VAR STRING IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
list1, list2, list3, list4 = ( [""] * (10**6 + 2), [""] * (10**6 + 2), [""] * 10**6, [""] * 10**6, ) def abc(n, x): l = -1 for i in range(x): if n[i] == "0": for j in range(l + 1, i + 1): list3[j] = i l = i for i in range(l + 1, x): list3[i] = x if list3[0] == x: return 0 l = -1 for i in range(x): if n[i] == "1": for j in range(l + 1, i + 1): list4[j] = i l = i for i in range(l + 1, x): list4[i] = x list1[x] = 0 list1[x + 1] = 0 list2[x] = 0 list2[x + 1] = 0 for i in range(x - 1, -1, -1): list1[i] = list1[i + 1] if n[i] == "0" and list4[i] < x: list1[i] = max(list1[i], list1[list4[i] + 1] + 1) if n[i] == "1" and list3[i] < x: list1[i] = max(list1[i], list1[list3[i] + 1] + 1) list2[i] = list1[i + 1] if list4[i] < x: list2[i] = max(list2[i], list1[list4[i] + 1] + 1) var2 = list2[0] + 1 var3 = list4[0] + 1 sol = "1" for i in range(1, var2): if var3 >= x: sol += "0" continue if list3[var3] >= x: sol += "0" var3 = list3[var3] + 1 continue if list1[list3[var3] + 1] < var2 - i - 1: sol += "0" var3 = list3[var3] + 1 else: sol += "1" var3 = list4[var3] + 1 return sol for _ in range(int(input())): n = input() x = len(n) print(abc(n, x))
ASSIGN VAR VAR VAR VAR BIN_OP LIST STRING BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP LIST STRING BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP LIST STRING BIN_OP NUMBER NUMBER BIN_OP LIST STRING BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
T = int(input()) maxn = 10**6 + 2 for _ in range(T): S = input() N = len(S) next0 = [N for i in range(N)] next1 = [N for i in range(N)] n0 = maxn n1 = maxn for i in range(N - 1, -1, -1): next0[i] = n0 next1[i] = n1 if S[i] == "1": n1 = i else: n0 = i if n0 == maxn: print(0) continue if n1 == maxn: print(1) continue zero, one = False, False count = 0 length = [""] * N for i in range(N - 1, -1, -1): length[i] = count if S[i] == "0": zero = True if S[i] == "1": one = True if one and zero: count += 1 zero, one = False, False ans = "" curr = n1 while 1: ans += S[curr] p1, p0 = next1[curr], next0[curr] if p0 == maxn: ans += "0" break if p1 == maxn: ans += "1" break if length[p0] <= length[p1]: curr = p0 else: curr = p1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR VAR WHILE NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR STRING IF VAR VAR VAR STRING IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
t = int(input()) for _ in range(t): s = input() n = len(s) nx0 = [None] * n nx1 = [None] * n ps1 = -1 ps0 = -1 for i in range(n): if s[i] == "0": for j in range(ps0 + 1, i + 1): nx0[j] = i ps0 = i else: for j in range(ps1 + 1, i + 1): nx1[j] = i ps1 = i for i in range(ps0 + 1, n): nx0[i] = n for i in range(ps1 + 1, n): nx1[i] = n if nx0[0] == n: print(0) continue st0 = [None] * (n + 2) st1 = [None] * (n + 2) st0[n] = 0 st0[n + 1] = 0 st1[n] = 0 st1[n + 1] = 0 for i in range(n - 1, -1, -1): st0[i] = st0[i + 1] if s[i] == "0": if nx1[i] < n: st0[i] = max(st0[i], st0[nx1[i] + 1] + 1) elif nx0[i] < n: st0[i] = max(st0[i], st0[nx0[i] + 1] + 1) for i in range(n - 1, -1, -1): st1[i] = st1[i + 1] if nx1[i] < n: st1[i] = max(st1[i], st0[nx1[i] + 1] + 1) mx = st1[0] + 1 cur = nx1[0] + 1 mis = "1" for i in range(1, mx): if cur >= n: mis += "0" elif nx0[cur] >= n: mis += "0" cur = nx0[cur] + 1 elif st0[nx0[cur] + 1] < mx - i - 1: mis += "0" cur = nx0[cur] + 1 else: mis += "1" cur = nx1[cur] + 1 print(mis)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
import sys t = int(input()) while t > 0: str = input() n = len(str) lp = -1 n0 = [(-1) for i in range(len(str))] n1 = [(-1) for i in range(len(str))] dp1 = [(-1) for i in range(len(str) + 2)] dp2 = [(-1) for i in range(len(str) + 2)] for i in range(n): if str[i] == "0": for j in range(lp + 1, i + 1): n0[j] = i lp = i for i in range(lp + 1, n): n0[i] = n if n0[0] == n: print(0) else: lp = -1 for i in range(n): if str[i] == "1": for j in range(lp + 1, i + 1): n1[j] = i lp = i for i in range(lp + 1, n): n1[i] = n dp1[n] = dp1[n + 1] = 0 dp2[n] = dp2[n + 1] = 0 for i in range(n - 1, -1, -1): dp1[i] = dp1[i + 1] if str[i] == "0" and n1[i] < n: dp1[i] = max(dp1[i], dp1[n1[i] + 1] + 1) if str[i] == "1" and n0[i] < n: dp1[i] = max(dp1[i], dp1[n0[i] + 1] + 1) dp2[i] = dp2[i + 1] if n1[i] < n: dp2[i] = max(dp2[i], dp1[n1[i] + 1] + 1) l = dp2[0] + 1 index = n1[0] + 1 result = "1" for i in range(1, l): if index >= n: result = result + "0" continue if n0[index] >= n: result = result + "0" index = n0[index] + 1 continue if dp1[n0[index] + 1] < l - i - 1: result = result + "0" index = n0[index] + 1 else: result = result + "1" index = n1[index] + 1 print(result) t -= 1
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR STRING IF VAR VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
import itertools test = int(input()) class dparr: def dparrcalc(s, li1, li2, strl): mem1 = [0] * (strl + 1) mem2 = [0] * (strl + 1) for i in range(strl - 1, -1, -1): mem1[i] = mem1[i + 1] if s[i] == "0" and li2[i] < strl: mem1[i] = max(mem1[i], mem1[li2[i] + 1] + 1) elif s[i] == "1" and li1[i] < strl: mem1[i] = max(mem1[i], mem1[li1[i] + 1] + 1) mem2[i] = mem2[i + 1] if li2[i] < strl: mem2[i] = max(mem2[i], mem1[li2[i] + 1] + 1) return mem1, mem2 def solve(s): arr = "1" if "0" not in s: print(0) else: strl = len(s) li1 = [0] * strl li2 = [0] * strl mem1 = [0] * (strl + 1) mem2 = [0] * (strl + 1) zero = -1 for i in range(0, strl): if s[i] == "0": for j in range(zero + 1, i + 1): li1[j] = i zero = i one = -1 for i in range(0, strl): if s[i] == "1": for j in range(one + 1, i + 1): li2[j] = i one = i for i in range(zero + 1, strl): li1[i] = strl for i in range(one + 1, strl): li2[i] = strl x = dparr mem1, mem2 = x.dparrcalc(s, li1, li2, strl) iter = li2[0] iter += 1 for i in range(1, 1 + mem2[0]): y = 1 + mem2[0] - i - 1 if iter >= strl: arr += "0" elif li1[iter] >= strl: arr += "0" iter = li1[iter] + 1 elif mem1[li1[iter] + 1] < y: arr += "0" iter = li1[iter] + 1 else: arr += "1" iter = li2[iter] + 1 print(arr) for i in range(0, test): s = input() solve(s)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR STRING IF STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
for _ in range(int(input())): s = input() n = len(s) c0, c1 = [n] * n, [n] * n dp0, dp1 = [0] * (n + 2), [0] * (n + 2) pos0 = -1 pos1 = -1 for i in range(n): if s[i] == "0": for j in range(pos0 + 1, i + 1): c0[j] = i pos0 = i else: for j in range(pos1 + 1, i + 1): c1[j] = i pos1 = i if c0[0] == n: print("0") else: for i in range(n - 1, -1, -1): dp0[i] = dp0[i + 1] if s[i] == "0" and c1[i] < n: dp0[i] = max(dp0[i], dp0[c1[i] + 1] + 1) if s[i] == "1" and c0[i] < n: dp0[i] = max(dp0[i], dp0[c0[i] + 1] + 1) dp1[i] = dp1[i + 1] if c1[i] < n: dp1[i] = max(dp1[i], dp0[c1[i] + 1] + 1) res = "1" bits = dp1[0] + 1 c = c1[0] + 1 for i in range(1, bits): if c >= n: res += "0" elif c0[c] >= n: res += "0" c = c0[c] + 1 elif dp0[c0[c] + 1] < bits - i - 1: res += "0" c = c0[c] + 1 else: res += "1" c = c1[c] + 1 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST VAR VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
T = int(input()) for t in range(T): binary_string = str(input()) n = len(binary_string) num_zero = [-1] * n num_one = [-1] * n last_zero = n last_one = n for i in range(n - 1, -1, -1): if binary_string[i] == "1": last_one = i else: last_zero = i num_zero[i] = last_zero num_one[i] = last_one dp = [-1] * (n + 2) dp_1 = [-1] * (n + 2) dp[n] = 0 dp[n + 1] = 0 dp_1[n] = 0 dp_1[n + 1] = 0 for i in range(n - 1, -1, -1): dp[i] = dp[i + 1] if binary_string[i] == "0" and num_one[i] < n: dp[i] = max(dp[i], dp[num_one[i] + 1] + 1) if binary_string[i] == "1" and num_zero[i] < n: dp[i] = max(dp[i], dp[num_zero[i] + 1] + 1) dp_1[i] = dp_1[i + 1] if num_one[i] < n: dp_1[i] = max(dp_1[i], dp[num_one[i] + 1] + 1) ans_size = dp_1[0] + 1 x = num_one[0] + 1 if "0" in binary_string: ans = "1" for i in range(1, ans_size): if x >= n: ans += "0" elif num_zero[x] >= n: x = num_zero[x] + 1 ans += "0" elif dp[num_zero[x] + 1] < ans_size - i - 1: x = num_zero[x] + 1 ans += "0" else: x = num_one[x] + 1 ans += "1" else: ans = "0" print(ans)
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 ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF STRING VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
def get_indices(l, i): n = len(l) x, y = -1, -1 while i < n: if l[i] == "0" and x == -1: x = i if l[i] == "1" and y == -1: y = i if x != -1 and y != -1: return x, y i += 1 if x == -1: return i, y if y == -1: return x, i def solve2(): s = input() n = len(s) cnt = list() one = zero = False ans = 0 ones, zeros = 0, 0 for i in range(n - 1, -1, -1): if one and zero: ans += 1 cnt.append(ans) one = zero = False else: cnt.append(ans) if s[i] == "1": one = True ones += 1 else: zero = True zeros += 1 if cnt[0] == 0: if zeros == 0: return zeros if ones == 0: return 1 cnt.reverse() ret = "1" i = 0 while i < n and s[i] != "1": i += 1 while i < n: a, b = get_indices(s, i + 1) if a == n: ret += "0" break if b == n: ret += "1" break if cnt[a] <= cnt[b]: ret += "0" i = a else: ret += "1" i = b return ret for i in range(int(input())): print(solve2())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR VAR IF VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING IF VAR VAR VAR STRING IF VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
def solve(s): if "0" not in s: return "0" if "1" not in s: return "1" n = len(s) nx0 = [n for i in range(n + 1)] nx1 = [n for i in range(n + 1)] for i in range(n - 1, -1, -1): if s[i] == "0": nx0[i] = i else: nx0[i] = nx0[i + 1] if s[i] == "1": nx1[i] = i else: nx1[i] = nx1[i + 1] dp0 = [(0) for i in range(n + 2)] dp1 = [(0) for i in range(n + 2)] dp0[-1] = 0 dp0[-2] = 0 dp1[-1] = 0 dp1[-2] = 0 for i in range(n - 1, -1, -1): dp0[i] = dp0[i + 1] dp1[i] = dp1[i + 1] if s[i] == "0" and nx1[i] < n: dp0[i] = max(dp0[i], dp0[nx1[i] + 1] + 1) if s[i] == "1" and nx0[i] < n: dp0[i] = max(dp0[i], dp0[nx0[i] + 1] + 1) if nx1[i] < n: dp1[i] = max(dp1[i], dp0[nx1[i] + 1] + 1) ans_len = dp1[0] + 1 ans = "1" pos = nx1[0] + 1 for i in range(1, ans_len): if pos >= n: ans += "0" continue if nx0[pos] >= n: ans += "0" pos = nx0[pos] + 1 continue if dp0[nx0[pos] + 1] < ans_len - i - 1: ans += "0" pos = nx0[pos] + 1 else: ans += "1" pos = nx1[pos] + 1 return ans def gen(): t = int(input()) for _ in range(t): s = input() print(solve(s)) gen()
FUNC_DEF IF STRING VAR RETURN STRING IF STRING VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
for _ in range(int(input())): s = input() if "0" not in s: print(0) continue if "1" not in s: print(1) continue start = s.index("1") s = s[start:] if "0" not in s: print(10) continue n = len(s) num0 = [n] * n num1 = [n] * n dp = [0] * (n + 5) dp1 = [0] * (n + 5) for i in range(n): if s[i] == "0": num0[i] = i k = i - 1 while k >= 0 and num0[k] > i: num0[k] = i k -= 1 if s[i] == "1": num1[i] = i k = i - 1 while k >= 0 and num1[k] > i: num1[k] = i k -= 1 for i in range(n - 1, -1, -1): dp[i] = dp[i + 1] if s[i] == "0" and num1[i] < n: dp[i] = max(dp[i], dp[num1[i] + 1] + 1) if s[i] == "1" and num0[i] < n: dp[i] = max(dp[i], dp[num0[i] + 1] + 1) dp1[i] = dp1[i + 1] if num1[i] < n: dp1[i] = max(dp1[i], dp[num1[i] + 1] + 1) ans = "1" l = dp1[0] + 1 x = num1[0] + 1 for i in range(1, l): if x >= n: ans += "0" continue if num0[x] >= n: ans += "0" x = num0[x] + 1 continue if dp[num0[x] + 1] < l - i - 1: ans += "0" x = num0[x] + 1 continue ans += "1" x = num1[x] + 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR IF STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given a binary string $S$. Chef defines $\mathrm{MEX}(S)$ as the smallest non-negative integer such that its binary representation (without leading '0'-s; in particular, the binary representation of $0$ is "0") is not a [subsequence] of $S$. Chef is asking you to find $\mathrm{MEX}(S)$. Since this integer could be very large, find its binary representation (without leading '0'-s). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single string $S$. ------ Output ------ For each test case, print a single line containing one string: $\mathrm{MEX}(S)$ in binary representation. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 10^{6}$ $S$ contains only characters '0' and '1' the sum of $|S|$ over all test cases does not exceed $2 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): - $1 ≤ T ≤ 2 \cdot 10^{3}$ $|S| ≤ 10$ Subtask #2 (20 points): - $1 ≤ T ≤ 10^{5}$ $|S| ≤ 60$ Subtask #2 (60 points): - original constraints ----- Sample Input 1 ------ 2 1001011 1111 ----- Sample Output 1 ------ 1100 0 ----- explanation 1 ------ Example case 1: All integers between $0$ and $11$ inclusive, in binary representation, appear in $S$ as subsequences. However, the binary representation of $12$ (which is "1100") is not a subsequence of $S$. Example case 2: Since $S$ contains only '1'-s, the string "0" is not a subsequence of $S$ and therefore $\mathrm{MEX}(S) = 0$.
t = int(input()) while t > 0: s = input() t -= 1 n = len(s) if s.count("1") == n: print(0) elif s.count("0") == n: print(1) else: ind = 0 while s[ind] == "0": ind += 1 if s[-1] == "0": dp = [0] * (n + 1) zero = [0] * (n + 1) for i in range(n - 1, -1, -1): if s[i] == "0": zero[i] = zero[i + 1] + 1 else: zero[i] = zero[i + 1] n0_m1 = 2 ind_m1 = n n0_m2 = 2 ind_m2 = n - 1 n1 = 1 ind_n1 = -1 dp[n - 1] = -1 for i in range(n - 2, -1, -1): if s[i] == "0": dp[i] = ind_n1 if n1 < n0_m2: if n1 < n0_m1: n0_m2 = n0_m1 + 1 ind_m2 = ind_m1 n0_m1 = n1 + 1 ind_m1 = i else: n0_m1 = n0_m1 + 1 n0_m2 = n1 + 1 ind_m2 = i else: n0_m1 += 1 n0_m2 += 1 else: if n1 < n0_m1: dp[i] = ind_n1 n1 = n1 + 1 else: dp[i] = ind_m1 n1 = 1 + n0_m1 ind_n1 = i else: dp = [0] * (n + 1) zero = [0] * (n + 1) for i in range(n - 1, -1, -1): if s[i] == "0": zero[i] = zero[i + 1] + 1 else: zero[i] = zero[i + 1] n0_m1 = 1 ind_m1 = n n0_m2 = 999999999 ind_m2 = -1 n1 = 2 ind_n1 = n - 1 dp[n - 1] = n for i in range(n - 2, -1, -1): if s[i] == "0": dp[i] = ind_n1 if n1 < n0_m2: if n1 < n0_m1: n0_m2 = n0_m1 + 1 ind_m2 = ind_m1 n0_m1 = n1 + 1 ind_m1 = i else: n0_m1 = n0_m1 + 1 n0_m2 = n1 + 1 ind_m2 = i else: n0_m1 += 1 n0_m2 += 1 else: if n1 < n0_m1: dp[i] = ind_n1 n1 = n1 + 1 else: dp[i] = ind_m1 n1 = 1 + n0_m1 ind_n1 = i prev = 0 i = ind while i <= n: if i == -1: print("1", end="") break elif i == n: count_0 = zero[prev] for j in range(count_0): print("0", end="") print("0", end="") break elif s[i] == "0": count_0 = zero[prev] - zero[i] for j in range(count_0): print("0", end="") print("0", end="") prev = i i = dp[prev] else: print("1", end="") prev = i i = dp[prev] print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING STRING IF VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): conn = [(100) for times in range(0, 31)] for i in range(0, 2 * N, 2): conn[arr[i]] = arr[i + 1] visited = [(-1) for times in range(0, 31)] visited[1] = 0 q = [1] front, back = 0, 0 while front <= back: val = q[front] front += 1 for i in range(1, 7): if val + i <= 30: if visited[val + i] == -1 and conn[val + i] >= val + i: if conn[val + i] == 100: q.append(val + i) visited[val + i] = visited[val] + 1 back += 1 else: q.append(conn[val + i]) visited[val + i] = visited[val] + 1 visited[conn[val + i]] = visited[val + i] back += 1 return visited[30]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): a1 = {} a2 = {} for i in range(1, len(arr), 2): if arr[i] > arr[i - 1]: if arr[i] not in a1: a1[arr[i]] = arr[i - 1] else: a1[arr[i]] = min(a1[arr[i]], arr[i - 1]) else: a2[arr[i - 1]] = arr[i] d = [float("inf")] * 31 d[1] = 0 for i in range(2, 31): c = 1 e = float("inf") while i - c >= 1 and c <= 6: if i - c not in a2: e = min(e, d[i - c] + 1) c += 1 if i in a1: e = min(e, d[a1[i]]) d[i] = e return d[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): queue = [1] l = {} s = {} visited = set() visited.add(1) ans = 0 i = 0 while i < N: l[arr[i]] = arr[i + 1] i += 2 j = N while j <= 2 * N - 2: s[arr[j]] = arr[j + 1] j += 2 while len(queue) != 0: for _ in range(len(queue)): ele = queue.pop(0) for i in range(1, 7): temp = ele + i if temp == 30: return ans + 1 if temp not in visited: if temp in l: queue.append(l[temp]) visited.add(l[temp]) elif temp in s: queue.append(s[temp]) visited.add(s[temp]) else: queue.append(temp) visited.add(temp) ans += 1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): edges = dict() i = 0 while i < len(arr): if arr[i] in edges: ar = edges[arr[i]] ar.append(arr[i + 1]) else: edges[arr[i]] = [arr[i + 1]] i += 2 distance = [float("+inf")] * 30 distance[0] = 0 q = [1] visited = dict() while q: ver = q.pop(0) visited[ver] = True i = ver + 1 while i < ver + 6 + 1 and i < 31: if i not in visited: q.append(i) visited[i] = True distance[i - 1] = distance[ver - 1] + 1 if i in edges: for edge in edges[i]: if edge not in visited: q.append(edge) visited[edge] = True distance[edge - 1] = distance[i - 1] i += 1 return distance[29]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): pos = 1 moves = 0 vect = [] for i in range(0, N * 2, 2): vect.append([arr[i], arr[i + 1]]) vect.sort() for i in range(1, N): if ( vect[i - 1][1] > vect[i][0] and vect[i][0] < vect[i][1] and vect[i][1] > vect[i - 1][1] ): vect[i - 1][1] = vect[i - 1][0] - 1 while pos < 30: mi = pos + 6 pm = [1, 2, 3, 4, 5, 6] i = 0 while i < N: if vect[i][0] <= pos + 6 and vect[i][0] >= pos: if vect[i][0] < vect[i][1]: mi = max(mi, vect[i][1]) elif pos + 6 >= vect[i][0]: pm.remove(vect[i][0] - pos) if len(pm): if mi <= pos + 6: mi = pos + pm[-1] else: assert 0 i += 1 pos = mi moves += 1 return moves
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): d = {} for i in range(N): d[arr[2 * i]] = arr[2 * i + 1] vis = [(False) for i in range(31)] vis[1] = 1 q = [] q.append((1, 0)) while q: curr, cnt = q.pop(0) if curr == 30: return cnt for i in range(curr + 1, curr + 7): if i < 31 and not vis[i]: vis[i] = 1 new = i if d.get(i): new = d.get(i) q.append((new, cnt + 1)) return -1
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): visited = [0] * 30 hash = {} for i in range(0, len(arr) - 1, 2): hash[arr[i] - 1] = arr[i + 1] - 1 q = [(0, 0)] visited[0] = 1 while q: cur = q.pop(0) throw = cur[0] start = cur[1] if start == 29: return throw for i in range(6): new_pos = start + i + 1 if new_pos <= 29: if new_pos in hash: new_pos = hash[new_pos] if visited[new_pos] == 0: visited[new_pos] = 1 q.append((throw + 1, new_pos)) return -1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, n, a): l = [([0] * 6) for i in range(5)] def getcoard(x): r = x // 6 c = x % 6 if r % 2 != 0: c = 5 - c return r, c def formboard(): for i in range(0, 2 * n, 2): x, y = getcoard(a[i] - 1) l[x][y] = a[i + 1] formboard() v = set() queue = [(1, 0)] v.add(1) while queue: z = queue.pop(0) if z[0] == 30: return z[1] for j in range(z[0] + 1, z[0] + 7): if j in v: continue if j > 30: break x, y = getcoard(j - 1) if l[x][y] == 0: queue.append((j, z[1] + 1)) else: queue.append((l[x][y], z[1] + 1)) v.add(j)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR RETURN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): d = {} for i in range(0, N * 2, 2): d[arr[i]] = arr[i + 1] que = [[1, 0]] vis = [False] * 40 vis[0] = True vis[1] = True while que: ans, steps = que.pop(0) if ans == 30: return steps for i in range(1, 7): res = ans + i if vis[res] == False: vis[res] = True if res in d: que.append([d[res], steps + 1]) else: que.append([res, steps + 1]) return -1
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER RETURN NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
def add_edge(graph, x, y): graph[x].append(y) def single_source_shortest_path(graph, start): visited = {start: 0} q = [start] while len(q): node = q.pop(0) for n in graph[node]: if n not in visited: q.append(n) visited[n] = visited[node] + 1 return visited class Solution: def minThrow(self, N, arr): n = 30 graph = [[] for i in range(n + 1)] board = [(0) for i in range(n + 10)] for i in range(0, len(arr), 2): board[arr[i]] = arr[i + 1] - arr[i] for i in range(n + 1): for dice in range(1, 7): j = i + dice j += board[j] if j <= n: add_edge(graph, i, j) paths = single_source_shortest_path(graph, 1) return paths[n]
FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR DICT VAR NUMBER ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): dp = [float("inf")] * 31 dp[0], dp[1] = 0, 0 h = {} for i in range(0, 2 * N, 2): startPoint = arr[i] endPoint = arr[i + 1] h[startPoint] = endPoint for i in range(2, 31): if i in h: if h[i] < i: continue for j in range(1, 7): if i - j > 0 and dp[i - j] != float("inf"): dp[i] = min(dp[i], dp[i - j] + 1) if i in h: if h[i] > i: dp[h[i]] = min(dp[h[i]], dp[i]) return dp[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, n, arr, dp=None, i=0, poss=None): if dp == None: dp = [(-1) for i in range(30)] poss = [(-1) for i in range(29)] dp[29] = 0 for j in range(0, n): poss[arr[2 * j] - 1] = arr[2 * j + 1] - 1 if i > 29: return float("inf") if dp[i] != -1: return dp[i] if poss[i] == -1: minm = float("inf") for j in range(6): if i + j + 1 < 30: minm = min(1 + self.minThrow(n, arr, dp, i + j + 1, poss), minm) dp[i] = minm elif poss[i] < i: dp[i] = 31 else: dp[i] = self.minThrow(n, arr, dp, poss[i], poss) return dp[i]
CLASS_DEF FUNC_DEF NONE NUMBER NONE IF VAR NONE ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR NUMBER RETURN VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): visited = [(-1) for i in range(31)] snl = dict() for i in range(0, len(arr) - 1, 2): snl[arr[i]] = arr[i + 1] q = [(1, 0)] visited[1] = 1 while q: curr, diceRolled = q.pop(0) if curr == 30: return diceRolled for i in range(1, 7): newPos = curr + i if newPos > 30: break if newPos in snl: newPos = snl[newPos] if visited[newPos] == -1: q.append((newPos, diceRolled + 1)) visited[newPos] = 1 return -1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): mpl = [(0) for i in range(31)] for i in range(0, 2 * N, 2): mpl[arr[i]] = arr[i + 1] ans = c = 0 flag = 0 q = [] q.append(1) vis = [(0) for j in range(31)] vis[1] = 1 while c < 10: t = [] for i in range(len(q)): x = q[i] for j in range(1, 7): if vis[30] == 1: return ans + 1 if x + j < 30 and mpl[x + j] != 0 and vis[mpl[x + j]] == 0: t.append(mpl[x + j]) vis[mpl[x + j]] = 1 elif mpl[x + j] == 0 and vis[x + j] == 0: t.append(x + j) vis[x + j] = 1 if vis[30] == 1: return ans + 1 q = t ans += 1 c += 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): teleport = [i for i in range(31)] visited = [(False) for i in range(31)] visited[1] = True for i in range(N): teleport[arr[2 * i]] = arr[2 * i + 1] q = [1] ptr = 0 level = 0 while ptr < len(q): c = len(q) - ptr for _ in range(c): top = q[ptr] if top == 30: return level ptr += 1 for i in range(1, 7): if top + i <= 30 and not visited[teleport[top + i]]: q.append(teleport[top + i]) visited[teleport[top + i]] = True level += 1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER RETURN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, n, array): ladder, snakes, idx = {}, {}, 0 while idx < len(array): if array[idx] < array[idx + 1]: ladder[array[idx]] = array[idx + 1] else: snakes[array[idx]] = array[idx + 1] idx += 2 return self.breadthFirstSearch(snakes, ladder, 30) def breadthFirstSearch(self, snakes, ladder, target): queue, visited, minSteps = [(1, 0)], {}, float("inf") while queue: current, steps = queue.pop(0) if current == target: minSteps = min(minSteps, steps) continue visited[current] = True for change in (1, 2, 3, 4, 5, 6): new = current + change if new <= 30: if new in ladder: new = ladder[new] elif new in snakes: new = snakes[new] if new not in visited: queue.append((new, steps + 1)) visited[new] = True return minSteps
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR DICT DICT NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR LIST NUMBER NUMBER DICT FUNC_CALL VAR STRING WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): dic = {} for i in range(len(arr) // 2): dic[arr[2 * i]] = arr[2 * i + 1] a = [0] for i in range(1, 31): if i in dic: a.append(dic[i]) else: a.append(i) vis = [(0) for _ in range(32)] q = [1] cnt = 0 vis[1] = 1 res = 0 while len(q) > 0: if res != 0: return res tq = [] cnt += 1 for k in q: for jj in range(k + 1, k + 7): if jj > 30: continue j = a[jj] if vis[j] != 1: vis[j] = 1 tq.append(j) if j == 30: res = cnt break q = tq return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, n, arr): q = [] q.append([1, 0]) m = dict() for i in range(n): m[arr[2 * i]] = arr[2 * i + 1] while len(q) != 0: cur = q.pop(0) throws = cur[1] node = cur[0] if node == 30: return throws for i in range(1, 7): newnode = node + i if newnode <= 30: if newnode in m: q.append([m[newnode], throws + 1]) else: q.append([newnode, throws + 1]) return -1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER RETURN NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): tele = [i for i in range(31)] visited = [(False) for i in range(31)] visited[1] = True for i in range(0, 2 * N, 2): tele[arr[i]] = arr[i + 1] q = [1] dice = 0 p = 0 s = 1 while p < len(q): node = q[p] if node == 30: return dice p += 1 s -= 1 for i in range(1, 7): if node + i <= 30 and visited[tele[node + i]] == False: q.append(tele[node + i]) visited[tele[node + i]] = True if s == 0: dice += 1 s = len(q) - p return -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER RETURN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): g = [-1] * 31 for i in range(0, 2 * N, 2): g[arr[i]] = arr[i + 1] q = [] q.append(1) cnt = 0 vis = [(False) for i in range(31)] vis[1] = True while q: size = len(q) for i in range(size): front = q.pop(0) if front == 30: return cnt for dice in range(1, 7): if dice + front > 30: break pos = dice + front if vis[pos]: continue else: vis[pos] = True if g[pos] == -1: q.append(pos) else: q.append(g[pos]) cnt += 1 return -1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): d = {} for i in range(0, N * 2, 2): d[arr[i]] = arr[i + 1] st = [] st.append([1, 0]) while True: t = st.pop(0) for i in range(1, 7): if t[0] + i <= 30: if t[0] + i == 30: return t[1] + 1 if t[0] + i in d: st.append([d[t[0] + i], t[1] + 1]) else: st.append([t[0] + i, t[1] + 1])
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): vis = [0] * 31 d = [i for i in range(31)] for i in range(0, len(arr), 2): d[arr[i]] = arr[i + 1] q = [[1, 0]] while q: x, y = q.pop(0) if x == 30: return y for i in range(1, 7): if 0 <= x + i < 31 and vis[d[x + i]] == 0: vis[d[x + i]] = 1 q.append([d[x + i], y + 1]) return -1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): endCell = 30 moves = [(-1) for _ in range(endCell)] for start in range(0, 2 * N, 2): s = arr[start] e = arr[start + 1] moves[s - 1] = e - 1 queue = [(0, 0)] totalHop = 0 while len(queue): start, hop = queue.pop(0) if start == endCell - 1: totalHop = hop break v = start + 1 while v <= start + 6 and v < 30: newV = v if moves[v] == -1 else moves[v] queue.append((newV, hop + 1)) v += 1 return totalHop
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
SIZE = 5 * 6 class Solution: def BFS(self, adj, start): visited = set() visited.add(start) q = [] q.append((0, start)) while q: cost, curr = q.pop(0) if curr == SIZE - 1: return cost for v in adj[curr]: if v not in visited: visited.add(v) q.append((cost + 1, v)) return -1 def minThrow(self, N, arr): sn_ld = {} for i in range(0, 2 * N, 2): sn_ld[arr[i] - 1] = arr[i + 1] - 1 adj = [[] for i in range(SIZE)] for i in range(SIZE): for j in range(i + 1, i + 7): if j < SIZE: if j in sn_ld: adj[i].append(sn_ld[j]) else: adj[i].append(j) return self.BFS(adj, 0)
ASSIGN VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
from sys import maxsize, setrecursionlimit setrecursionlimit(10**9) class Solution: def minThrow(self, N, arr): from sys import maxsize ans = [maxsize] dp = {} vis = [(0) for i in range(31)] d = {} for i in range(N): sp, ep = arr[2 * i], arr[2 * i + 1] d[sp] = ep def f(cn): if vis[cn] == 1: return maxsize if cn in dp: return dp[cn] if cn == 30: return 0 if cn in d: temp = f(d[cn]) dp[cn] = temp return temp vis[cn] = 1 temp = maxsize for i in range(1, 7): nn = cn + i if nn > 30: continue temp = min(temp, f(nn) + 1) dp[cn] = temp vis[cn] = 0 return temp return f(1)
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): d_up = {} d_down = {} for i in range(0, len(arr), 2): if arr[i] < arr[i + 1]: d_up[arr[i]] = arr[i + 1] else: d_down[arr[i]] = arr[i + 1] count = self.shortestsum(d_up, d_down, arr, 1, 30) start = -1 end = -1 ma = 0 temp_count = 0 for i in d_up: if d_up[i] - i > ma: start = i end = d_up[i] ma = d_up[i] - i temp_count += self.shortestsum(d_up, d_down, arr, 1, start) temp_count += 1 temp_count += self.shortestsum(d_up, d_down, arr, end, 30) if temp_count < count: return temp_count return count def shortestsum(self, d_up, d_down, arr, current, end): count = 0 while current < end: ma = current for i in range(0, 6 + 1): temp = current + i if temp in d_up: if d_up[temp] > ma: ma = d_up[temp] elif temp not in d_down: if temp > ma: ma = temp current = ma count += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell). You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following. Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell. Example 1: Input: N = 8 arr[] = {3, 22, 5, 8, 11, 26, 20, 29, 17, 4, 19, 7, 27, 1, 21, 9} Output: 3 Explanation: The given board is the board shown in the figure. For the above board output will be 3. a) For 1st throw get a 2. b) For 2nd throw get a 6. c) For 3rd throw get a 2. Your Task: You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10 1 ≤ arr[i] ≤ 30
class Solution: def minThrow(self, N, arr): vis = [False] * 37 d = dict() i = 0 while i < 2 * N - 1: d[arr[i]] = arr[i + 1] i += 2 q = list() q.append((1, 0)) vis[1] = 1 while q: node = q[0][0] dist = q[0][1] q.pop(0) if node == 30: return dist for i in range(node + 1, node + 7): if not vis[i]: vis[i] = 1 if i in d.keys(): nNode = d[i] else: nNode = i q.append((nNode, dist + 1)) return -1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER