description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m, n = len(matrix), len(matrix[0]) ans = 0 for sx in range(m): for sy in range(n): if matrix[sx][sy] == 1: ans += 1 for by in range(sy + 1, n): if matrix[sx][by] == 1 and sx + (by - sy) < m: side = by - sy all_one = True for x in range(sx, sx + side + 1): if matrix[x][by] != 1: all_one = False break if not all_one: break for y in range(sy, sy + side + 1): if matrix[sx + side][y] != 1: all_one = False break if all_one: ans += 1 else: break else: break return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: h, w = len(matrix), len(matrix[0]) acc_mat, merge_status_mat = [([0] * w) for _ in range(h)], [ ([0] * w) for _ in range(h) ] for i in range(h): for j in range(w): if j - 1 >= 0: left = acc_mat[i][j - 1] left_status = merge_status_mat[i][j - 1] else: left = 0 left_status = 0 if i - 1 >= 0: top = acc_mat[i - 1][j] top_status = merge_status_mat[i - 1][j] else: top = 0 top_status = 0 if i - 1 >= 0 and j - 1 >= 0: topLeft = acc_mat[i - 1][j - 1] topLeft_status = merge_status_mat[i - 1][j - 1] else: topLeft = 0 topLeft_status = 0 merge_status_mat[i][j] = ( min(topLeft_status, top_status, left_status) + 1 if matrix[i][j] == 1 else 0 ) acc_mat[i][j] = left + top - topLeft + merge_status_mat[i][j] return acc_mat[i][j]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: n, m = len(matrix), len(matrix[0]) dp = [matrix[0][j] for j in range(m)] count = sum(dp) for i in range(1, n): new_dp = [matrix[i][j] for j in range(m)] for j in range(1, m): if matrix[i][j] == 1: new_dp[j] = min(dp[j], new_dp[j - 1], dp[j - 1]) + 1 dp = new_dp count += sum(dp) return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: row, col = len(matrix), len(matrix[0]) count = 0 for i in range(row): for j in range(col): if matrix[i][j]: if i == 0 or j == 0: count += 1 else: check_val = min( matrix[i - 1][j], matrix[i][j - 1], matrix[i - 1][j - 1] ) count += check_val + 1 matrix[i][j] = check_val + 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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 ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: def is_valid_square(start_row, end_row, start_col, end_col, matrix): return not any( matrix[row][col] == 0 for row in range(start_row, end_row + 1) for col in range(start_col, end_col + 1) ) result = 0 for row in range(len(matrix)): for col in range(len(matrix[0])): search_space_increase = 0 while row + search_space_increase < len( matrix ) and col + search_space_increase < len(matrix[0]): if not is_valid_square( row, row + search_space_increase, col, col + search_space_increase, matrix, ): break result += 1 search_space_increase += 1 return result
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: ret = 0 for row in range(len(matrix)): for col in range(len(matrix[0])): if matrix[row][col] == 1: sublen = 1 while ( row + sublen <= len(matrix) and col + sublen <= len(matrix[0]) and 0 not in [ matrix[row + sublen - 1][ncol] for ncol in range(col, col + sublen) ] and 0 not in [ matrix[nrow][col + sublen - 1] for nrow in range(row, row + sublen) ] ): ret += 1 sublen += 1 return ret
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: memo = {} def recursive(row, col): if ( 0 <= row < len(matrix) and 0 <= col < len(matrix[0]) and matrix[row][col] == 1 ): nextRow = recursive(row + 1, col) nextCol = recursive(row, col + 1) nextDiag = recursive(row + 1, col + 1) return min(nextRow, nextCol, nextDiag) + 1 return 0 def cached(row, col): pair = row, col if pair in memo: return memo[pair] if ( 0 <= row < len(matrix) and 0 <= col < len(matrix[0]) and matrix[row][col] == 1 ): nextRow = cached(row + 1, col) nextCol = cached(row, col + 1) nextDiag = cached(row + 1, col + 1) memo[pair] = min(nextRow, nextCol, nextDiag) + 1 return memo[pair] return 0 count = 0 for i in range(len(matrix)): for j in range(len(matrix[0])): count += cached(i, j) return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR RETURN VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def memoization(self, grid, i, j, memo): if i == len(grid) - 1 or j == len(grid[0]) - 1: return int(grid[i][j] == 1) if memo[i][j] is None: if grid[i][j] == 0: memo[i][j] = 0 else: col = self.memoization(grid, i, j + 1, memo) row = self.memoization(grid, i + 1, j, memo) diag = self.memoization(grid, i + 1, j + 1, memo) memo[i][j] = min(min(col, row), diag) + 1 return memo[i][j] def countSquares(self, matrix: List[List[int]]) -> int: memo = [[None for j in range(len(matrix[0]))] for i in range(len(matrix))] out = 0 for i in range(len(matrix)): for j in range(len(matrix[0])): out += self.memoization(matrix, i, j, memo) return out
CLASS_DEF FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NONE IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: height, width = len(matrix), len(matrix[0]) n = 0 def n_squares(x, y): length = 0 if not matrix[y][x]: return 0 while x + length + 1 <= width and y + length + 1 <= height: content = [ row[x : x + length + 1] for row in matrix[y : y + length + 1] ] if not all(i for j in content for i in j): break length += 1 return length for x in range(width): for y in range(height): n += n_squares(x, y) return n
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR RETURN NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m, n = len(matrix), len(matrix[0]) dp = [[(0) for _ in range(n + 1)] for _ in range(m + 1)] for i in range(m): for j in range(n): if matrix[i][j] == 1: dp[i + 1][j + 1] = ( min(dp[i][j + 1], min(dp[i + 1][j], dp[i][j])) + 1 ) cnts = [(0) for _ in range(min(m, n) + 1)] for i in range(1, min(m, n) + 1): cnts[i] = i**2 + cnts[i - 1] res = 0 for i in range(1, m + 1): for j in range(1, n + 1): res += dp[i][j] return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR 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 VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, M: List[List[int]]) -> int: if not M: return 0 m, n = len(M), len(M[0]) rowCounts = [([0] * n) for _ in range(m)] for i in range(m): for j in range(n): if M[i][j]: rowCounts[i][j] = 1 + rowCounts[i][j - 1] if j > 0 else 1 colCounts = [([0] * n) for _ in range(m)] for j in range(n): for i in range(m): if M[i][j]: colCounts[i][j] = 1 + colCounts[i - 1][j] if i > 0 else 1 dp = [([0] * n) for _ in range(m)] out = 0 for i in range(m): for j in range(n): if M[i][j]: if i == 0 or j == 0: dp[i][j] = min(rowCounts[i][j], colCounts[i][j]) else: dp[i][j] = min( rowCounts[i][j], colCounts[i][j], 1 + dp[i - 1][j - 1] ) out += dp[i][j] return out
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER 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 VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER 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 VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: squares = {(1): set()} max_l = min(len(matrix), len(matrix[0])) for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j]: squares[1].add((i, j)) for l in range(2, max_l + 1): squares[l] = set() for s in squares[l - 1]: right = s[0] + 1, s[1] down = s[0], s[1] + 1 down_right = s[0] + 1, s[1] + 1 if ( right in squares[l - 1] and down in squares[l - 1] and down_right in squares[l - 1] ): squares[l].add(s) return sum([len(s) for s in squares.values()])
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: for i in range(1, len(matrix)): for j in range(1, len(matrix[0])): matrix[i][j] = matrix[i][j] * ( min({matrix[i - 1][j - 1], matrix[i - 1][j], matrix[i][j - 1]}) + 1 ) s = 0 for i in matrix: s += sum(i) return s
CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m = len(matrix) n = len(matrix[0]) if m == 1 and n == 1: return matrix[0][0] count = [[([0] * 3) for i in range(n + 1)] for _ in range(m + 1)] res = 0 for i in range(1, m + 1): for j in range(1, n + 1): if matrix[i - 1][j - 1]: count[i][j][0] = count[i - 1][j][0] + 1 count[i][j][1] = count[i][j - 1][1] + 1 count[i][j][2] = ( min( count[i - 1][j - 1][2], count[i - 1][j][0], count[i][j - 1][1], ) + 1 ) res += count[i][j][2] return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: dp = [[(0) for _ in range(len(matrix[0]))] for _ in range(len(matrix))] sum = 0 for i in range(len(dp[0])): dp[0][i] = matrix[0][i] sum += dp[0][i] print(sum) for j in range(1, len(dp)): dp[j][0] = matrix[j][0] sum += dp[j][0] print(sum) for i in range(1, len(dp)): for j in range(1, len(dp[0])): if matrix[i][j] == 1: dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1 sum += dp[i][j] return sum
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL 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 VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: n = len(matrix) m = len(matrix[0]) res = 0 for i in range(n): for j in range(m): done = False new_i = i new_j = j while 0 <= new_i < n and 0 <= new_j < m and not done: area = 0 for k in range(i, new_i + 1): area += matrix[k][new_j] for k in range(j, new_j + 1): area += matrix[new_i][k] area -= matrix[new_i][new_j] if area == new_j - j + new_i - i + 1: res += 1 new_i = new_i + 1 new_j = new_j + 1 else: done = True return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: totalRows = len(matrix) totalColumns = len(matrix[0]) longestStreak = [ [(0, 0, 0) for i in range(totalColumns)] for j in range(totalRows) ] total = 0 for i in range(totalRows): for j in range(totalColumns): if i == 0 and j == 0: longestStreak[i][j] = matrix[i][j], matrix[i][j], matrix[i][j] total += matrix[i][j] elif i == 0: longestStreak[i][j] = ( matrix[i][j] * (longestStreak[i][j - 1][0] + 1), matrix[i][j], matrix[i][j], ) total += matrix[i][j] elif j == 0: longestStreak[i][j] = ( matrix[i][j], matrix[i][j] * (longestStreak[i - 1][j][1] + 1), matrix[i][j], ) total += matrix[i][j] else: a = matrix[i][j] * (longestStreak[i][j - 1][0] + 1) b = matrix[i][j] * (longestStreak[i - 1][j][1] + 1) c = matrix[i][j] * (longestStreak[i - 1][j - 1][2] + 1) longestStreak[i][j] = a, b, min([a, b, c]) total += min([a, b, c]) return total
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
def checkCorners(row_size, col_size, r, c): if ( (0 <= r - 1 < row_size and 0 <= c < col_size) and (0 <= r - 1 < row_size and 0 <= c - 1 < col_size) and (0 <= r < row_size and 0 <= c - 1 < col_size) ): return True return False class Solution: def countSquares(self, matrix: List[List[int]]) -> int: dp = matrix[:] r_size = len(matrix) c_size = len(matrix[0]) acc = 0 for row in range(len(matrix)): for col in range(len(matrix[0])): if matrix[row][col] == 1 and checkCorners(r_size, c_size, row, col): dp[row][col] += min( matrix[row][col - 1], matrix[row - 1][col], matrix[row - 1][col - 1], ) acc += dp[row][col] return acc
FUNC_DEF IF NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: if not matrix: return 0 m, n = len(matrix), len(matrix[0]) squares = copy.deepcopy(matrix) count = 0 ones_in_col = [0] * n for row in range(m): ones_in_row = 0 for col in range(n): ones_in_row = ones_in_row + 1 if matrix[row][col] else 0 ones_in_col[col] = ones_in_col[col] + 1 if matrix[row][col] else 0 top_left_res = 0 if row == 0 or col == 0 else squares[row - 1][col - 1] squares[row][col] = min(ones_in_row, ones_in_col[col], top_left_res + 1) count += squares[row][col] return count
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: dp = [[(0) for _ in range(len(matrix[0]))] for _ in range(len(matrix))] summ = 0 for i in range(len(matrix)): for j in range(len(matrix[0])): if i == 0 or j == 0: dp[i][j] = matrix[i][j] elif matrix[i][j] == 0: dp[i][j] = 0 else: dp[i][j] = 1 + min(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]) summ += dp[i][j] return summ
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m = len(matrix) n = len(matrix[0]) dp = [[(0) for i in range(n)] for j in range(m)] count = 0 for i in range(m): for j in range(n): if i - 1 >= 0 and j - 1 >= 0 and matrix[i][j] == 1: dp[i][j] = ( min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1 ) elif matrix[i][j] == 1: dp[i][j] = 1 else: dp[i][j] = 0 count += dp[i][j] return count
CLASS_DEF FUNC_DEF 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: memo = [ [(0) for x in range(len(matrix[0]) + 1)] for y in range(len(matrix) + 1) ] for i in range(1, len(memo)): for j in range(1, len(memo[0])): if matrix[i - 1][j - 1] == 0: memo[i][j] = 0 else: memo[i][j] = ( min([memo[i - 1][j - 1], memo[i - 1][j], memo[i][j - 1]]) + 1 ) tot = 0 for i in range(len(memo)): for j in range(len(memo[0])): tot += memo[i][j] return tot
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: hori = [row[:] for row in matrix] vert = [row[:] for row in matrix] R = len(matrix) C = len(matrix[0]) for i in range(R): s = 0 for j in range(C): if matrix[i][j]: s += matrix[i][j] else: s = 0 hori[i][j] = s for j in range(C): s = 0 for i in range(R): if matrix[i][j]: s += matrix[i][j] else: s = 0 vert[i][j] = s out = 0 for i in range(R): for j in range(C): if matrix[i][j] == 1: if i - 1 < 0 or j - 1 < 0: out += 1 continue if matrix[i - 1][j - 1] == 0: out += 1 continue x = int(matrix[i - 1][j - 1] ** 0.5) a = min([x, hori[i][j - 1], vert[i - 1][j]]) out += a + 1 matrix[i][j] = (a + 1) ** 2 return out
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: sq = 0 m_row = range(len(matrix)) m_col = range(len(matrix[0])) def expand_square(row, n, m, p, square=0, count=1): while count > -1: print(count, square) for i in range(count): if row + i == m: return square for j in range(count): if n + j == p: return square elif matrix[row + i][n + j] != 1: return square square += 1 count += 1 for row in m_row: for n in m_col: if matrix[row][n] == 1: sq += expand_square(row, n, len(matrix), len(matrix[0])) return sq
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF NUMBER NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR NUMBER VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: def is_ones(lst, n, i, j): ones = 0 for ix in range(i, i + n): for jy in range(j, j + n): if lst[ix][jy] == 1: ones += 1 return ones == n * n n = len(matrix) m = len(matrix[0]) ans = 0 for i in range(n): for j in range(m): if matrix[i][j] == 1: tmp = 1 while i + tmp <= n and j + tmp <= m: if is_ones(matrix, tmp, i, j): ans += 1 tmp += 1 else: break return ans
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: n, m = len(matrix), len(matrix[0]) for i in range(1, n): for j in range(1, m): if matrix[i][j]: matrix[i][j] = ( min(matrix[i - 1][j], matrix[i][j - 1], matrix[i - 1][j - 1]) + 1 ) return sum(sum(x) for x in matrix)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def gs(self, matrix, x, y, i, j): ans = matrix[i][j] if y: ans -= matrix[i][y - 1] if x: ans -= matrix[x - 1][j] if x and y: ans += matrix[x - 1][y - 1] return ans def countSquares(self, matrix: List[List[int]]) -> int: n: int = len(matrix) m = 0 m = len(matrix[0]) for i in range(n): pre = 0 for j in range(m): matrix[i][j] += pre pre = matrix[i][j] for i in range(1, n): for j in range(m): matrix[i][j] += matrix[i - 1][j] ans = 0 for i in range(n): for j in range(m): x, y = i, j l = 1 while x >= 0 and y >= 0: if l * l == self.gs(matrix, x, y, i, j): ans += 1 else: break x, y, l = x - 1, y - 1, l + 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, a: List[List[int]]) -> int: d = [] r = len(a) + 1 c = len(a[0]) + 1 d = [[(0) for j in range(c)] for i in range(r)] for i in range(1, r): for j in range(1, c): if a[i - 1][j - 1] == 1: d[i][j] = 1 + min(d[i - 1][j], d[i][j - 1], d[i - 1][j - 1]) print(d) return sum(map(sum, d))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: n = len(matrix) m = len(matrix[0]) a = 0 for i in range(n): for j in range(m): if matrix[i][j] == 1: if i == 0 or j == 0: a += 1 else: t = ( min( matrix[i - 1][j - 1], matrix[i - 1][j], matrix[i][j - 1] ) + matrix[i][j] ) a += t matrix[i][j] = t return a
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m, n = len(matrix), len(matrix[0]) dp = [([0] * (n + 1)) for _ in range(m + 1)] result = 0 for i in range(m): for j in range(n): dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1] - dp[i][j] + matrix[i][j] for i in range(m): for j in range(n): for l in range(1, min(m - i, n - j) + 1): expected_sum = l**2 x, y = i + l, j + l range_sum = dp[x][y] - dp[x][j] - dp[i][y] + dp[i][j] if expected_sum != range_sum: break result += 1 return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN 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 FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: rows = len(matrix) cols = len(matrix[0]) count = 0 for r in range(rows): for c in range(cols): if matrix[r][c] == 1: print((r, c)) size = 0 notOne = False while not notOne and r + size <= rows - 1 and c + size <= cols - 1: for p in range(size + 1): if ( matrix[r + p][c + size] != 1 or matrix[r + size][c + p] != 1 ): notOne = True break if matrix[r + size][c + size] != 1: notOne = True if not notOne: count += 1 size += 1 print(size) print(count) return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: grid = matrix self.dp = [[[0, 0] for j in grid[0]] for i in grid] dp = self.dp def ut(r, c): if r >= 0 and c >= 0: return self.dp[r][c][0] return 0 for r in range(len(grid)): for c in range(len(grid[0])): if r and c and grid[r][c]: dp[r][c][1] = min( dp[r][c - 1][1], dp[r - 1][c][1], dp[r - 1][c - 1][1] ) if dp[r][c][1]: dp[r][c][1] += 1 dp[r][c][1] = max(dp[r][c][1], grid[r][c]) dp[r][c][0] += dp[r][c][1] dp[r][c][0] += ut(r, c - 1) + ut(r - 1, c) - ut(r - 1, c - 1) print(dp) return dp[-1][-1][0]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER NUMBER VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m, n = len(matrix), len(matrix[0]) dp = [([0] * n) for _ in range(m)] squares = 0 for i in range(m): for j in range(n): if not matrix[i][j]: continue dp[i][j] = 1 if i > 0 and j > 0: dp[i][j] += min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) squares += dp[i][j] return squares
CLASS_DEF FUNC_DEF 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: count = 0 m = len(matrix) n = len(matrix[0]) def invalid_matrix(r, c, s): for i in range(0, s): for j in range(s): if r + i >= m or c + j >= n: return True if r + i < m and c + j < n and matrix[r + i][c + j] == 0: return True return False for i in range(m): for j in range(n): if matrix[i][j] == 1: count += 1 size = 2 while True: if invalid_matrix(i, j, size): break count += 1 size += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: dp = [] count = 0 for i in range(len(matrix)): inner = [] for j in range(len(matrix[0])): if matrix[i][j] == 0: inner.append(0) elif i == 0 or j == 0: inner.append(1) count += 1 else: c = 1 + min(dp[i - 1][j], min(inner[j - 1], dp[i - 1][j - 1])) inner.append(c) count += c dp.append(inner) return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: if not matrix: return 0 m, n = len(matrix), len(matrix[0]) diag = 0 mem = [(0) for _ in range(n)] res = 0 for i in range(n): mem[i] = matrix[0][i] res += mem[i] for i in range(1, m): for j in range(n): tmp = mem[j] if matrix[i][j] == 0: mem[j] = 0 elif j == 0: mem[j] = matrix[i][j] res += mem[j] else: mem[j] = min(diag, mem[j - 1], mem[j]) + 1 res += mem[j] diag = tmp return res
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def dobra(self, x, i, j, red_matrice): for k in range(i, i + red_matrice): for l in range(j, j + red_matrice): if x[k][l] == 0: return False return True def countSquares(self, matrix: List[List[int]]) -> int: m = len(matrix) n = len(matrix[0]) res = 0 for i in range(m): for j in range(n): for red_matrice in range(1, min(m, n) + 1): if i + red_matrice <= m and j + red_matrice <= n: if self.dobra(matrix, i, j, red_matrice): res += 1 else: break return res
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: if len(matrix) > 0: t = [ [(0) for i in range(len(matrix[0]) + 1)] for j in range(len(matrix) + 1) ] result = 0 for i in range(1, len(matrix) + 1): for j in range(1, len(matrix[0]) + 1): if matrix[i - 1][j - 1] == 0: t[i][j] == 0 else: t[i][j] = min(t[i - 1][j], t[i][j - 1], t[i - 1][j - 1]) + 1 result += t[i][j] for i in t: print(i) return result else: return 0
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR RETURN NUMBER VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m = len(matrix) n = len(matrix[0]) @functools.lru_cache(None) def dp(i, j): if matrix[i][j]: if 0 < i and 0 < j: return min(dp(i - 1, j - 1), dp(i - 1, j), dp(i, j - 1)) + 1 return 1 return 0 return sum([dp(i, j) for i in range(m) for j in range(n)])
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR IF NUMBER VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_CALL VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: sqrsCnt = 0 for y, row in enumerate(matrix): for x, val in enumerate(row): i, j = x, y while True: if i < 0 or j < 0: break if any(matrix[j][a] != 1 for a in range(i, x + 1)): break if any(matrix[b][i] != 1 for b in range(j, y + 1)): break if matrix[j][i] != 1: break sqrsCnt += 1 i, j = i - 1, j - 1 return sqrsCnt
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE NUMBER IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m, n = len(matrix), len(matrix[0]) res = 0 @lru_cache(None) def search(i, j): nonlocal res if i >= m or i < 0 or j >= n or j < 0: return 0 val = ( 1 + min(search(i - 1, j), search(i, j - 1), search(i - 1, j - 1)) ) * matrix[i][j] res += val return val search(m - 1, n - 1) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: dp = [[matrix[i][j] for j in range(len(matrix[0]))] for i in range(len(matrix))] for i in range(1, len(matrix)): for j in range(1, len(matrix[0])): if matrix[i][j] == 0: continue if dp[i - 1][j] != 0 and dp[i][j - 1] != 0 and dp[i - 1][j - 1] != 0: dp[i][j] = min(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]) + 1 count = 0 for row in dp: count += sum(row) return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR 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 VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def getsum(self, prefix, i, j): if i < 0 or j < 0: return 0 else: return prefix[min(len(prefix), i)][min(len(prefix[0]), j)] def countSquares(self, matrix: List[List[int]]) -> int: m = len(matrix) n = len(matrix[0]) dp = [] res = 0 for i in range(m): dp.append([0] * n) for i in range(m): if matrix[i][0] == 1: dp[i][0] = 1 res = res + 1 for j in range(1, n): if matrix[0][j] == 1: dp[0][j] = 1 res = res + 1 print(res) for i in range(1, m): for j in range(1, n): if matrix[i][j] > 0: dp[i][j] = ( min(min(dp[i - 1][j - 1], dp[i - 1][j]), dp[i][j - 1]) + 1 ) res = res + dp[i][j] return res
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: M = len(matrix) N = len(matrix[0]) dp = [[(0) for _ in range(N)] for _ in range(M)] dp = matrix[:][:] for i in range(1, M): for j in range(1, N): if matrix[i][j] == 1: dp[i][j] = min([dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]]) + 1 res = 0 for i in range(M): for j in range(N): if dp[i][j] > 0: for size in range(1, dp[i][j] + 1): res += 1 print(dp) return res
CLASS_DEF FUNC_DEF 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 ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
def make_partial(m, n, matrix): ans = [[(0) for __ in range(n + 1)] for _ in range(m + 1)] for i in range(m + 1): for j in range(n + 1): if i == 0 and j == 0: p = 0 else: p = ( matrix[i - 1][j - 1] + ans[i - 1][j] + ans[i][j - 1] - ans[i - 1][j - 1] ) ans[i][j] = p return ans def get_rect(partial, i0, j0, i1, j1): return partial[i1][j1] + partial[i0][j0] - partial[i0][j1] - partial[i1][j0] def count_squares(matrix): m, n = len(matrix), len(matrix[0]) partial = make_partial(m, n, matrix) ans = 0 for i in range(0, m): for j in range(0, n): max_w = min(m - i, n - j) for w in range(1, max_w + 1): if get_rect(partial, i, j, i + w, j + w) == w**2: ans += 1 else: break return ans class Solution: def countSquares(self, matrix: List[List[int]]) -> int: return count_squares(matrix)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN 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 VAR VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: rows = len(matrix) cols = len(matrix[0]) def traverse_subsquares(i, j): max_size = min(i, j) + 1 n_subsquares = 0 for i_base in range(i, i - max_size, -1): size = i - i_base + 1 ip = i_base for jp in range(j, j - size, -1): if not matrix[ip][jp]: return n_subsquares for ip in range(i_base + 1, i_base + size, 1): if not matrix[ip][jp]: return n_subsquares n_subsquares += 1 return n_subsquares total = 0 for i in range(rows): for j in range(cols): n = traverse_subsquares(i, j) total += n return total
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: mat = deepcopy(matrix) for i in range(0, len(mat)): for j in range(0, len(mat[0])): if mat[i][j] == 0: continue if i - 1 >= 0: top = mat[i - 1][j] else: top = 0 if j - 1 >= 0: left = mat[i][j - 1] else: left = -1 if i - 1 < 0 and j - 1 < 0: top_left = -1 else: top_left = mat[i - 1][j - 1] if top_left > 0 and left > 0 and top > 0: mat[i][j] = min(top_left, left, top) + 1 ret = 0 for i in range(0, len(mat)): for j in range(0, len(mat[0])): ret += mat[i][j] return ret
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL 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 VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: ans = 0 n = len(matrix) m = len(matrix[0]) pre = [[(0) for i in range(m + 1)] for j in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): if matrix[i - 1][j - 1] == 1: ans += 1 pre[i][j] = matrix[i - 1][j - 1] if i - 1 >= 0: pre[i][j] += pre[i - 1][j] if j - 1 >= 0: pre[i][j] += pre[i][j - 1] if i - 1 >= 0 and j - 1 >= 0: pre[i][j] -= pre[i - 1][j - 1] c = 2 while i - c >= 0 and j - c >= 0: if ( pre[i][j] - pre[i][j - c] - pre[i - c][j] + pre[i - c][j - c] == c**2 ): ans += 1 else: break c += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER 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 BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: if not matrix or not matrix[0]: return 0 ans = 0 topleft = [(0) for i in matrix[0]] totop = [(0) for i in matrix[0]] for i in range(len(matrix)): toleft, nextarea = 0, [(0) for i in range(len(matrix[0]) + 1)] for j in range(len(matrix[0])): if matrix[i][j] == 1: totop[j] += 1 toleft += 1 maxr = min(totop[j], toleft, topleft[j] + 1) nextarea[j + 1] = maxr ans += maxr else: totop[j] = 0 toleft = 0 nextarea[j + 1] = 0 topleft = nextarea return ans
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: rows = len(matrix) cols = len(matrix[0]) result = 0 for r in range(rows): for c in range(cols): if matrix[r][c] == 1: idx = 1 isTrue = True while isTrue and r + idx != rows and c + idx != cols: for h in range(idx + 1): if ( matrix[r + idx][c + h] != 1 or matrix[r + h][c + idx] != 1 ): isTrue = False break if isTrue: idx += 1 result += idx return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: if not matrix or not matrix[0]: return 0 res = 0 M = len(matrix) N = len(matrix[0]) for i in range(M): for j in range(N): if matrix[i][j] == 0: continue if i == 0 or j == 0: res += 1 else: add = ( min(matrix[i - 1][j], matrix[i][j - 1], matrix[i - 1][j - 1]) + 1 ) res += add matrix[i][j] = add return res
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: presum = [([0] * (len(matrix[0]) + 1)) for _ in range(len(matrix) + 1)] dp = [([0] * (len(matrix[0]) + 1)) for _ in range(len(matrix) + 1)] tot = 0 for r in range(len(matrix)): cur_row = [0] * (len(matrix[0]) + 1) for c in range(len(matrix[0])): cur_row[c] = cur_row[c - 1] + matrix[r][c] presum[r][c] = presum[r - 1][c] + cur_row[c] if matrix[r][c] == 0: dp[r][c] = 0 else: dp[r][c] = min(dp[r - 1][c - 1], dp[r - 1][c], dp[r][c - 1]) + 1 tot += dp[r][c] return tot
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: result = [matrix[0]] for i in range(1, len(matrix)): row = [matrix[i][0]] result.append(row) for j in range(1, len(matrix[i])): if not matrix[i][j]: row.append(0) else: row.append( 1 + min(result[i - 1][j], result[i][j - 1], result[i - 1][j - 1]) ) return sum(sum(r) for r in result)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: if not matrix: return 0 n = len(matrix) m = len(matrix[0]) squares = 0 for i in range(0, n): for j in range(0, m): if matrix[i][j] == 1: squares += self.get_squares(matrix, i, j, n, m) return squares def get_squares(self, matrix, i, j, n, m): count = 1 s = 2 while i + s <= n and j + s <= m: all_ones = True for k in range(i, i + s): for l in range(j, j + s): if matrix[k][l] == 0: return count count += 1 s += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m, n = len(matrix) if matrix else 0, len(matrix[0]) if matrix else 0 table = [([0] * n) for _ in range(m)] ans = 0 for i, j in product(list(range(m)), list(range(n))): if matrix[i][j] and j > 0 and i > 0: table[i][j] = ( min(table[i - 1][j], table[i][j - 1], table[i - 1][j - 1]) + 1 ) else: table[i][j] = matrix[i][j] ans += table[i][j] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: max_y = len(matrix) max_x = len(matrix[0]) for y in range(1, max_y): for x in range(1, max_x): if ( matrix[y][x] and matrix[y][x - 1] and matrix[y - 1][x] and matrix[y - 1][x - 1] ): matrix[y][x] = ( min(matrix[y][x - 1], matrix[y - 1][x], matrix[y - 1][x - 1]) + 1 ) row_sums = [sum(row) for row in matrix] return sum(row_sums)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m, n, count = len(matrix), len(matrix[0]), 0 size_matrix = [([0] * n) for j in range(m)] indices = [(-1, -1), (-1, 0), (0, -1)] def _in_range(i, j): return i >= 0 and i < m and j >= 0 and j < n for i in range(0, m): for j in range(0, n): if matrix[i][j] == 1: if i == 0 or j == 0: size_matrix[i][j] = matrix[i][j] if matrix[i][j] == 1: count += 1 else: minimum = min( [ size_matrix[x + i][y + j] for x, y in indices if _in_range(x + i, y + j) ] ) size_matrix[i][j] = minimum + 1 count += 1 + minimum return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR VAR
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.   Example 1: Input: matrix = [   [0,1,1,1],   [1,1,1,1],   [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7.   Constraints: 1 <= arr.length <= 300 1 <= arr[0].length <= 300 0 <= arr[i][j] <= 1
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m = len(matrix) n = len(matrix[0]) max_square_size = min(m, n) ones = [] for i in range(m): for j in range(n): if matrix[i][j] == 1: ones.append([i, j]) square_count = len(ones) for p in ones: for j in range(1, max_square_size): found_zero = False for rj in range(j + 1): for cj in range(j + 1): try: if matrix[p[0] + rj][p[1] + cj] == 0: found_zero = True break except IndexError: found_zero = True if found_zero: break if found_zero: break else: square_count += 1 return square_count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR IF VAR VAR NUMBER RETURN VAR VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): f = [[nums[0], nums[0]]] ans = nums[0] for i in range(1, len(nums)): f.append([0, 0]) f[i][0] = min(f[i - 1][0] * nums[i], f[i - 1][1] * nums[i], nums[i]) f[i][1] = max(f[i - 1][0] * nums[i], f[i - 1][1] * nums[i], nums[i]) ans = max(ans, f[i][1]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): maxP = nums[0] minP = nums[0] p = 1 result = nums[0] while p < len(nums): tmp1 = max(nums[p], nums[p] * maxP, nums[p] * minP) tmp2 = min(nums[p], nums[p] * maxP, nums[p] * minP) maxP = tmp1 minP = tmp2 print((nums[p], maxP, minP)) result = max(result, maxP) p += 1 return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): if not nums: return 0 res = premax = premin = nums[0] curmax = curmin = None for n in nums[1:]: curmax, curmin = max(premax * n, premin * n, n), min( premax * n, premin * n, n ) premax, premin = curmax, curmin res = max(res, curmax) return res
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NONE FOR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): if not nums: return 0 res = loc_min = loc_max = nums[0] for i in nums[1:]: if i < 0: loc_min, loc_max = loc_max, loc_min loc_min = min(i, loc_min * i) loc_max = max(i, loc_max * i) res = max(loc_min, loc_max, res) return res
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): dp_max = [0] * len(nums) dp_min = [0] * len(nums) res = dp_max[0] = dp_min[0] = nums[0] for i in range(1, len(nums)): dp_max[i] = max(nums[i], dp_max[i - 1] * nums[i], dp_min[i - 1] * nums[i]) dp_min[i] = min(nums[i], dp_max[i - 1] * nums[i], dp_min[i - 1] * nums[i]) res = max(res, dp_max[i]) return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): def prod(nums): prod = 1 for i in nums: prod *= i return prod def listsplit(ls1, index): result = [] st = -1 for i in index: if i == 0: st = i else: result.append(ls1[st + 1 : i]) st = i if st < len(ls1) - 1: result.append(ls1[st + 1 :]) return result if not nums: return 0 if len(nums) == 1: return nums[0] result = [] if 0 in nums: zeros = [i for i in range(len(nums)) if nums[i] == 0] sublist = listsplit(nums, zeros) result.append(0) else: sublist = [nums] sublist = [i for i in sublist if i] for i in sublist: if prod(i) < 0: negative = [j for j in range(len(i)) if i[j] < 0] left, right = negative[0], negative[-1] if len(i) == 1: result_t = i[0] elif left == 0 or right == len(i) - 1: result_t = max(prod(i[left + 1 :]), prod(i[:right])) else: left_p, right_p = prod(i[:left]), prod(i[right + 1 :]) if left_p <= right_p: result_t = prod(i[left + 1 :]) else: result_t = prod(i[:right]) else: result_t = prod(i) result.append(result_t) return max(result)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR LIST IF NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): max_val = -float("inf") product = 1 for num in nums: product *= num max_val = max(product, max_val) if num == 0: product = 1 product = 1 for num in nums[::-1]: product *= num max_val = max(product, max_val) if num == 0: product = 1 return max_val
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): glo_max = nums[0] imax = glo_max imin = glo_max i = 1 while i < len(nums): if nums[i] < 0: temp = imax imax = imin imin = temp imax = max(nums[i], nums[i] * imax) imin = min(nums[i], nums[i] * imin) glo_max = max(glo_max, imax) i += 1 return glo_max
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): if len(nums) == 1: return nums[0] else: result = big = small = nums[0] for i in nums[1:]: big, small = max(i, i * big, i * small), min(i, i * small, i * big) result = max(result, big) return result
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): maxPos = nums[0] maxNeg = -nums[0] p = 1 result = nums[0] while p < len(nums): if nums[p] >= 0: tmp1 = max(nums[p], nums[p] * maxPos) tmp2 = max(-nums[p], nums[p] * maxNeg) maxPos = tmp1 maxNeg = tmp2 else: tmp1 = max(nums[p], -nums[p] * maxNeg) tmp2 = max(-nums[p], -nums[p] * maxPos) maxPos = tmp1 maxNeg = tmp2 print((nums[p], maxPos, maxNeg)) result = max(result, maxPos) p += 1 return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): largestproduct = [] smallestproduct = [] n = len(nums) largestproduct.append(nums[0]) smallestproduct.append(nums[0]) for i in range(n - 1): lp = max( max(largestproduct[i] * nums[i + 1], smallestproduct[i] * nums[i + 1]), nums[i + 1], ) sp = min( min(largestproduct[i] * nums[i + 1], smallestproduct[i] * nums[i + 1]), nums[i + 1], ) largestproduct.append(lp) smallestproduct.append(sp) sol = largestproduct[0] for i in range(n): sol = max(sol, largestproduct[i]) return sol
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): self._prod_cache = {} m_value = None if len(nums) == 1: return nums[0] if len(nums) == 0: return 0 for i in range(len(nums)): a_value = self.get_max_prod(nums, i, 1) if m_value is None or a_value > m_value: m_value = a_value return m_value def get_max_prod(self, nums, i, acc_p): if i < 0: return None if (i, acc_p) in self._prod_cache: return self._prod_cache[i, acc_p] val = nums[i] if i > 0: max_p = max(self.get_max_prod(nums, i - 1, acc_p * val), acc_p * val) else: max_p = acc_p * val self._prod_cache[i, acc_p] = max_p return max_p
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NONE IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NONE VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NONE IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): n = len(nums) dp = [[nums[0], nums[0]]] * n ans = nums[0] for i in range(1, n): temp = [dp[i - 1][0] * nums[i], dp[i - 1][1] * nums[i], nums[i]] dp[i][0], dp[i][1] = max(temp), min(temp) if dp[i][0] > ans: ans = dp[i][0] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST LIST VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution: def maxProduct(self, nums): n = len(nums) if n == 0: return 0 if n == 1: return nums[0] ans = nums[0] l = 0 r = n - 1 prod = 1 i = 0 while i < n: if nums[i] == 0: zero_idx = i return max( self.maxProduct(nums[zero_idx + 1 :]), max(0, self.maxProduct(nums[0:zero_idx])), ) else: prod = prod * nums[i] i = i + 1 if prod > 0: return prod prod_right = prod max_prod_right = prod prod_left = prod max_prod_left = prod if prod < 0: while 0 <= r: prod_right = prod_right // nums[r] r = r - 1 if prod_right > max_prod_right: if prod_right > 0: max_prod_right = prod_right break else: max_prod_right = prod_right print(max_prod_left) while l < n: prod_left = prod_left // nums[l] l = l + 1 if prod_left > max_prod_left: if prod_left > 0: max_prod_left = prod_left break else: max_prod_left = prod_left return max(max_prod_left, max_prod_right)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER WHILE NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution(object): def maxProduct(self, nums): if len(nums) == 0: return nums if len(nums) == 1: return nums[0] curr_min = curr_max = best_max = nums[0] for i in range(1, len(nums)): curr_min, curr_max = min( nums[i], nums[i] * curr_min, nums[i] * curr_max ), max(nums[i], nums[i] * curr_min, nums[i] * curr_max) best_max = max(curr_max, best_max) return best_max
CLASS_DEF VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: N = len(A) def makeStack(S): ret = [None] * N stack = [] for i in S: while stack and i > stack[-1]: ret[stack.pop()] = i stack.append(i) return ret S = sorted(list(range(N)), key=lambda i: A[i]) odd = makeStack(S) S = sorted(list(range(N)), key=lambda i: A[i], reverse=True) even = makeStack(S) dp = [[0, 0]] * N dp[N - 1] = [1, 1] for i in range(N - 2, -1, -1): dp[i] = [0, 0] if odd[i] is not None: dp[i][1] = dp[odd[i]][0] if even[i] is not None: dp[i][0] = dp[even[i]][1] cnt = 0 for i in range(N): cnt += dp[i][1] return cnt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER IF VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: N = len(A) def make(B): ans = [None] * N stack = [] for i in B: while stack and i > stack[-1]: ans[stack.pop()] = i stack.append(i) return ans B = sorted(list(range(N)), key=lambda i: A[i]) oddnext = make(B) print((oddnext, B)) B.sort(key=lambda i: -A[i]) evennext = make(B) dest = len(A) - 1 cnt = 0 for idx in range(N): jump, ptr = 1, idx while ptr != None: if ptr == dest: cnt += 1 break jump, ptr = (0, oddnext[ptr]) if jump == 1 else (1, evennext[ptr]) return cnt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR NONE IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR RETURN VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: n = len(A) next_higher, next_lower = [0] * n, [0] * n stack = [] for a, i in sorted([a, i] for i, a in enumerate(A)): while stack and stack[-1] < i: next_higher[stack.pop()] = i stack.append(i) for a, i in sorted([-a, i] for i, a in enumerate(A)): while stack and stack[-1] < i: next_lower[stack.pop()] = i stack.append(i) higher, lower = [0] * n, [0] * n higher[-1] = 1 lower[-1] = 1 for i in range(n - 1)[::-1]: higher[i] = lower[next_higher[i]] lower[i] = higher[next_lower[i]] return sum(higher)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: N = len(A) def makeorder(sortedIdx): ans = [-1] * N stack = [] for i in sortedIdx: while stack and i > stack[-1]: ans[stack[-1]] = i stack.pop() stack.append(i) return ans sortedIdx = sorted(range(N), key=lambda i: A[i]) oddNext = makeorder(sortedIdx) sortedIdx = sorted(range(N), key=lambda i: -A[i]) evenNext = makeorder(sortedIdx) odd = [False] * N even = [False] * N odd[N - 1] = True even[N - 1] = True for i in range(N - 2, -1, -1): if oddNext[i] != -1: odd[i] = even[oddNext[i]] if evenNext[i] != -1: even[i] = odd[evenNext[i]] return sum(odd)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: n = len(A) odd_jump = [-1] * n even_jump = [-1] * n final_odd = [False] * n final_even = [False] * n final_odd[-1] = True final_even[-1] = True stack = [] for n, i in sorted((n, i) for i, n in enumerate(A)): while stack and stack[-1] < i: odd_jump[stack.pop()] = i stack.append(i) stack = [] for n, i in sorted((-n, i) for i, n in enumerate(A)): while stack and stack[-1] < i: even_jump[stack.pop()] = i stack.append(i) for i in range(len(A) - 2, -1, -1): print(i) if odd_jump[i] != -1: final_odd[i] = final_even[odd_jump[i]] if even_jump[i] != -1: final_even[i] = final_odd[even_jump[i]] return sum(final_odd)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: next_high = [(0) for _ in range(len(A))] next_low = [(0) for _ in range(len(A))] stack = [] for val, i in sorted([(val, i) for i, val in enumerate(A)]): while stack and stack[-1] < i: next_high[stack.pop()] = i stack.append(i) stack = [] for val, i in sorted([(-val, i) for i, val in enumerate(A)]): while stack and stack[-1] < i: next_low[stack.pop()] = i stack.append(i) dp = [[(False) for _ in range(len(A))] for _ in range(2)] dp[0][-1] = dp[1][-1] = True for i in range(len(A) - 2, -1, -1): dp[0][i] = dp[1][next_high[i]] dp[1][i] = dp[0][next_low[i]] return dp[0].count(1)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: index = 0 n = len(A) next_high = [0] * n next_low = [0] * n stack = [] B = [[i, a] for i, a in enumerate(A)] B.sort(key=lambda x: x[1]) for i, a in B: while stack and stack[-1] < i: next_high[stack.pop()] = i stack.append(i) B.sort(key=lambda x: -x[1]) stack = [] for i, a in B: while stack and stack[-1] < i: next_low[stack.pop()] = i stack.append(i) higher = [0] * n lower = [0] * n higher[-1] = lower[-1] = 1 for i in range(n - 2, -1, -1): higher[i] = lower[next_high[i]] lower[i] = higher[next_low[i]] return sum(higher)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: memo = {} next_memo = {} def get_next_move(self, array, index, odd): if index == len(array) - 1: return index if (index, odd) not in list(Solution.next_memo.keys()): if odd: min_geq = 1e99 next_index = -1 for i in range(index + 1, len(array)): if array[i] == array[index]: next_index = i break if array[i] > array[index]: if array[i] < min_geq: min_geq = array[i] next_index = i else: max_leq = -1e99 next_index = -1 for i in range(index + 1, len(array)): if array[i] == array[index]: next_index = i break if array[i] < array[index]: if array[i] > max_leq: max_leq = array[i] next_index = i Solution.next_memo[index, odd] = next_index return Solution.next_memo[index, odd] def can_get_to_end(self, array, index, odd): if (index, odd) not in list(Solution.memo.keys()): next_index = self.get_next_move(array, index, odd) if next_index == len(array) - 1: Solution.memo[index, odd] = 1 elif next_index == -1: Solution.memo[index, odd] = 0 else: Solution.memo[index, odd] = self.can_get_to_end( array, next_index, not odd ) return Solution.memo[index, odd] def other(self, a): b = list(sorted(list(range(len(a))), key=lambda i: a[i])) c = list(sorted(list(range(len(a))), key=lambda i: -a[i])) odd_next = {} even_next = {} i = 1 while i < len(b): j = i while j < len(b) - 1 and b[j] <= b[i - 1]: j += 1 if b[j] > b[i - 1]: odd_next[b[i - 1]] = b[j] i += 1 i = 1 while i < len(c): j = i while j < len(c) - 1 and c[j] <= c[i - 1]: j += 1 if c[j] > c[i - 1]: even_next[c[i - 1]] = c[j] i += 1 paths = 0 for start in list(odd_next.keys()): index = start can_go = True odd = True while can_go and index != len(a) - 1: if odd: if index in list(odd_next.keys()): index = odd_next[index] else: can_go = False elif index in list(even_next.keys()): index = even_next[index] else: can_go = False odd = not odd if index == len(a) - 1: paths += 1 if len(a) - 1 not in list(odd_next.keys()): paths += 1 return paths def oddEvenJumps(self, a: List[int]) -> int: return self.other(a)
CLASS_DEF ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: n = len(A) B = sorted(range(n), key=A.__getitem__) odds = [-1] * n stack = [] for i in B: while stack and i > stack[-1]: odds[stack.pop()] = i stack.append(i) evens = [-1] * n B = sorted(range(n), key=A.__getitem__, reverse=True) stack.clear() for i in B: while stack and i > stack[-1]: evens[stack.pop()] = i stack.append(i) dp = [[0, 0] for i in range(n)] dp[n - 1] = [1, 1] result = 1 for i in range(n - 2, -1, -1): if odds[i] != -1 and dp[odds[i]][0]: dp[i][1] = 1 result += 1 if evens[i] != -1 and dp[evens[i]][1]: dp[i][0] = 1 return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER RETURN VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: def findNextHighestIdx(B: List[int]) -> List[int]: next_idx_list = [None] * len(B) stack = [] for i in B: while stack and stack[-1] < i: next_idx_list[stack.pop()] = i stack.append(i) return next_idx_list N = len(A) B = sorted(range(N), key=lambda i: A[i]) oddnextidx = findNextHighestIdx(B) B.sort(key=lambda i: -A[i]) evennextidx = findNextHighestIdx(B) odd = [False] * N odd[N - 1] = True even = [False] * N even[N - 1] = True for i in range(N - 2, -1, -1): if oddnextidx[i] is not None: odd[i] = even[oddnextidx[i]] if evennextidx[i] is not None: even[i] = odd[evennextidx[i]] return sum(odd)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NONE ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: odd_jump = {} even_jump = {} index_ele = {index: ele for index, ele in enumerate(A)} sorted_A = sorted(list(range(len(A))), key=lambda x: A[x]) even_sorted_A = sorted(list(range(len(A))), key=lambda x: (A[x], -x)) stack = [] for i in range(len(A)): idx = sorted_A[i] while stack and stack[-1] < idx: old_index = stack.pop() odd_jump[old_index] = idx stack.append(idx) for idx in stack: odd_jump[idx] = -1 stack = [] for i in range(len(A) - 1, -1, -1): idx = even_sorted_A[i] while stack and stack[-1] < idx: old_index = stack.pop() even_jump[old_index] = idx stack.append(idx) for idx in stack: even_jump[idx] = -1 self.ans = 0 @lru_cache(None) def helper(index, odd): if index == len(A) - 1: return 1 ans = 0 if index == -1: return 0 if odd: ans += helper(odd_jump[index], not odd) else: ans += helper(even_jump[index], not odd) return ans ans = 0 for i in range(len(A)): ans += helper(i, True) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: n = len(A) even = {} odd = {} stack = [] ascending_idx = sorted([i for i in range(n)], key=lambda i: A[i]) for idx in ascending_idx: while stack and idx > stack[-1]: odd[stack.pop()] = idx stack.append(idx) stack = [] descending_idx = sorted([i for i in range(n)], key=lambda i: -A[i]) for idx in descending_idx: while stack and idx > stack[-1]: even[stack.pop()] = idx stack.append(idx) del stack res = 0 for key in list(odd.keys()): if odd[key] == n - 1: res += 1 else: nxt = odd[key] jumps, other = 0, even while nxt in other: jumps += 1 nxt = even[nxt] if jumps % 2 != 0 else odd[nxt] if nxt == n - 1: res += 1 break other = even if jumps % 2 == 0 else odd return res + 1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR RETURN BIN_OP VAR NUMBER VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A): n = len(A) next_higher, next_lower = [0] * n, [0] * n stack = [] for a, i in sorted([[a, i] for i, a in enumerate(A)]): while stack and stack[-1] < i: next_higher[stack.pop()] = i stack.append(i) stack = [] for a, i in sorted([[-a, i] for i, a in enumerate(A)]): while stack and stack[-1] < i: next_lower[stack.pop()] = i stack.append(i) def jump(index, jump_num): if index == n - 1: return 1 next_index = 0 if jump_num % 2 == 1: next_index = next_higher[index] else: next_index = next_lower[index] return jump(next_index, jump_num + 1) if next_index else 0 result = 0 for start in range(n): result += jump(start, 1) return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def getIndex(self, val, arr): start = 0 end = len(arr) - 1 while start < end: mid = start + (end - start) // 2 if arr[mid][0] < val: start = mid + 1 else: end = mid if arr[start][0] < val: start += 1 return start def oddEvenJumps(self, A: List[int]) -> int: items = len(A) even = [False] * items odd = [False] * items goodindex = 1 even[-1], odd[-1] = True, True vals = [[A[-1], items - 1]] for index in range(items - 2, -1, -1): insind = self.getIndex(A[index], vals) if insind < len(vals): eind = vals[insind][1] odd[index] = even[eind] eq = False if insind < len(vals) and vals[insind][0] == A[index]: oind = vals[insind][1] even[index] = odd[oind] eq = True elif insind > 0: oind = vals[insind - 1][1] even[index] = odd[oind] if eq: vals[insind][1] = index else: vals.insert(insind, [A[index], index]) if odd[index]: goodindex += 1 return goodindex
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def populate_jumps(self, sorted_values_idx, jumps): stack = [] for jump_to in sorted_values_idx: while stack and stack[-1] < jump_to: jump_from = stack.pop() jumps[jump_from] = jump_to stack.append(jump_to) def oddEvenJumps(self, A: List[int]) -> int: odd_jump = [None] * len(A) even_jump = [None] * len(A) num_idx_array = [(num, idx) for idx, num in enumerate(A)] sorted_idxs = list(map(lambda x: x[1], list(sorted(num_idx_array)))) reverse_sorted_idxs = list( map( lambda x: x[1], list(sorted(num_idx_array, key=lambda x: (-x[0], x[1]))) ) ) self.populate_jumps(sorted_idxs, odd_jump) self.populate_jumps(reverse_sorted_idxs, even_jump) odd_dp = [False] * len(A) even_dp = [False] * len(A) odd_dp[-1] = True even_dp[-1] = True result = 1 print(odd_dp) for start_idx in range(len(A) - 2, -1, -1): if odd_jump[start_idx] is not None and even_dp[odd_jump[start_idx]] is True: odd_dp[start_idx] = True result += 1 if ( even_jump[start_idx] is not None and odd_dp[even_jump[start_idx]] is True ): even_dp[start_idx] = True return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NONE VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NONE VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: N = len(A) aS = [(val, i) for i, val in enumerate(A)] aS.sort(key=lambda x: x[0]) a_map = {} for id, (val, i) in enumerate(aS): a_map[i] = id dS = [(val, i) for i, val in enumerate(A)] dS.sort(key=lambda x: x[0], reverse=True) d_map = {} for id, (val, i) in enumerate(dS): d_map[i] = id dp = [[0, 0]] * N dp[N - 1] = [1, 1] for i in range(N - 2, -1, -1): dp[i] = [0, 0] id = d_map[i] while id + 1 < N: val, j = dS[id + 1] if j > i: dp[i][0] = dp[j][1] break id = id + 1 id = a_map[i] while id + 1 < N: val, j = aS[id + 1] if j > i: dp[i][1] = dp[j][0] break id = id + 1 cnt = 0 for i in range(N): cnt += dp[i][1] return cnt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: ALen = len(A) moves = [[None, None] for i in range(ALen)] sortedNum = [[A[-1], ALen - 1]] def insertAt(x): l = 0 r = len(sortedNum) - 1 if x < sortedNum[l][0]: return 0 if x > sortedNum[r][0]: return r + 1 while r > l: m = (r + l) // 2 if x > sortedNum[m][0]: l = m + 1 elif x < sortedNum[m][0]: r = m else: return m return l for i in range(ALen - 2, -1, -1): i1 = insertAt(A[i]) if i1 < len(sortedNum) and sortedNum[i1][0] == A[i]: moves[i] = [sortedNum[i1][1], sortedNum[i1][1]] sortedNum[i1][1] = i else: if i1 == 0: moves[i][0] = sortedNum[0][1] moves[i][1] = None elif i1 == len(sortedNum): moves[i][0] = None moves[i][1] = sortedNum[-1][1] else: moves[i] = [sortedNum[i1][1], sortedNum[i1 - 1][1]] sortedNum.insert(i1, [A[i], i]) goodMoveList = [[False, False] for i in range(ALen)] goodMoveList[-1] = [True, True] ans = 1 for i in range(ALen - 2, -1, -1): if moves[i][0] != None and goodMoveList[moves[i][0]][1]: goodMoveList[i][0] = True ans += 1 if moves[i][1] != None and goodMoveList[moves[i][1]][0]: goodMoveList[i][1] = True return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NONE NONE VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR LIST VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NONE IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NONE ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR LIST VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NONE VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NONE VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER RETURN VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: nums_in = sorted((num, index) for index, num in enumerate(A)) print(nums_in) index_in = self.helper(nums_in) nums_de = sorted((-num, index) for index, num in enumerate(A)) index_de = self.helper(nums_de) greater = [False] * len(A) smaller = [False] * len(A) greater[-1] = True smaller[-1] = True for i in range(len(A) - 2, -1, -1): greater[i] = smaller[index_in[i]] smaller[i] = greater[index_de[i]] return sum(greater) def helper(self, nums): result = [0] * len(nums) queue = list() for _, index in nums: while queue and index > queue[-1]: result[queue.pop()] = index queue.append(index) return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def findIndex(self, arr, val): if not arr: return 0 start = 0 end = len(arr) - 1 while start < end: mid = start + (end - start) // 2 if arr[mid][0] < val: start = mid + 1 else: end = mid if arr[start][0] < val: start += 1 return start def oddEvenJumps(self, A: List[int]) -> int: n = len(A) odd = [False] * n even = [False] * n odd[-1] = even[-1] = True arr = [[A[-1], n - 1]] count = 1 for index in range(n - 2, -1, -1): val = A[index] insindex = self.findIndex(arr, val) evenindex = None if insindex < len(arr): odd[index] = even[arr[insindex][1]] if arr[insindex][0] == val: evenindex = arr[insindex][1] elif insindex > 0: evenindex = arr[insindex - 1][1] else: evenindex = arr[insindex - 1][1] if evenindex is not None: even[index] = odd[evenindex] if insindex < len(arr) and arr[insindex][0] == val: arr[insindex][1] = index else: arr.insert(insindex, [val, index]) if odd[index]: count += 1 return count
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST LIST VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NONE ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: ans = 0 def bfs(self, index, even, odd, curr): if index == len(even) - 1: self.ans += 1 return if curr % 2 == 0: if even[index] != -1: self.bfs(even[index], even, odd, curr + 1) elif odd[index] != -1: self.bfs(odd[index], even, odd, curr + 1) def oddEvenJumps(self, A: List[int]) -> int: self.ans = 0 s = sorted(list(range(len(A))), key=lambda i: A[i]) a = [-1] * len(A) b = [-1] * len(A) stack = [] for val in s: while len(stack) > 0 and stack[-1] < val: a[stack.pop()] = val stack.append(val) s = sorted(list(range(len(A))), key=lambda i: A[i], reverse=True) stack = [] for val in s: while len(stack) > 0 and stack[-1] < val: b[stack.pop()] = val stack.append(val) for i, val in enumerate(s): self.bfs(i, b, a, 1) return self.ans
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN IF BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution(object): def oddEvenJumps(self, A): lenOfA = len(A) sortedA = sorted(range(lenOfA), key=lambda key: A[key]) def make(array): toNextIndex = [None] * lenOfA stack = [] for i in array: while stack and i > stack[-1]: toNextIndex[stack.pop()] = i stack.append(i) return toNextIndex nextOdd = make(sortedA) sortedA.sort(key=lambda key: -A[key]) nextEven = make(sortedA) def update(memo, footPrints): for footPrint in footPrints: memo["%d-%s" % (footPrint[0], footPrint[1])] = None success = {} failed = {} output = 0 for startIndex in range(lenOfA): index = startIndex jump = 1 footPrints = set([]) while index < lenOfA: if index == lenOfA - 1: update(success, footPrints) output += 1 break isOdd = jump % 2 == 1 cacheKey = "%d-odd" % index if isOdd else "%d-even" % index if cacheKey in success: output += 1 break elif cacheKey in failed: break if isOdd: nextIndex = nextOdd[index] if nextIndex is None: update(failed, footPrints) break footPrints.add((index, "odd")) else: nextIndex = nextEven[index] if nextIndex is None: update(failed, footPrints) break footPrints.add((index, "even")) index = nextIndex jump += 1 return output
CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR ASSIGN VAR BIN_OP STRING VAR NUMBER VAR NUMBER NONE ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST WHILE VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP STRING VAR BIN_OP STRING VAR IF VAR VAR VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR NUMBER RETURN VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: sortA = sorted(list(range(len(A))), key=lambda i: A[i]) oddStack = self.makeStack(sortA) oddStack[-1] = len(A) - 1 sortA = sorted(list(range(len(A))), key=lambda i: A[i], reverse=True) evenStack = self.makeStack(sortA) evenStack[-1] = len(A) - 1 print(("evenstakc", evenStack)) print(("oddsatck", oddStack)) odd = [False] * len(A) even = [False] * len(A) odd[-1] = True even[-1] = True print(("len odd", len(odd))) print(("len even", len(even))) for i in range(len(A) - 2, -1, -1): print(("i is", i)) if oddStack[i]: odd[i] = even[oddStack[i]] if evenStack[i]: even[i] = odd[evenStack[i]] print(odd) print(even) return sum(odd) def makeStack(self, sortA): stack = [None] * len(sortA) res = [] for i in sortA: while res and i > res[-1]: stack[res.pop()] = i res.append(i) return stack
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, B: List[int]) -> int: A = [[v, i] for i, v in enumerate(B)] A.sort() nextHigher = [len(A)] * (len(A) + 1) nextSmaller = [len(A)] * (len(A) + 1) stack = collections.deque() for v, i in A: while stack and stack[-1] < i: nextHigher[stack.pop()] = i stack.append(i) stack = collections.deque() A = [[-v, i] for i, v in enumerate(B)] A.sort() for v, i in A: while stack and stack[-1] < i: nextSmaller[stack.pop()] = i stack.append(i) dp = [[False, False] for _ in range(len(A) + 1)] dp[-2] = [True, True] res = 1 for i in range(len(A) - 2, -1, -1): nH, nL = nextHigher[i], nextSmaller[i] dp[i][0] = dp[nL][1] dp[i][1] = dp[nH][0] res += dp[i][1] return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: nextHigher = [0] * len(A) nextLower = [0] * len(A) validOdd = [0] * len(A) validEven = [0] * len(A) validOdd[-1] = 1 validEven[-1] = 1 stack = [] for a, i in sorted([a, i] for i, a in enumerate(A)): while stack and stack[-1] < i: nextHigher[stack.pop()] = i stack.append(i) stack = [] for a, i in sorted([-a, i] for i, a in enumerate(A)): while stack and stack[-1] < i: nextLower[stack.pop()] = i stack.append(i) for index in reversed(range(len(A) - 1)): validOdd[index] = validEven[nextHigher[index]] validEven[index] = validOdd[nextLower[index]] return sum(validOdd)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: l = len(A) idxs = sorted(range(l), key=lambda i: A[i]) odds = [-1] * l stack = [] for i in idxs: while stack and stack[-1] < i: odds[stack.pop()] = i stack.append(i) idxs = sorted(range(l), key=lambda i: -A[i]) evens = [-1] * l stack = [] for i in idxs: while stack and stack[-1] < i: evens[stack.pop()] = i stack.append(i) reachable = [[-1, -1] for _ in range(l)] reachable[-1] = [1, 1] def helper(i, j): if reachable[i][j] >= 0: return reachable[i][j] if j == 0: reachable[i][j] = 0 if odds[i] == -1 else helper(odds[i], 1) else: reachable[i][j] = 0 if evens[i] == -1 else helper(evens[i], 0) return reachable[i][j] return sum(helper(i, 0) for i in range(l))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER LIST NUMBER NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: N = len(A) nextSmallestLarger = [-1] * N indexes = sorted(range(N), key=lambda i: A[i]) stk = [] for i in indexes: while stk and i > stk[-1]: nextSmallestLarger[stk.pop()] = i stk.append(i) nextLargestSmaller = [-1] * N indexes = sorted(range(N), key=lambda i: -A[i]) stk = [] for i in indexes: while stk and i > stk[-1]: nextLargestSmaller[stk.pop()] = i stk.append(i) @lru_cache(None) def jump(i, odd): if i >= N - 1: return True if odd: return ( False if nextSmallestLarger[i] == -1 else jump(nextSmallestLarger[i], False) ) else: return ( False if nextLargestSmaller[i] == -1 else jump(nextLargestSmaller[i], True) ) return sum(jump(i, True) for i in range(N))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR RETURN VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR
You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value.  If there are multiple such indexes j, you can only jump to the smallest such index j. (It may be the case that for some index i, there are no legal jumps.) A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.) Return the number of good starting indexes.   Example 1: Input: [10,13,12,14,15] Output: 2 Explanation: From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more. From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more. From starting index i = 3, we can jump to i = 4, so we've reached the end. From starting index i = 4, we've reached the end already. In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps. Example 2: Input: [2,3,1,1,4] Output: 3 Explanation: From starting index i = 0, we make jumps to i = 1, i = 2, i = 3: During our 1st jump (odd numbered), we first jump to i = 1 because A[1] is the smallest value in (A[1], A[2], A[3], A[4]) that is greater than or equal to A[0]. During our 2nd jump (even numbered), we jump from i = 1 to i = 2 because A[2] is the largest value in (A[2], A[3], A[4]) that is less than or equal to A[1]. A[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3. During our 3rd jump (odd numbered), we jump from i = 2 to i = 3 because A[3] is the smallest value in (A[3], A[4]) that is greater than or equal to A[2]. We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good. In a similar manner, we can deduce that: From starting index i = 1, we jump to i = 4, so we reach the end. From starting index i = 2, we jump to i = 3, and then we can't jump anymore. From starting index i = 3, we jump to i = 4, so we reach the end. From starting index i = 4, we are already at the end. In total, there are 3 different starting indexes (i = 1, i = 3, i = 4) where we can reach the end with some number of jumps. Example 3: Input: [5,1,3,4,2] Output: 3 Explanation: We can reach the end from starting indexes 1, 2, and 4.   Note: 1 <= A.length <= 20000 0 <= A[i] < 100000
class Solution: def oddEvenJumps(self, A: List[int]) -> int: n = len(A) idxs_sorted_by_value = sorted(range(n), key=lambda i: A[i]) odd_next_hops = self.get_next_hops(idxs_sorted_by_value) idxs_sorted_by_value.sort(key=lambda i: -A[i]) even_next_hops = self.get_next_hops(idxs_sorted_by_value) odd, even = [False] * n, [False] * n odd[-1], even[-1] = True, True for i in reversed(range(n - 1)): odd_next_hop, even_next_hop = odd_next_hops[i], even_next_hops[i] if odd_next_hop: odd[i] = even[odd_next_hop] if even_next_hop: even[i] = odd[even_next_hop] return sum(odd) def get_next_hops(self, idxs_sorted_by_value): next_hop = [None] * len(idxs_sorted_by_value) stack = [] for i in idxs_sorted_by_value: while stack and stack[-1] < i: next_hop[stack.pop()] = i stack.append(i) print(stack) print(next_hop) return next_hop
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR