description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: M = len(mat) N = len(mat[0]) if K >= M - 1 and K >= N - 1: t = 0 for h in mat: for w in h: t += w rtv = [([t] * N) for _ in range(M)] return rtv def calc(presums, h, w, K): h1 = h - K h2 = h + K + 1 w1 = w - K w2 = w + K + 1 w1 = max(w1, 0) h1 = max(h1, 0) w2 = min(w2, N) h2 = min(h2, M) return presums[h1][w1] + presums[h2][w2] - presums[h1][w2] - presums[h2][w1] presums = [([0] * (N + 1)) for _ in range(M + 1)] for h in range(M): for w in range(N): presums[h + 1][w + 1] = ( presums[h + 1][w] + presums[h][w + 1] - presums[h][w] + mat[h][w] ) rtv = [([0] * N) for _ in range(M)] for h in range(M): for w in range(N): rtv[h][w] = calc(presums, h, w, K) return rtv
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: def getSum(i, j): ii = max(i - K, 0) s = 0 while ii <= min(i + K, len(mat) - 1): jj = max(j - K, 0) while jj <= min(j + K, len(mat[0]) - 1): s += mat[ii][jj] jj += 1 ii += 1 return s def editCol(i, j, s): if j - K - 1 >= 0: ii = max(i - K, 0) while ii <= min(i + K, len(mat) - 1): s -= mat[ii][j - K - 1] ii += 1 if j + K < len(mat[0]): ii = max(i - K, 0) while ii <= min(i + K, len(mat) - 1): s += mat[ii][j + K] ii += 1 return s rett = [] for i in range(len(mat)): s = getSum(i, 0) ret = [s] for j in range(1, len(mat[i])): s = editCol(i, j, s) ret.append(s) rett.append(ret) return rett
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
from itertools import accumulate as acc class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: N = len(mat) M = len(mat[0]) prefix = [*list(zip(*list(map(acc, list(zip(*list(map(acc, mat))))))))] for i in prefix: print(i) dp = [[(0) for _ in range(M)] for _ in range(N)] for i in range(N): for j in range(M): r = min(i + K, N - 1) b = min(j + K, M - 1) l = max(0, i - K) t = max(0, j - K) cursum = prefix[r][b] if l - 1 >= 0: cursum -= prefix[l - 1][b] if t - 1 >= 0: cursum -= prefix[r][t - 1] if l - 1 >= 0 and t - 1 >= 0: cursum += prefix[l - 1][t - 1] dp[i][j] = cursum return dp
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: if K == 0: return mat.copy() answers = [] dp = [] for row in mat: answers.append(row.copy()) dp.append(row.copy()) count = mat[0][0] for j in range(1, len(mat[0])): count += mat[0][j] dp[0][j] = count for i in range(1, len(mat)): count = 0 for j in range(len(mat[0])): count += mat[i][j] dp[i][j] = count + dp[i - 1][j] for i in range(len(mat)): for j in range(len(mat[0])): x_outer = i + K if i + K < len(mat) - 1 else len(mat) - 1 y_outer = j + K if j + K < len(mat[0]) - 1 else len(mat[0]) - 1 total = dp[x_outer][y_outer] if i - K > 0 and j - K > 0: total += dp[i - K - 1][j - K - 1] if j - K > 0: total -= dp[x_outer][j - K - 1] if i - K > 0: total -= dp[i - K - 1][y_outer] print((i, j, [i - K - 1, j - K - 1], [x_outer, y_outer], total)) answers[i][j] = total print(dp) return answers
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR LIST BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER LIST VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: if not mat or not mat[0]: return mat answer = [[(0) for _ in mat[0]] for _ in mat] for i in range(len(mat)): for j in range(1, len(mat[0])): mat[i][j] += mat[i][j - 1] for i in range(len(mat)): for j in range(len(mat[0])): answer[i][j] = mat[i][min(j + K, len(mat[i]) - 1)] if j - K > 0: answer[i][j] -= mat[i][j - K - 1] mat, answer = answer, [[(0) for _ in mat[0]] for _ in mat] for i in range(len(mat)): for j in range(len(mat[0])): answer[i][j] = sum( [mat[k][j] for k in range(max(0, i - K), min(len(mat), i + K + 1))] ) return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) res = [([0] * n) for _ in range(m)] for i in range(m): hori_slid_win = [0] * (K * 2 + 1) for jj in range(0, K + 1): if jj >= 0 and jj < n: hori_slid_win[jj + K] = sum( [ mat[r][jj] for r in range(i - K, i + K + 1) if r >= 0 and r < m ] ) for j in range(n): res[i][j] = sum(hori_slid_win) hori_slid_win.pop(0) if j + K + 1 < n: hori_slid_win.append( sum( [ mat[r][j + K + 1] for r in range(i - K, i + K + 1) if r >= 0 and r < m ] ) ) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: k = K def convolve(values): acc = sum(values[:k]) result = [] for i in range(len(values)): if i + k < len(values): acc += values[i + k] if i - k - 1 >= 0: acc -= values[i - k - 1] result.append(acc) return result def transpose(matrix): m = len(matrix) n = len(matrix[0]) transposed = [] for j in range(n): col = [matrix[i][j] for i in range(m)] row = col transposed.append(row) return transposed convolved_rows = [convolve(row) for row in mat] tranposed = transpose(convolved_rows) convolved = [convolve(row) for row in tranposed] return transpose(convolved)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: h, w = len(mat), len(mat[0]) dp = [[(0) for i in range(w)] for j in range(h)] for y in range(h): for x in range(w): sum = 0 for i in range( x - K if x - K >= 0 else 0, x + K + 1 if x + K + 1 <= w else w ): sum += mat[y][i] dp[y][x] = sum res = [[(0) for i in range(w)] for j in range(h)] for y in range(h): for x in range(w): sum = 0 for j in range( y - K if y - K >= 0 else 0, y + K + 1 if y + K + 1 <= h else h ): sum += dp[j][x] res[y][x] = sum return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: prev = 0 answer = [] for row in range(len(mat)): answer.append([]) for col in range(len(mat[0])): l = max(0, col - k) r = min(col + k + 1, len(mat[0])) t = max(0, row - k) b = min(row + k + 1, len(mat)) submat = [mat[i][l:r] for i in range(t, b)] total = 0 for one in submat: for two in one: total += two answer[-1].append(total) return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: def sumMatrix(center: tuple): Sum = 0 start_i = max(center[0] - K, 0) stop_i = min(center[0] + K + 1, len(mat[0])) start_j = max(center[1] - K, 0) stop_j = min(center[1] + K + 1, len(mat)) for j in range(start_j, stop_j): Sum += sum(mat[j][start_i:stop_i]) print( "start_i", start_i, "stop_i", stop_i, "start_j", start_j, "stop_j", stop_j, ) print("sum", Sum) return Sum res = [] for i in range(len(mat)): res.append([]) for j in range(len(mat[i])): res[i].append(sumMatrix((j, i))) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR STRING VAR STRING VAR EXPR FUNC_CALL VAR STRING VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) cum_sum = [[(0) for x in range(n + 1)] for y in range(m + 1)] for i in range(1, m + 1): for j in range(1, n + 1): if i == 0 and j == 0: cum_sum[i][j] = mat[i - 1][j - 1] elif i == 0: cum_sum[i][j] = mat[i - 1][j - 1] + cum_sum[i][j - 1] elif j == 0: cum_sum[i][j] = mat[i - 1][j - 1] + cum_sum[i - 1][j] else: cum_sum[i][j] = ( mat[i - 1][j - 1] + cum_sum[i - 1][j] + cum_sum[i][j - 1] - cum_sum[i - 1][j - 1] ) ans = [[(0) for x in range(n)] for y in range(m)] for i in range(m): for j in range(n): maxr = min(i + K, m - 1) + 1 maxc = min(j + K, n - 1) + 1 minr = max(i - K - 1, -1) + 1 minc = max(j - K - 1, -1) + 1 ans[i][j] = ( cum_sum[maxr][maxc] - cum_sum[maxr][minc] - cum_sum[minr][maxc] + cum_sum[minr][minc] ) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: n, m = len(mat), len(mat[0]) k = K range_sum = [([0] * m) for _ in range(n)] for i in range(n): for j in range(m): top_left = range_sum[i - 1][j - 1] if j - 1 >= 0 and i - 1 >= 0 else 0 top = range_sum[i - 1][j] if i - 1 >= 0 else 0 left = range_sum[i][j - 1] if j - 1 >= 0 else 0 range_sum[i][j] = top + left - top_left + mat[i][j] print(range_sum) dp = [([0] * m) for _ in range(n)] for i in range(n): for j in range(m): col = min(m - 1, j + k) row = min(n - 1, i + k) print([row, col]) dp[i][j] = ( range_sum[row][col] + ( range_sum[i - k - 1][j - k - 1] if i - k - 1 >= 0 and j - k - 1 >= 0 else 0 ) - (range_sum[row][j - k - 1] if j - k - 1 >= 0 else 0) - (range_sum[i - k - 1][col] if i - k - 1 >= 0 else 0) ) return dp
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: n = len(mat) m = len(mat[0]) answer = [([0] * m) for i in range(n)] maxo = max sumo = sum for i in range(n): for j in range(m): r = maxo(0, i - K) sum1 = 0 while r <= i + K and r < n: if j - K < 0: if j + K < m: sum1 += sumo(mat[r][: j + K + 1]) else: sum1 += sumo(mat[r]) elif j + K < m: sum1 += sumo(mat[r][j - K : j + K + 1]) else: sum1 += sumo(mat[r][j - K :]) r += 1 answer[i][j] = sum1 return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def sumHelper( self, mat: List[List[int]], rowLow: int, rowHigh: int, colLow: int, colHigh: int ) -> int: res = sum(sum(row[colLow : colHigh + 1]) for row in mat[rowLow : rowHigh + 1]) return res def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: rows = len(mat) cols = len(mat[0]) res = [[(0) for c in range(cols)] for r in range(rows)] for r in range(rows): for c in range(cols): lowR = r - K if lowR < 0: lowR = 0 highR = r + K if highR >= rows: highR = rows - 1 lowC = c - K if lowC < 0: lowC = 0 highC = c + K if highC >= cols: highC = cols - 1 res[r][c] = self.sumHelper(mat, lowR, highR, lowC, highC) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: ans = [] for i in range(0, len(mat)): ans.append([]) for j in range(0, len(mat[0])): rL = max(0, i - K) rR = min(len(mat) - 1, i + K) cL = max(0, j - K) cR = min(len(mat[0]) - 1, j + K) posVal = 0 for rVal in range(rL, rR + 1): for cVal in range(cL, cR + 1): posVal += mat[rVal][cVal] ans[i].append(posVal) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: list, K: int): def calc_surround(r, c): nonlocal mat, K total = 0 for i in range(r - K, r + K + 1): for j in range(c - K, c + K + 1): if 0 <= i < len(mat) and 0 <= j < len(mat[0]): total += mat[i][j] return total def calc_left_column(r, c): nonlocal mat, K total = 0 for i in range(r - K, r + K + 1): if 0 <= i < len(mat) and 0 <= c - K - 1 < len(mat[0]): total += mat[i][c - K - 1] return total def calc_right_column(r, c): nonlocal mat, K total = 0 for i in range(r - K, r + K + 1): if 0 <= i < len(mat) and 0 <= c + K < len(mat[0]): total += mat[i][c + K] return total grid = [([0] * len(mat[0])) for _ in range(len(mat))] for i in range(len(mat)): cache = 0 for j in range(len(mat[0])): if j == 0: cache = calc_surround(i, j) else: left_c = calc_left_column(i, j) right_c = calc_right_column(i, j) cache = cache - left_c + right_c grid[i][j] = cache return grid
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: acc = [([0] + list(accumulate(m))) for m in mat] print(acc) m, n = len(mat), len(mat[0]) ans = [([0] * n) for _ in range(m)] for i in range(m): for j in range(n): for k in range(max(i - K, 0), min(i + K, m - 1) + 1): l, r = max(j - K, 0), min(j + K, n - 1) ans[i][j] += acc[k][r + 1] - acc[k][l] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) dp = [] for i in range(m + 2 * K): row = [] for j in range(n + 2 * K): row.append(0) dp.append(row) for i in range(m): for j in range(n): dp[K + i][K + j] = mat[i][j] for i in range(m): for j in range(n): temp = 0 for i1 in range(-K, K + 1): temp += sum(dp[i1 + K + i][j : j + 2 * K + 1]) mat[i][j] = temp return mat
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: def calc(mat, area): (a, b), (c, d) = area return sum([sum(row[b : d + 1]) for row in mat[a : c + 1]]) m = len(mat) - 1 n = len(mat[0]) - 1 row = [] lookup = {} for i, r in enumerate(mat): col = [] for j, c in enumerate(mat[0]): area = (0 if i - K < 0 else i - K, 0 if j - K < 0 else j - K), ( m if i + K > m else i + K, n if j + K > n else j + K, ) key = str(area) if key not in lookup: lookup[key] = calc(mat, area) col += [lookup[key]] row += [col] return row
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR LIST VAR VAR VAR LIST VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: k = K n, m = len(mat), len(mat[0]) matrix = [] for i in range(n): temp = [] for j in range(m): temp.append(0) matrix.append(temp) for i in range(n): for j in range(m): if i == 0 and j == 0: summ = 0 for o in range(min(n, k + 1)): for l in range(min(m, k + 1)): summ += mat[o][l] matrix[i][j] = summ elif j == 0: summ = 0 remSumm = 0 for l in range(min(m, k + 1)): if i + k <= n - 1: summ += mat[i + k][l] if i - k - 1 >= 0: remSumm += mat[i - k - 1][l] matrix[i][j] = matrix[i - 1][j] + summ - remSumm else: summ = 0 remSumm = 0 for l in range(max(0, i - k), min(n, i + k + 1)): if j + k <= m - 1: summ += mat[l][j + k] if j - k - 1 >= 0: remSumm += mat[l][j - k - 1] matrix[i][j] = matrix[i][j - 1] + summ - remSumm return matrix
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: row = len(mat) col = len(mat[0]) answer = [[(0) for i in range(col)] for j in range(row)] for i in range(row): for j in range(1, col): mat[i][j] = mat[i][j - 1] + mat[i][j] print(mat) for i in range(row): for j in range(col): answer[i][j] = self.new_matrix(mat, i, j, K, row, col) return answer def new_matrix(self, mat, i, j, k, row, col): ans = 0 new_row_low = i - k new_row_high = i + k new_col_low = j - k new_col_high = j + k while new_row_low < 0: new_row_low += 1 while new_row_high > row - 1: new_row_high -= 1 while new_col_low < 0: new_col_low += 1 while new_col_high > col - 1: new_col_high -= 1 for a in range(new_row_low, new_row_high + 1): ans += mat[a][new_col_high] if new_col_low - 1 >= 0: ans -= mat[a][new_col_low - 1] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def distk(self, k, rowsum, colsum, i, j): s = 0 m = len(rowsum) n = len(rowsum[0]) if i - k >= 0: l = max(-1, j - k - 1) h = min(n - 1, j + k) s += rowsum[i - k][h] if l > -1: s -= rowsum[i - k][l] if i + k < m: l = max(-1, j - k - 1) h = min(n - 1, j + k) s += rowsum[i + k][h] if l > -1: s -= rowsum[i + k][l] if j - k >= 0: l = max(-1, i - k) h = min(m - 1, i + k - 1) s += colsum[h][j - k] if l > -1: s -= colsum[l][j - k] if j + k < n: l = max(-1, i - k) h = min(m - 1, i + k - 1) s += colsum[h][j + k] if l > -1: s -= colsum[l][j + k] return s def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) if K <= 0: return mat rowsum = [[(0) for i in range(n)] for j in range(m)] colsum = [[(0) for i in range(n)] for j in range(m)] for i in range(m): rowsum[i][0] = mat[i][0] for j in range(1, n): rowsum[i][j] += rowsum[i][j - 1] + mat[i][j] for i in range(n): colsum[0][i] = mat[0][i] for j in range(1, m): colsum[j][i] += colsum[j - 1][i] + mat[j][i] ans = [[mat[j][i] for i in range(n)] for j in range(m)] for k in range(1, K + 1): for i in range(m): for j in range(n): x = self.distk(k, rowsum, colsum, i, j) ans[i][j] += x return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: rows, cols = len(mat), len(mat[0]) dic, seen = {}, {} for i, row in enumerate(mat): prefix_sum = [0] * (cols + 1) for j, num in enumerate(row): prefix_sum[j] = prefix_sum[j - 1] + num dic[i] = prefix_sum for i in range(rows): for j in range(cols): start_col = j - K if j - K >= 0 else 0 end_col = j + K if j + K < cols else cols - 1 start_row = i - K if i - K >= 0 else 0 end_row = i + K if i + K < rows else rows - 1 if (start_col, end_col, start_row, end_row) in seen: mat[i][j] = seen[start_col, end_col, start_row, end_row] continue res = 0 for idx in range(start_row, end_row + 1): res += dic[idx][end_col] - dic[idx][start_col - 1] mat[i][j] = res seen[start_col, end_col, start_row, end_row] = res return mat
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR DICT DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) RangeSum = [[(0) for _ in range(n + 1)] for _ in range(m + 1)] for i in range(1, m + 1): for j in range(1, n + 1): RangeSum[i][j] = ( RangeSum[i - 1][j] + RangeSum[i][j - 1] - RangeSum[i - 1][j - 1] + mat[i - 1][j - 1] ) ans = [[(0) for _ in range(n)] for _ in range(m)] print(RangeSum) for r in range(m): for c in range(n): i2 = min(m - 1, r + K) j2 = min(n - 1, c + K) i1 = max(0, r - K) j1 = max(0, c - K) ans[r][c] = ( RangeSum[i2 + 1][j2 + 1] - RangeSum[i2 + 1][j1] - RangeSum[i1][j2 + 1] + RangeSum[i1][j1] ) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: rows, cols = len(mat), len(mat[0]) ans = [[(0) for _ in range(cols)] for _ in range(rows)] for i in range(rows): for j in range(1, cols): mat[i][j] += mat[i][j - 1] if i > 0: mat[i][j - 1] += mat[i - 1][j - 1] if i > 0: mat[i][cols - 1] += mat[i - 1][cols - 1] for i in range(rows): for j in range(cols): top, bottom, left, right = i, i, j, j countT = k while top > 0 and countT > 0: top -= 1 countT -= 1 countB = k while bottom < rows - 1 and countB > 0: bottom += 1 countB -= 1 countL = k while left > 0 and countL > 0: left -= 1 countL -= 1 countR = k while right < cols - 1 and countR > 0: right += 1 countR -= 1 ans[i][j] += mat[bottom][right] if top - 1 >= 0 and countT == 0: ans[i][j] -= mat[top - 1][right] if left - 1 >= 0 and countL == 0: ans[i][j] -= mat[bottom][left - 1] if top - 1 >= 0 and left - 1 >= 0 and countT == 0 and countL == 0: ans[i][j] += mat[top - 1][left - 1] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: if not mat or not mat[0]: return [] height = len(mat) width = len(mat[0]) prefix_sum = [([0] * width) for _ in range(height)] for i in range(height): for j in range(width): up = prefix_sum[i - 1][j] if i > 0 else 0 left = prefix_sum[i][j - 1] if j > 0 else 0 diag = prefix_sum[i - 1][j - 1] if i > 0 and j > 0 else 0 prefix_sum[i][j] = up + left - diag + mat[i][j] answer = [([0] * width) for _ in range(height)] for i in range(height): for j in range(width): lri = min(height - 1, i + K) lrj = min(width - 1, j + K) uli = i - K - 1 ulj = j - K - 1 total = prefix_sum[lri][lrj] up = prefix_sum[uli][lrj] if uli >= 0 else 0 left = prefix_sum[lri][ulj] if ulj >= 0 else 0 diag = prefix_sum[uli][ulj] if uli >= 0 and ulj >= 0 else 0 answer[i][j] = total - up - left + diag return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) p = [] for i in range(m): row = [0] s = 0 for j in range(n): s += mat[i][j] row.append(s) p.append(row) ans = [] for i in range(m): row = [] for j in range(n): s = 0 top = max(0, i - k) bottom = min(m, i + k + 1) for g in range(top, bottom): left = max(0, j - k) right = min(n, j + k + 1) s += p[g][right] - p[g][left] row.append(s) ans.append(row) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: res = [[(0) for j in range(len(mat[0]))] for i in range(len(mat))] for i in range(0, min(len(mat), K + 1)): for j in range(0, min(len(mat[0]), K + 1)): res[0][0] += mat[i][j] for i in range(0, len(mat)): for j in range(0, len(mat[0])): if i == 0 and j == 0: continue if j == 0: res[i][j] = res[i - 1][j] old_i = i - K - 1 if old_i >= 0: for curr_j in range(max(0, j - K), min(len(mat[0]), j + K + 1)): res[i][j] -= mat[old_i][curr_j] new_i = i + K if new_i < len(mat): for curr_j in range(max(0, j - K), min(len(mat[0]), j + K + 1)): res[i][j] += mat[new_i][curr_j] else: res[i][j] = res[i][j - 1] old_j = j - K - 1 if old_j >= 0: for curr_i in range(max(0, i - K), min(len(mat), i + K + 1)): res[i][j] -= mat[curr_i][old_j] new_j = j + K if new_j < len(mat[0]): for curr_i in range(max(0, i - K), min(len(mat), i + K + 1)): res[i][j] += mat[curr_i][new_j] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def suma(self, mat, m, n, red, stupac, K): s = 0 poc_red = max(red - K, 0) kraj_red = min(red + K + 1, m) poc_stupac = max(stupac - K, 0) kraj_stupac = min(stupac + K + 1, n) for i in range(poc_red, kraj_red): for j in range(poc_stupac, kraj_stupac): s += mat[i][j] return s def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) res = [[(0) for j in range(n)] for i in range(m)] for i in range(m): for j in range(n): res[i][j] = self.suma(mat, m, n, i, j, K) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: res = [] for i in range(len(mat)): row = [] for j in range(len(mat[0])): up = max(i - K, 0) bottom = min(i + K, len(mat) - 1) left = max(j - K, 0) right = min(j + K, len(mat[0]) - 1) sum = 0 for x in range(up, bottom + 1): for y in range(left, right + 1): sum += mat[x][y] row.append(sum) res.append(row) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) def get_sum(i, j): ssum = 0 for a in range(-K, K + 1): for b in range(-K, K + 1): ssum += ( mat[i + a][j + b] if 0 <= i + a < m and 0 <= j + b < n else 0 ) return ssum res = [[(0) for _ in range(n)] for _ in range(m)] for i in range(m): for j in range(n): if i == 0 and j == 0: res[i][j] = get_sum(0, 0) elif i == 0: res[i][j] = ( res[i][j - 1] - sum( [ ( mat[b][j - K - 1] if 0 <= j - K - 1 < n and 0 <= b < m else 0 ) for b in range(i - K, i + K + 1) ] ) + sum( [ (mat[b][j + K] if 0 <= j + K < n and 0 <= b < m else 0) for b in range(i - K, i + K + 1) ] ) ) else: res[i][j] = ( res[i - 1][j] - sum( [ ( mat[i - K - 1][b] if 0 <= i - K - 1 < m and 0 <= b < n else 0 ) for b in range(j - K, j + K + 1) ] ) + sum( [ (mat[i + K][b] if 0 <= i + K < m and 0 <= b < n else 0) for b in range(j - K, j + K + 1) ] ) ) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR 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 FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: n = len(mat) m = len(mat[0]) matx = [] for i in range(n): row = [] for j in range(m): summ = 0 for y in range(max(0, i - K), min(n, i + K + 1)): summ = summ + sum(mat[y][max(0, j - K) : min(m, j + K + 1)]) row.append(summ) print(row) matx.append(row) return matx
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: answer = [[(0) for j in i] for i in mat] def calc(mat, a, b, c, d): return sum([sum(row[b : d + 1]) for row in mat[a : c + 1]]) for i in range(len(mat)): for j in range(len(mat[0])): a = i - K if i - K >= 0 else 0 b = j - K if j - K >= 0 else 0 c = i + K if i + K <= len(mat) else len(mat) d = j + K if j + K <= len(mat[0]) else len(mat[0]) answer[i][j] = calc(mat, a, b, c, d) return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) f = [([0] * n) for _ in range(m)] for i in range(m): for j in range(n): if i == 0 and j == 0: for i_ in range(i, i + K + 1): for j_ in range(j, j + K + 1): if i_ < m and j_ < n: f[i][j] += mat[i_][j_] elif j == 0: f[i][j] = f[i - 1][j] if i - 1 - K >= 0: for j_ in range(j, j + K + 1): if j_ < n: f[i][j] -= mat[i - 1 - K][j_] if i + K < m: for j_ in range(j, j + K + 1): if j_ < n: f[i][j] += mat[i + K][j_] else: f[i][j] = f[i][j - 1] if j - 1 - K >= 0: for i_ in range(i - K, i + K + 1): if 0 <= i_ < m: f[i][j] -= mat[i_][j - 1 - K] if j + K < n: for i_ in range(i - K, i + K + 1): if 0 <= i_ < m: f[i][j] += mat[i_][j + K] return f
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat, K): n = len(mat) m = len(mat[0]) arr = [([0] * m) for i in range(n)] for i in range(n): for j in range(1, m): mat[i][j] += mat[i][j - 1] for j in range(m): for i in range(1, n): mat[i][j] += mat[i - 1][j] for i in range(n): for j in range(m): min_row, max_row = max(0, i - K), min(n - 1, i + K) min_col, max_col = max(0, j - K), min(m - 1, j + K) arr[i][j] = mat[max_row][max_col] if min_row > 0: arr[i][j] -= mat[min_row - 1][max_col] if min_col > 0: arr[i][j] -= mat[max_row][min_col - 1] if min_col > 0 and min_row > 0: arr[i][j] += mat[min_row - 1][min_col - 1] return arr
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat[0]), len(mat) dp = [([0] * (m + 1)) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): dp[i][j] = ( dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + mat[i - 1][j - 1] ) for i in range(n): for j in range(m): l, r = max(0, i - K), max(0, j - K) p, q = min(n, i + K + 1), min(m, j + K + 1) mat[i][j] = dp[p][q] - dp[p][r] - dp[l][q] + dp[l][r] return mat
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): def succ(root, suc): while root != None: if root.key <= key: root = root.right else: suc[0] = root root = root.left def pres(root, pre): while root != None: if root.key >= key: root = root.left else: pre[0] = root root = root.right succ(root, suc) pres(root, pre)
FUNC_DEF FUNC_DEF WHILE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR FUNC_DEF WHILE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): while root: if root.key == key: if root.left: temp = root.left while temp and temp.right: temp = temp.right pre[0] = temp if root.right: temp = root.right while temp and temp.left: temp = temp.left suc[0] = temp return if root.key < key: pre[0] = root root = root.right else: suc[0] = root root = root.left
FUNC_DEF WHILE VAR IF VAR VAR IF VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR IF VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def solve(root, mini, maxa, key): if root == None: return if root.key < key and root.key > mini[0]: mini[0] = root.key if root.key > key and root.key < maxa[0]: maxa[0] = root.key solve(root.left, mini, maxa, key) solve(root.right, mini, maxa, key) def findPreSuc(root, pre, suc, key): mini = [-1000000] maxa = [1000000] solve(root, mini, maxa, key) if mini[0] != -1000000: pre[0] = Node(mini[0]) if maxa[0] != 1000000: suc[0] = Node(maxa[0]) return pre, suc
FUNC_DEF IF VAR NONE RETURN IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def get_max(root): temp = root while temp.right: temp = temp.right return temp def get_min(root): temp = root while temp.left: temp = temp.left return temp def findPreSuc(root, pre, suc, key): temp = root while temp: if temp.key < key: if pre[0] is None or pre[0].key < temp.key: pre[0] = temp temp = temp.right elif temp.key > key: if suc[0] is None or suc[0].key > temp.key: suc[0] = temp temp = temp.left else: if temp.right is None and temp.left is not None: pre[0] = get_max(temp.left) return if temp.right is not None and temp.left is None: suc[0] = get_min(temp.right) return if temp.left and temp.right: pre[0] = get_max(temp.left) suc[0] = get_min(temp.right) return if temp.left is None and temp.right is None: return
FUNC_DEF ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR IF VAR VAR IF VAR NUMBER NONE VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR IF VAR NUMBER NONE VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR IF VAR NONE VAR NONE ASSIGN VAR NUMBER FUNC_CALL VAR VAR RETURN IF VAR NONE VAR NONE ASSIGN VAR NUMBER FUNC_CALL VAR VAR RETURN IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR RETURN IF VAR NONE VAR NONE RETURN
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def inorder(root, ans): if root == None: return inorder(root.left, ans) ans.append(root) inorder(root.right, ans) def findPreSuc(root, pre, suc, key): ans = [] inorder(root, ans) for i in range(len(ans)): if ans[i].key > key: suc[0] = ans[i] if i != 0: if ans[i - 1].key != key: pre[0] = ans[i - 1] elif i > 1: pre[0] = ans[i - 2] return if ans[-1].key != key: pre[0] = ans[-1] else: pre[0] = ans[-2]
FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER RETURN IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): if root == None: return findPreSuc(root.left, pre, suc, key) if root.key < key: pre[0] = root if root.key > key: if suc[0] == None or root.key < suc[0].key: suc[0] = root findPreSuc(root.right, pre, suc, key)
FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR IF VAR VAR IF VAR NUMBER NONE VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): temp = root while temp: if key < temp.key: suc[0] = temp temp = temp.left elif key > temp.key: pre[0] = temp temp = temp.right else: if temp.left: pre[0] = temp.left while pre[0].right: pre[0] = pre[0].right if temp.right: suc[0] = temp.right while suc[0].left: suc[0] = suc[0].left break
FUNC_DEF ASSIGN VAR VAR WHILE VAR IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR IF VAR ASSIGN VAR NUMBER VAR WHILE VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR WHILE VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def inorder(root): l = [] cur = root q = [] while True: if cur is not None: q.append(cur) cur = cur.left elif len(q): cur = q.pop() l.append(cur) cur = cur.right else: break return l def findPreSuc(root, pre, suc, data): kl = inorder(root) pre[0] = None suc[0] = None for i in range(len(kl)): if kl[i].key == data: if i - 1 >= 0: pre[0] = kl[i - 1] if i + 1 < len(kl): suc[0] = kl[i + 1] return pre, suc elif kl[i].key > data: if i - 1 >= 0: pre[0] = kl[i - 1] suc[0] = kl[i] return pre, suc if kl[len(kl) - 1].key < data: pre[0] = kl[len(kl) - 1] suc[0] = None return pre, suc
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR LIST WHILE NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NONE ASSIGN VAR NUMBER NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR RETURN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NONE RETURN VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): if not root: return if key < root.key: suc[0] = root findPreSuc(root.left, pre, suc, key) elif key > root.key: pre[0] = root findPreSuc(root.right, pre, suc, key) else: findPreSuc(root.left, pre, suc, key) findPreSuc(root.right, pre, suc, key) return
FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): def successor(root, pre, suc, key): while root: if key >= root.key: root = root.right else: suc[0] = root root = root.left return suc def predecessor(root, pre, suc, key): while root: if key > root.key: pre[0] = root root = root.right else: root = root.left return pre suc = successor(root, pre, suc, key) pre = predecessor(root, pre, suc, key) return pre, suc
FUNC_DEF FUNC_DEF WHILE VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF WHILE VAR IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def predecessor(root, pre, key): if root is None: return if root.key >= key: predecessor(root.left, pre, key) else: if pre[0] is None: pre[0] = root else: pre[0] = max((pre[0], root), key=lambda x: x.key) predecessor(root.right, pre, key) def successor(root, suc, val): if root is None: return if root.key <= val: successor(root.right, suc, val) else: if suc[0] is None: suc[0] = root else: suc[0] = min((suc[0], root), key=lambda x: x.key) successor(root.left, suc, val) def findPreSuc(root, pre, suc, key): pre[0], suc[0] = None, None predecessor(root, pre, key) successor(root, suc, key)
FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER NONE NONE EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def inorder(root): if not root: return [] d = [] d += inorder(root.left) d.append(root.key) d += inorder(root.right) return d inorder(root.right, pre, suc, key) def findPreSuc(root, pre, suc, key): d = inorder(root) for i in d: if i < key: pre[0] = Node(i) if i > key: suc[0] = Node(i) break
FUNC_DEF IF VAR RETURN LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): curr = root next = root while curr is not None: if curr.key <= key: curr = curr.right else: suc[0] = curr curr = curr.left while root is not None: if root.key >= key: root = root.left else: pre[0] = root root = root.right return
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR WHILE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR RETURN
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def floor(node, key): res = None while node: if node.key >= key: node = node.left else: res = node node = node.right return res def ceil(node, key): res = None while node: if node.key > key: res = node node = node.left else: node = node.right return res def findPreSuc(root, pre, suc, key): pre[0] = floor(root, key) suc[0] = ceil(root, key)
FUNC_DEF ASSIGN VAR NONE WHILE VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE WHILE VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def inorder(root): if root is None: return [] return inorder(root.left) + [root] + inorder(root.right) def findPreSuc(root, pre, suc, key): inord = inorder(root) n = len(inord) for i in range(n): if inord[i].key == key: pre[0] = inord[i - 1] if i != 0 else pre[0] suc[0] = inord[i + 1] if i != n - 1 else suc[0] break elif inord[i].key > key: pre[0] = inord[i - 1] if i != 0 else pre[0] suc[0] = inord[i] break if pre[0] is None and suc[0] is None: pre[0] = inord[-1] def findPreSuc(root, pre, suc, key): while root: if root.key == key: if root.left: temp = root.left while temp and temp.right: temp = temp.right pre[0] = temp if root.right: temp = root.right while temp and temp.left: temp = temp.left suc[0] = temp return if root.key < key: pre[0] = root root = root.right else: suc[0] = root root = root.left
FUNC_DEF IF VAR NONE RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER NONE VAR NUMBER NONE ASSIGN VAR NUMBER VAR NUMBER FUNC_DEF WHILE VAR IF VAR VAR IF VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR IF VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def store(root, inorder): if root is None: return store(root.left, inorder) inorder.append(root.key) store(root.right, inorder) def findPreSuc(root, pre, suc, key): arr = [] info = {} store(root, arr) if key not in arr: arr.append(key) arr = sorted(arr) for itr in range(0, len(arr)): info[arr[itr]] = itr idx = info[key] if idx - 1 >= 0: pre[0] = Node(arr[idx - 1]) if idx + 1 < len(arr): suc[0] = Node(arr[idx + 1])
FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): if root is None: return elif root.key == key: if root.left: pre[0] = root.left findPreSuc(root.left.right, pre, suc, key) if root.right: suc[0] = root.right findPreSuc(root.right.left, pre, suc, key) elif root.key < key: pre[0] = root findPreSuc(root.right, pre, suc, key) elif root.key > key: suc[0] = root findPreSuc(root.left, pre, suc, key)
FUNC_DEF IF VAR NONE RETURN IF VAR VAR IF VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): prev = root succ = root node = root def find_biggest(node): rv = node while node is not None: rv = node node = node.right return rv def find_smallest(node): rv = node while node is not None: rv = node node = node.left return rv while node is not None: if key == node.key: if node.left: prev = find_biggest(node.left) if node.right: succ = find_smallest(node.right) break elif key < node.key: succ = node node = node.left elif key > node.key: prev = node node = node.right pre[0] = prev suc[0] = succ if pre[0] is not None and pre[0].key >= key: pre[0] = Node(-1) if pre[0] is not None and suc[0].key <= key: suc[0] = Node(-1)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR WHILE VAR NONE IF VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER NONE VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER IF VAR NUMBER NONE VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): if root is None: return findPreSuc(root.left, pre, suc, key) findPreSuc(root.right, pre, suc, key) if root.key > key and (suc[0] is None or suc[0].key > root.key): suc[0] = root if root.key < key and (pre[0] is None or pre[0].key < root.key): pre[0] = root def findNode(root, key): if root is None: return if root.key > key: return findNode(root.left, key) elif root.key < key: return findNode(root.right, key) else: return root
FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER NONE VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR VAR VAR NUMBER NONE VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): def helper1(root): if not root: return if root.key < key: pre[0] = root helper1(root.right) else: helper1(root.left) def helper2(root): if not root: return if root.key > key: suc[0] = root helper2(root.left) else: helper2(root.right) helper1(root) helper2(root) return pre[0], suc[0]
FUNC_DEF FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR NUMBER
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): if not root: return if root.key == key: if root.left: tmp = root.left while tmp.right: tmp = tmp.right pre[0] = tmp if root.right: tmp = root.right while tmp.left: tmp = tmp.left suc[0] = tmp return if root.key > key: suc[0] = root findPreSuc(root.left, pre, suc, key) else: pre[0] = root findPreSuc(root.right, pre, suc, key)
FUNC_DEF IF VAR RETURN IF VAR VAR IF VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR IF VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): pre[0], suc[0] = Node(-1), Node(-1) previous = Node(float("-inf")) next = Node(float("inf")) def inorder(root): if root is None: return inorder(root.left) nonlocal previous, next if root.key < key: previous.key = max(previous.key, root.key) elif root.key > key: next.key = min(next.key, root.key) inorder(root.right) inorder(root) if previous.key != float("-inf"): pre[0] = previous if next.key != float("inf"): suc[0] = next
FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): l1, l2 = [], [] def floor(root, l, k): if root == None: return if root.key >= k: floor(root.left, l, k) if root.key < k: if len(l) > 0: if l[0].key < root.key: l.pop() l.append(root) if len(l) == 0: l.append(root) floor(root.right, l, k) def ceil(root, l, k): if root == None: return if root.key <= k: ceil(root.right, l, k) if root.key > k: if len(l) > 0: if l[0].key > root.key: l.pop() l.append(root) if len(l) == 0: l.append(root) ceil(root.left, l, k) floor(root, l1, key) ceil(root, l2, key) if l1 != []: pre[0] = l1[0] if l2 != []: suc[0] = l2[0]
FUNC_DEF ASSIGN VAR VAR LIST LIST FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR LIST ASSIGN VAR NUMBER VAR NUMBER IF VAR LIST ASSIGN VAR NUMBER VAR NUMBER
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, data): def find(root, pre, suc, data): if root: if root.key < data and (pre[0] == None or root.key > pre[0].key): pre[0] = root if root.key > data and (suc[0] == None or root.key < suc[0].key): suc[0] = root find(root.left, pre, suc, data) find(root.right, pre, suc, data) return find(root, pre, suc, data)
FUNC_DEF FUNC_DEF IF VAR IF VAR VAR VAR NUMBER NONE VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR VAR VAR NUMBER NONE VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): prev = None stack = [] count = 2 while root != None or len(stack) != 0: while root != None: stack.append(root) root = root.left root = stack.pop() if root.key < key: pre[0] = root if root.key > key: suc[0] = root count = 0 return prev = root root = root.right
FUNC_DEF ASSIGN VAR NONE ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NONE FUNC_CALL VAR VAR NUMBER WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER RETURN ASSIGN VAR VAR ASSIGN VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): ans = [] def binarySearch(ans, key): low = 0 high = len(ans) while low < high: mid = (low + high) // 2 if ans[mid] == key: return mid elif ans[mid] > key: high = mid else: low = mid + 1 return low def inorder(root, d): if root is None: return inorder(root.left, d) ans.append(root.key) d[root.key] = root inorder(root.right, d) d = {} inorder(root, d) res = [] ind = binarySearch(ans, key) if ind < len(ans) and ans[ind] == key: if ind - 1 >= 0: pre[0] = d[ans[ind - 1]] if ind + 1 < len(ans): suc[0] = d[ans[ind + 1]] else: if ind - 1 >= 0: pre[0] = d[ans[ind - 1]] if ind < len(ans): suc[0] = d[ans[ind]]
FUNC_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): lol.pre = None lol.suc = None lol(root, key) pre[0] = lol.pre suc[0] = lol.suc def lol(root, key): if root is None: return if root.key == key: if root.left is not None: tmp = root.left while tmp.right: tmp = tmp.right lol.pre = tmp if root.right is not None: tmp = root.right while tmp.left: tmp = tmp.left lol.suc = tmp return if root.key > key: lol.suc = root lol(root.left, key) else: lol.pre = root lol(root.right, key)
FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR IF VAR NONE ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre_val, succ_val, key): if root is None: return if root.key == key: if root.left is not None: pre_val[0] = predecessor(root) if root.right is not None: succ_val[0] = successor(root) if root.key > key: succ_val[0] = root findPreSuc(root.left, pre_val, succ_val, key) if root.key < key: pre_val[0] = root findPreSuc(root.right, pre_val, succ_val, key) def predecessor(node): pre = node.left while pre.right is not None: pre = pre.right return pre def successor(node): succ = node.right while succ.left is not None: succ = succ.left return succ
FUNC_DEF IF VAR NONE RETURN IF VAR VAR IF VAR NONE ASSIGN VAR NUMBER FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR RETURN VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): mn = 0 mnD = 1000 mx = 0 mxD = 1000 temp = root while temp: if temp.key < key and key - temp.key < mnD: mnD = key - temp.key mn = temp.key if temp.key >= key: temp = temp.left else: temp = temp.right temp = root while temp: if temp.key > key and abs(key - temp.key) < mxD: mxD = abs(key - temp.key) mx = temp.key if temp.key > key: temp = temp.left else: temp = temp.right if mxD == 1000: mx = -1 if mnD == 1000: mn = -1 pre[0] = Node(mn) suc[0] = Node(mx)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): inr2 = [] inr = [] def inorder(node): if not node: return inorder(node.left) inr.append(node) inr2.append(node.key) inorder(node.right) inorder(root) pre[0] = None suc[0] = None n = len(inr) for i in range(n): if inr2[i] > key: suc[0] = inr[i] break for j in range(n - 1, -1, -1): if inr2[j] < key: pre[0] = inr[j] break
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NONE ASSIGN VAR NUMBER NONE ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): pre[0] = Node(float("-inf")) suc[0] = Node(float("inf")) def inorder(node): if node is None: return inorder(node.left) if node.key < key: pre[0] = pre[0] if pre[0].key > node.key else node elif node.key > key: suc[0] = suc[0] if suc[0].key < node.key else node inorder(node.right) inorder(root) pre[0] = None if pre[0].key == float("-inf") else pre[0] suc[0] = None if suc[0].key == float("inf") else suc[0]
FUNC_DEF ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR STRING NONE VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR STRING NONE VAR NUMBER
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): pre[0] = None suc[0] = None def succ(root, key): if root is None: return if root.key > key: nonlocal suc suc[0] = root succ(root.left, key) else: succ(root.right, key) def pred(root, key): if root is None: return if root.key < key: nonlocal pre pre[0] = root pred(root.right, key) else: pred(root.left, key) pred(root, key) succ(root, key)
FUNC_DEF ASSIGN VAR NUMBER NONE ASSIGN VAR NUMBER NONE FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def pred(root): root = root.left while root.right: root = root.right return root def succ(root): root = root.right while root.left: root = root.left return root def findPreSuc(root, pre, suc, key): if not root: return if root.key == key: if root.left: pre[0] = pred(root) if root.right: suc[0] = succ(root) if key < root.key: suc[0] = root findPreSuc(root.left, pre, suc, key) if key > root.key: pre[0] = root findPreSuc(root.right, pre, suc, key)
FUNC_DEF ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN IF VAR VAR IF VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def inorder(root, arr): if root == None: return inorder(root.left, arr) arr.append(root) inorder(root.right, arr) def findPreSuc(root, pre, suc, k): arr = [] inorder(root, arr) mini = 99999 maxi = -1 for i in arr: if i.key < k: mini = i else: break for i in range(len(arr) - 1, -1, -1): if arr[i].key > k: maxi = arr[i] else: break if mini != 99999: pre[0] = mini if maxi != -1: suc[0] = maxi
FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): stack = [] prev = None while True: if root: stack.append(root) root = root.left else: if not stack: break root = stack.pop() if root.key > key: break else: if root.key != key: prev = root root = root.right pre[0] = prev suc[0] = root
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NONE WHILE NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): pre[0] = suc[0] = Node(-1) dummy = root while dummy is not None: if dummy.key < key: pre[0] = dummy dummy = dummy.right else: dummy = dummy.left dummy = root while dummy is not None: if dummy.key > key: suc[0] = dummy dummy = dummy.left else: dummy = dummy.right
FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def InorderPredecessor(root): if root.left == None: return None temp = root.left while temp.right != None: temp = temp.right return temp def InorderSuccessor(root): if root.right == None: return None temp = root.right while temp.left != None: temp = temp.left return temp def findPreSuc(root, pre, suc, key): temp = root while temp != None: if temp.key == key: p = InorderPredecessor(temp) s = InorderSuccessor(temp) if p != None: pre[0] = p if s != None: suc[0] = s return elif temp.key > key: suc[0] = temp temp = temp.left else: pre[0] = temp temp = temp.right
FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR NUMBER VAR IF VAR NONE ASSIGN VAR NUMBER VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): def successful(root, key, pre, suc): if not root: return None if key < root.key: suc[0] = root successful(root.left, key, pre, suc) elif key > root.key: pre[0] = root successful(root.right, key, pre, suc) else: if root.left: pre[0] = pred(root.left) if root.right: suc[0] = succ(root.right) return def pred(root): while root.right: root = root.right return root def succ(root): while root.left: root = root.left return root successful(root, key, pre, suc)
FUNC_DEF FUNC_DEF IF VAR RETURN NONE IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR RETURN FUNC_DEF WHILE VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def InOrder(root, lis): if root != None: InOrder(root.left, lis) lis.append(root) InOrder(root.right, lis) def findPreSuc(root, pre, suc, key): lis = [] InOrder(root, lis) for i in range(0, len(lis)): if lis[i].key == key: if i - 1 >= 0: pre[0] = lis[i - 1] if i + 1 < len(lis): suc[0] = lis[i + 1] break if lis[i].key < key and i + 1 < len(lis) and lis[i + 1].key > key: pre[0] = lis[i] suc[0] = lis[i + 1] break if lis[i].key < key and i + 1 == len(lis): pre[0] = lis[i] break
FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): c = root while c: if c.key < key: pre[0] = c c = c.right else: c = c.left c = root while c: if c.key > key: suc[0] = c c = c.left else: c = c.right
FUNC_DEF ASSIGN VAR VAR WHILE VAR IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def dfs(node, key, pre, suc, prev): if not node: return if dfs(node.left, key, pre, suc, prev): return True if prev[0] < key and node.key > key: pre[0] = Node(prev[0]) suc[0] = node return True if key != node.key: prev[0] = node.key if dfs(node.right, key, pre, suc, prev): return True def findPreSuc(root, pre, suc, key): prev = [-1] if not dfs(root, key, pre, suc, prev): pre[0] = Node(prev[0])
FUNC_DEF IF VAR RETURN IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def findPreSuc(root, pre, suc, key): if root == None: return temp = root while temp != None and temp.key != key: if temp.key > key: suc[0] = temp temp = temp.left elif temp.key < key: pre[0] = temp temp = temp.right if temp == None: return leftTree = temp.left while leftTree != None: pre[0] = leftTree leftTree = leftTree.right rightTree = temp.right while rightTree != None: suc[0] = rightTree rightTree = rightTree.left
FUNC_DEF IF VAR NONE RETURN ASSIGN VAR VAR WHILE VAR NONE VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR IF VAR NONE RETURN ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR NUMBER VAR ASSIGN VAR VAR
There is BST given with the root node with the key part as an integer only. You need to find the in-order successor and predecessor of a given key. If either predecessor or successor is not found, then set it to NULL. Note:- In an inorder traversal the number just smaller than the target is the predecessor and the number just greater than the target is the successor. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 key = 8 Output: 6 10 Explanation: In the given BST the inorder predecessor of 8 is 6 and inorder successor of 8 is 10. Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 key = 11 Output: 10 -1 Explanation: In given BST, the inorder predecessor of 11 is 10 whereas it does not have any inorder successor. Your Task: You don't need to print anything. You need to update pre with the predecessor of the key or NULL if the predecessor doesn't exist and succ to the successor of the key or NULL if the successor doesn't exist. pre and succ are passed as an argument to the function findPreSuc(). Expected Time Complexity: O(Height of the BST).Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{4} 1 <= key of node <= 10^{7} 1 <= key <= 10^{7}
def inorder(root): if root == None: return [] return inorder(root.left) + [root] + inorder(root.right) def findPreSuc(root, pre, suc, key): data = inorder(root) for i in range(len(data)): if data[i].key == key: if i != 0: pre[0] = data[i - 1] if i + 1 < len(data): suc[0] = data[i + 1] break elif data[i].key > key: if i != 0: pre[0] = data[i - 1] suc[0] = data[i] break if pre[0] == None and suc[0] == None: pre[0] = data[-1]
FUNC_DEF IF VAR NONE RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER NONE VAR NUMBER NONE ASSIGN VAR NUMBER VAR NUMBER
On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th square dropped (positions[i] = (left, side_length)) is a square with the left-most point being positions[i][0] and sidelength positions[i][1]. The square is dropped with the bottom edge parallel to the number line, and from a higher height than all currently landed squares. We wait for each square to stick before dropping the next. The squares are infinitely sticky on their bottom edge, and will remain fixed to any positive length surface they touch (either the number line or another square). Squares dropped adjacent to each other will not stick together prematurely. Return a list ans of heights. Each height ans[i] represents the current highest height of any square we have dropped, after dropping squares represented by positions[0], positions[1], ..., positions[i]. Example 1: Input: [[1, 2], [2, 3], [6, 1]] Output: [2, 5, 5] Explanation: After the first drop of positions[0] = [1, 2]: _aa _aa ------- The maximum height of any square is 2. After the second drop of positions[1] = [2, 3]: __aaa __aaa __aaa _aa__ _aa__ -------------- The maximum height of any square is 5. The larger square stays on top of the smaller square despite where its center of gravity is, because squares are infinitely sticky on their bottom edge. After the third drop of positions[1] = [6, 1]: __aaa __aaa __aaa _aa _aa___a -------------- The maximum height of any square is still 5. Thus, we return an answer of [2, 5, 5]. Example 2: Input: [[100, 100], [200, 100]] Output: [100, 100] Explanation: Adjacent squares don't get stuck prematurely - only their bottom edge can stick to surfaces. Note: 1 . 1 . 1 .
class Solution: def fallingSquares(self, positions): height = [0] pos = [0] res = [] max_h = 0 for left, side in positions: i = bisect.bisect_right(pos, left) j = bisect.bisect_left(pos, left + side) high = max(height[i - 1 : j] or [0]) + side pos[i:j] = [left, left + side] height[i:j] = [high, height[j - 1]] max_h = max(max_h, high) res.append(max_h) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR LIST NUMBER VAR ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th square dropped (positions[i] = (left, side_length)) is a square with the left-most point being positions[i][0] and sidelength positions[i][1]. The square is dropped with the bottom edge parallel to the number line, and from a higher height than all currently landed squares. We wait for each square to stick before dropping the next. The squares are infinitely sticky on their bottom edge, and will remain fixed to any positive length surface they touch (either the number line or another square). Squares dropped adjacent to each other will not stick together prematurely. Return a list ans of heights. Each height ans[i] represents the current highest height of any square we have dropped, after dropping squares represented by positions[0], positions[1], ..., positions[i]. Example 1: Input: [[1, 2], [2, 3], [6, 1]] Output: [2, 5, 5] Explanation: After the first drop of positions[0] = [1, 2]: _aa _aa ------- The maximum height of any square is 2. After the second drop of positions[1] = [2, 3]: __aaa __aaa __aaa _aa__ _aa__ -------------- The maximum height of any square is 5. The larger square stays on top of the smaller square despite where its center of gravity is, because squares are infinitely sticky on their bottom edge. After the third drop of positions[1] = [6, 1]: __aaa __aaa __aaa _aa _aa___a -------------- The maximum height of any square is still 5. Thus, we return an answer of [2, 5, 5]. Example 2: Input: [[100, 100], [200, 100]] Output: [100, 100] Explanation: Adjacent squares don't get stuck prematurely - only their bottom edge can stick to surfaces. Note: 1 . 1 . 1 .
class Solution: def fallingSquares(self, positions): ints = [] for left, size in positions: ints.append(left) ints.append(left + size - 1) index = {num: idx for idx, num in enumerate(sorted(ints))} heights = [0] * 2 * len(positions) def query(left, right): return max(heights[index[left] : index[right] + 1]) def updateHeights(left, right, newheight): for i in range(index[left], index[right] + 1): heights[i] = newheight maxheights = [] for left, size in positions: right = left + size - 1 newheight = query(left, right) + size updateHeights(left, right, newheight) if not maxheights or maxheights[-1] <= newheight: maxheights.append(newheight) else: maxheights.append(maxheights[-1]) return maxheights
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Bear Limak has a sequence of N non-negative integers A1, A2, ..., AN. He defines the score of a segment (consecutive subsequence) as its sum of elements modulo P (not necessarily prime). Find the maximum score of a non-empty segment, and also find the number of segments with this maximum score. -----Input----- First line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. For each test case, the first line of the input contains two space separated integers, N and P. The second line contains N space separated integers denoting the sequence. -----Output----- For each test case, output two space separated integers denoting the maximum score of a segment and the number of segments with the score, respectively. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ P ≤ 109 - 0 ≤ Ai ≤ 109 Subtask #1: (25 points) - 1 ≤ N ≤ 100 Subtask #2: (25 points) - 1 ≤ N ≤ 1000 Subtask #3: (50 points) - original constraints -----Example----- Input: 4 2 3 1 2 3 5 2 4 3 3 100 1 3 5 4 3 1 2 3 4 Output: 2 1 4 2 9 1 2 2 -----Explanation----- Example case 1. There are three segments - [1], [2] and [1, 2]. Sum of these segments are 1, 2 and 3 respectively. Sum of these segments modulo 3 will be 1, 2 and 0. Maximum score among these is 2. There is also only one segment with this score. Example case 2. There are six segments - [2], [4], [3], [2, 4], [4, 3] and [2, 4, 3]. Sum of these segments are 2, 4, 3, 6, 7, 9 respectively. Sum of these segments modulo 5 will be 2, 4, 3, 1, 2, 4. Maximum score among these is 4. And there are two segments with this score.
t = int(input()) for _ in range(t): n, p = map(int, input().split()) l = list(map(int, input().split())) d = {} for i in range(n): c = 0 for j in range(i, n): c += l[j] try: d[c % p] += 1 except: d[c % p] = 1 x = max(list(d.keys())) print(x, d[x])
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 DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Bear Limak has a sequence of N non-negative integers A1, A2, ..., AN. He defines the score of a segment (consecutive subsequence) as its sum of elements modulo P (not necessarily prime). Find the maximum score of a non-empty segment, and also find the number of segments with this maximum score. -----Input----- First line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. For each test case, the first line of the input contains two space separated integers, N and P. The second line contains N space separated integers denoting the sequence. -----Output----- For each test case, output two space separated integers denoting the maximum score of a segment and the number of segments with the score, respectively. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ P ≤ 109 - 0 ≤ Ai ≤ 109 Subtask #1: (25 points) - 1 ≤ N ≤ 100 Subtask #2: (25 points) - 1 ≤ N ≤ 1000 Subtask #3: (50 points) - original constraints -----Example----- Input: 4 2 3 1 2 3 5 2 4 3 3 100 1 3 5 4 3 1 2 3 4 Output: 2 1 4 2 9 1 2 2 -----Explanation----- Example case 1. There are three segments - [1], [2] and [1, 2]. Sum of these segments are 1, 2 and 3 respectively. Sum of these segments modulo 3 will be 1, 2 and 0. Maximum score among these is 2. There is also only one segment with this score. Example case 2. There are six segments - [2], [4], [3], [2, 4], [4, 3] and [2, 4, 3]. Sum of these segments are 2, 4, 3, 6, 7, 9 respectively. Sum of these segments modulo 5 will be 2, 4, 3, 1, 2, 4. Maximum score among these is 4. And there are two segments with this score.
for _ in range(int(input())): n, p = map(int, input().split()) l = [int(i) for i in input().split()] pre = [0] * (n + 1) for i in range(1, n + 1): pre[i] = pre[i - 1] + l[i - 1] pre[i] %= p cmaxi = 0 maxi = 0 for i in range(n): for j in range(i, n): curr = (pre[j + 1] - pre[i] + p) % p if curr > maxi: maxi = curr cmaxi = 1 elif curr == maxi: cmaxi += 1 print(maxi, cmaxi)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Bear Limak has a sequence of N non-negative integers A1, A2, ..., AN. He defines the score of a segment (consecutive subsequence) as its sum of elements modulo P (not necessarily prime). Find the maximum score of a non-empty segment, and also find the number of segments with this maximum score. -----Input----- First line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. For each test case, the first line of the input contains two space separated integers, N and P. The second line contains N space separated integers denoting the sequence. -----Output----- For each test case, output two space separated integers denoting the maximum score of a segment and the number of segments with the score, respectively. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ P ≤ 109 - 0 ≤ Ai ≤ 109 Subtask #1: (25 points) - 1 ≤ N ≤ 100 Subtask #2: (25 points) - 1 ≤ N ≤ 1000 Subtask #3: (50 points) - original constraints -----Example----- Input: 4 2 3 1 2 3 5 2 4 3 3 100 1 3 5 4 3 1 2 3 4 Output: 2 1 4 2 9 1 2 2 -----Explanation----- Example case 1. There are three segments - [1], [2] and [1, 2]. Sum of these segments are 1, 2 and 3 respectively. Sum of these segments modulo 3 will be 1, 2 and 0. Maximum score among these is 2. There is also only one segment with this score. Example case 2. There are six segments - [2], [4], [3], [2, 4], [4, 3] and [2, 4, 3]. Sum of these segments are 2, 4, 3, 6, 7, 9 respectively. Sum of these segments modulo 5 will be 2, 4, 3, 1, 2, 4. Maximum score among these is 4. And there are two segments with this score.
for _ in range(int(input())): n, m = input().split() n, m = int(n), int(m) x = y = c = 0 l = list(map(int, input().split())) for i in range(n): for j in range(i, n): x = x + l[j] if x % m > y: y = x % m c = 1 elif y == x % m: c += 1 x = 0 print(y, c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: permutations = [permutation for permutation in range(1, m + 1)] result = [] for query in queries: for pindex, permutation in enumerate(permutations): if permutation == query: result.append(pindex) del permutations[pindex] permutations.insert(0, permutation) break print(result) print(permutations) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Node: def __init__(self, val): self.val = val self.next = None class Linked: def __init__(self, m): self.head = Node(1) cur = self.head for n in range(2, m + 1): cur.next = Node(n) cur = cur.next def print_me(self): cur = self.head while cur: print(cur.val) cur = cur.next def move_to_front(self, val): if self.head.val == val: return 0 cur = self.head i = 0 while cur.val != val: prev = cur cur = cur.next i += 1 prev.next = cur.next cur.next = self.head self.head = cur return i class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: linked = Linked(m) result = [] for num in queries: result.append(linked.move_to_front(num)) return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: result = [] coord = [x for x in range(0, m)] for num in queries: before = coord[num - 1] result.append(before) coord = [(x + 1 if x < before else x) for x in coord] coord[num - 1] = 0 return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: ans = [] arr = list(range(1, m + 1)) for j in queries: for i in range(m): if j == arr[i]: ans.append(i) x = arr.pop(i) arr.insert(0, x) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: ans = [] P = [] for i in range(m): P.append(i + 1) for i in range(len(queries)): a = P.index(queries[i]) ans.append(a) b = P.pop(a) P = [b] + P return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: d = {k: (k + 1) for k in range(m)} result = [] for q in queries: current_pos = self.findPos(d, q) result.append(current_pos) while current_pos > 0: d[current_pos] = d[current_pos - 1] current_pos -= 1 d[0] = q return result def findPos(self, d, q): for idx, val in list(d.items()): if val == q: return idx
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR RETURN VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: pivot = [] for q in queries: pos = q - 1 for piv in pivot: pos = self.getpos(pos, piv) pivot.append(pos) return pivot def getpos(self, pos, piv): if pos > piv: return pos elif pos == piv: return 0 else: return pos + 1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR RETURN NUMBER RETURN BIN_OP VAR NUMBER
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: cache = {x: (x - 1) for x in range(1, m + 1)} n = len(queries) ans = [0] * n for i in range(n): curr = queries[i] ans[i] = cache[curr] pos = cache[curr] for j in cache.keys(): if cache[j] < pos: cache[j] += 1 cache[curr] = 0 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: data, result = list(range(1, m + 1)), [] for item in queries: idx = data.index(item) data = ( [data[idx]] + data[:idx] + (data[idx + 1 :] if idx + 1 < len(data) else []) ) result.append(idx) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER LIST EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: p = [i for i in range(1, m + 1)] res = [] for query in queries: index = p.index(query) res.append(index) p.remove(query) p = [query] + p return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: original = list(range(1, m + 1)) results = [] for q in queries: res = 0 for ind, el in enumerate(original): if el == q: res = ind break results.append(res) temp = original.pop(res) original = [temp] + original return results
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: retIndexlist = [] p = [(i + 1) for i in range(m)] for q in queries: idx = p.index(q) retIndexlist.append(idx) p.pop(idx) p.insert(0, q) return retIndexlist
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: P = [i for i in range(1, m + 1)] result = [] for q in queries: result.append(P.index(q)) P.remove(q) P.insert(0, q) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: result = [] result += [queries[0] - 1] for ind, q in enumerate(queries[1:]): ind = ind + 1 if q > max(queries[:ind]): result += [q - 1] else: equal_q = [i for i in range(ind) if q == queries[i]] if len(equal_q) > 0: diff = len(list(set(queries[equal_q[-1] + 1 : ind]))) result += [diff] else: sum_higher = len([x for x in list(set(queries[:ind])) if x > q]) result += [q + sum_higher - 1] return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR LIST BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: permu = list(range(1, m + 1)) pos = [] for i in queries: p = permu.index(i) pos.append(p) for j in range(p - 1, -1, -1): permu[j + 1] = permu[j] permu[0] = i return pos
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class linkedNode: def __init__(self, val): self.val = val self.prev = None self.next = None class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: head = linkedNode(-1) pointer = head for i in range(1, m + 1): newLN = linkedNode(i) newLN.prev = pointer pointer.next = newLN pointer = pointer.next pointer.next = linkedNode(-1) pointer.next.prev = pointer ans = [] for query in queries: i = 0 pointer = head.next while pointer.val != query: pointer = pointer.next i += 1 ans.append(i) pointer.prev.next = pointer.next pointer.next.prev = pointer.prev pointer.next = head.next head.next.prev = pointer head.next = pointer pointer.prev = head return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR