description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: row = len(mat) col = len(mat[0]) res = 0 def calculate1dArray(arr): ans = 0 pre = 0 for i, a in enumerate(arr): if a == 1: pre += 1 ans += pre else: pre = 0 return ans for a in range(row): arr = [1] * col for b in range(a, row): for c in range(col): arr[c] &= mat[b][c] res += calculate1dArray(arr) 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 FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: M, N = len(mat), len(mat[0]) res = 0 for up in range(M): h = [(1) for i in range(N)] for down in range(up, M): for col in range(N): h[col] &= mat[down][col] res += self.oneArray(h) return res def oneArray(self, arr): res = length = 0 for a in arr: length = 0 if a == 0 else length + 1 res += length return res
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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: nrows = len(mat) ncols = len(mat[0]) for i in range(nrows): for j in range(ncols): if mat[i][j]: if j > 0: mat[i][j] = mat[i][j - 1] + 1 cnt = 0 for i in range(nrows): for j in range(ncols): if mat[i][j]: row = i submatrix_width = mat[i][j] while row < nrows and mat[row][j]: submatrix_width = min(submatrix_width, mat[row][j]) cnt += submatrix_width row += 1 return cnt
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 VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: res = 0 m, n = len(mat), len(mat[0]) def v1d(h): ans = 0 length = 0 for v in h: length = length + 1 if v else 0 ans += length return ans for i in range(m): h = [1] * n for idown in range(i, m): for j in range(n): h[j] &= mat[idown][j] res += v1d(h) return res def numSubmat_dp_style(self, mat: List[List[int]]) -> int: allones = set() m, n = len(mat), len(mat[0]) q = collections.deque() for i, j in itertools.product(range(m), range(n)): if mat[i][j]: q.append((i, j, 1, 1)) allones.add((i, j, 1, 1)) while q: i, j, r, c = q.popleft() if i + r < m and (i + r, j, 1, c) in allones: q.append((i, j, r + 1, c)) allones.add((i, j, r + 1, c)) if j + c < n and (i, j + c, r, 1) in allones: q.append((i, j, r, c + 1)) allones.add((i, j, r, c + 1)) return len(allones)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) left = [[(0) for j in range(n + 1)] for i in range(m + 1)] for i in range(1, len(left)): for j in range(1, len(left[0])): if mat[i - 1][j - 1] == 1: left[i][j] = left[i][j - 1] + 1 res = 0 for i in range(len(mat)): for j in range(len(mat[0])): minlen = float("inf") for k in range(i, -1, -1): minlen = min(minlen, left[k + 1][j + 1]) res += minlen 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 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 BIN_OP 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 ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) res = 0 for i in range(len(mat)): for j in range(len(mat[0])): if mat[i][j] == 1: right_edge = n for res_i in range(i, m): if mat[res_i][j] == 0: break for res_j in range(j, right_edge): if mat[res_i][res_j] == 1: res += 1 else: right_edge = min(right_edge, res_j) break return res
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 FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: a = mat m, n = len(a), len(a[0]) def hist(h): ret = [0] * n s = [] for i, x in enumerate(h): while s and h[s[-1]] >= x: s.pop() if s: j = s[-1] ret[i] = ret[j] + (i - j) * x else: ret[i] = (i + 1) * x s.append(i) return sum(ret) h = [0] * n ans = 0 for row in a: for j in range(n): h[j] = row[j] + h[j] if row[j] else 0 ans += hist(h) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: res, lr, lc = 0, len(mat), len(mat[0]) if mat else 0 for i in range(lr): for j in range(lc): if mat[i][j]: cur, x, limit = 0, i, lc while x < lr: y = j if not mat[x][y]: break while y < limit: if not mat[x][y]: limit = y break cur, y = cur + 1, y + 1 x += 1 res += cur return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR WHILE VAR VAR ASSIGN VAR VAR IF VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: n, m = len(mat), len(mat[0]) accs = [] for i in range(n): acc = [0] for j in range(m): acc.append(acc[-1] + mat[i][j]) accs.append(acc) ans = 0 for i in range(m): for j in range(i, m): cnt = 0 for k in range(n): if accs[k][j + 1] - accs[k][i] == j - i + 1: cnt += 1 else: ans += cnt * (cnt + 1) // 2 cnt = 0 ans += cnt * (cnt + 1) // 2 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) n = len(mat[0]) heights = [0] * n count = 0 for i in range(m): for j in range(n): if mat[i][j] == 0: heights[j] = 0 else: heights[j] += 1 for j in range(n): k = j smallest_height = heights[k] while k > -1 and heights[k] > 0: smallest_height = min(smallest_height, heights[k]) count += smallest_height k -= 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: ans = 0 for i in range(len(mat)): for j in range(len(mat[0])): row, col = 0, 0 if mat[i][j] == 1: while row < len(mat) - i and mat[i + row][j] == 1: row += 1 while col < len(mat[0]) - j and mat[i][j + col] == 1: col += 1 cnt, col_min = 0, col for ii in range(row): for jj in range(min(col_min, col)): if mat[i + ii][j + jj] == 1: cnt += 1 else: col_min = jj break print((i, j, cnt)) ans += cnt return ans
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 ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) up = [([0] * n) for _ in range(m)] count = 0 for i in range(m): for j in range(n): if not mat[i][j]: continue if i == 0: up[i][j] = 1 else: up[i][j] = up[i - 1][j] + 1 k = j min_height = up[i][j] while k >= 0 and mat[i][k]: min_height = min(min_height, up[i][k]) count += min_height k -= 1 return count
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 IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: h, w = len(mat), len(mat[0]) dp_acc_of_1 = [[(0) for _ in range(w)] for _ in range(h)] for y in range(h): for x in range(w): if x == 0: dp_acc_of_1[y][x] = mat[y][x] elif mat[y][x] == 1: dp_acc_of_1[y][x] = dp_acc_of_1[y][x - 1] + 1 counter_of_rectangle = 0 for y in range(h): for x in range(w): minimum_width = dp_acc_of_1[y][x] for h_idx in range(y, -1, -1): minimum_width = min(minimum_width, dp_acc_of_1[h_idx][x]) counter_of_rectangle += minimum_width if minimum_width == 0: break return counter_of_rectangle
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 VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if not mat: return 0 rows = len(mat) cols = len(mat[0]) n = 0 for top in range(rows): for left in range(cols): bottom = rows for right in range(left, cols): scan = top while scan < bottom and mat[scan][right]: scan += 1 bottom = scan n += bottom - top if bottom == top: break return n
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 VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) left_ones = [([0] * n) for _ in range(m)] for i in range(m): for j in range(n): if mat[i][j] == 0: left_ones[i][j] = 0 else: left_ones[i][j] = 1 if j > 0: left_ones[i][j] += left_ones[i][j - 1] res = 0 for j in range(n): total = 0 stack = [] for i in range(m): height = 1 total += left_ones[i][j] while stack and stack[-1][0] > left_ones[i][j]: total -= stack[-1][1] * (stack[-1][0] - left_ones[i][j]) height += stack[-1][1] stack.pop() res += total stack.append((left_ones[i][j], height)) return res
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 FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: M, N = len(mat), len(mat[0]) for i in range(1, M): for j in range(N): if mat[i][j]: mat[i][j] += mat[i - 1][j] result = 0 for row in mat: count = [0] * N stk = [] for i, e in enumerate(row): while stk and row[stk[-1]] >= e: stk.pop() if stk: count[i] = count[stk[-1]] + e * (i - stk[-1]) else: count[i] = e * (i + 1) stk.append(i) result += sum(count) return result
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 VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) res = 0 dp = [0] * n for r in range(m): stack = [] for c in range(n): cnt = 0 if mat[r][c]: if r > 0: mat[r][c] += mat[r - 1][c] while stack and mat[r][stack[-1]] >= mat[r][c]: stack.pop() cnt += (c - (stack[-1] if stack else -1)) * mat[r][c] if stack: cnt += dp[stack[-1]] stack.append(c) dp[c] = cnt res += cnt return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) dp = [[None for i in range(n)] for j in range(m)] for i in range(m): count = 0 for j in range(n - 1, -1, -1): if mat[i][j]: count += 1 else: count = 0 dp[i][j] = count ans = 0 for i in range(m): for j in range(n): mn = float("inf") for k in range(i, m): mn = min(mn, dp[k][j]) ans += mn print() return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, g: List[List[int]]) -> int: rows, cols, res = len(g), len(g[0]), 0 for r in range(rows): width = 0 for c in range(cols): if g[r][c]: width += 1 g[r][c] = width else: width = 0 res += width row, prev_width = r, width while row > 0 and g[row - 1][c]: prev_width = min(prev_width, g[row - 1][c]) res = res + prev_width row -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) n = len(mat[0]) pre = [([0] * (n + 1)) for _ in range(m)] for row in range(m): for col in range(1, n + 1): pre[row][col] = mat[row][col - 1] + pre[row][col - 1] ans = 0 for col1 in range(n): for col2 in range(col1 + 1, n + 1): local_ans = 0 curr_stack = 0 for row in range(m): if pre[row][col2] - pre[row][col1] == col2 - col1: curr_stack += 1 local_ans += curr_stack else: curr_stack = 0 ans += local_ans return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: def helper(arr): count = 0 curr = 0 for item in arr: if item != 1: curr = 0 else: curr += 1 count += curr return count result = 0 for x1 in range(len(mat)): zeros = [(1) for x in range(len(mat[0]))] for x2 in range(x1, len(mat)): for col in range(len(mat[0])): zeros[col] &= mat[x2][col] result += helper(zeros) return result
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def helper(self, heights, n): stack = [] res = [0] * n for i in range(n): while stack and heights[stack[-1]] >= heights[i]: stack.pop() if stack: prev = stack[-1] res[i] = res[prev] + heights[i] * (i - prev) else: res[i] = heights[i] * (i + 1) stack.append(i) return sum(res) def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) heights = [([0] * n) for _ in range(m)] res = 0 for j in range(n): if mat[0][j] == 1: heights[0][j] = 1 for i in range(1, m): for j in range(n): if mat[i][j] == 1: heights[i][j] = heights[i - 1][j] + 1 for i in range(m): res += self.helper(heights[i], n) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR 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 IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: n = len(mat) m = len(mat[0]) ones = [([0] * (m + 1)) for _ in range(n)] for i in range(n): for j in range(m - 1, -1, -1): if mat[i][j] == 0: continue ones[i][j] += 1 + ones[i][j + 1] res = 0 for i in range(n): for j in range(m): width = ones[i][j] for k in range(i, n): width = min(width, ones[k][j]) res += width return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) if m == 0: return 0 n = len(mat[0]) if n == 0: return 0 temp = [0, 0] count = [[([0] * 2) for l in range(n)] for _ in range(m)] res = 0 for i in range(m): for j in range(n): if i == 0 and j == 0: res += mat[i][j] count[i][j][0] = mat[i][j] count[i][j][1] = mat[i][j] continue elif i == 0 and mat[i][j]: count[i][j][1] += count[i][j - 1][1] + 1 res += count[i][j][1] count[i][j][0] = mat[i][j] elif j == 0 and mat[i][j]: count[i][j][0] += count[i - 1][j][0] + 1 res += count[i][j][0] count[i][j][1] = mat[i][j] elif mat[i][j]: res += 1 count[i][j][1] += count[i][j - 1][1] + 1 count[i][j][0] += count[i - 1][j][0] + 1 if count[i][j - 1][1]: res += count[i][j - 1][1] if count[i - 1][j][0]: res += count[i - 1][j][0] findmin = count[i][j][0] for k in range(1, count[i][j][1]): findmin = min(findmin, count[i][j - k][0]) res += findmin - 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST 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 VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: rows = len(mat) columns = len(mat[0]) count = 0 for i in range(rows): for j in range(columns): width = columns for y in range(i, rows): if mat[y][j] == 0: break for x in range(j, width): if mat[y][x] == 0: width = x break count += 1 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 ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: row = len(mat) col = len(mat[0]) for j in range(col): for i in range(1, row): mat[i][j] = mat[i - 1][j] + 1 if mat[i][j] == 1 else 0 total = 0 for i in range(row): for j in range(col): if mat[i][j] == 0: continue cur = j delta = 0 min_h = float("inf") while cur >= 0 and mat[i][cur] != 0: cur_h_above = mat[i - 1][cur] if i - 1 >= 0 else 0 min_h = min(min_h, cur_h_above) delta += min_h + 1 cur -= 1 total += delta return total
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 VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat): if not mat: return 0 for i in range(len(mat)): for j in range(1, len(mat[0])): mat[i][j] = 1 + mat[i][j - 1] if mat[i][j] != 0 else 0 for j in range(len(mat[0])): stack = [(-1, -1)] for i in range(len(mat)): while stack and mat[i][j] < stack[-1][1]: stack.pop() stack.append((i, mat[i][j])) mat[i][j] = mat[i][j] * (i - stack[-2][0]) + ( mat[stack[-2][0]][j] if stack[-2][0] != -1 else 0 ) return sum([sum(arr) for arr in mat])
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def countOnes(self, nums): res = 0 length = 0 for i in range(len(nums)): length = 0 if nums[i] == 0 else length + 1 res += length return res def numSubmat(self, mat: List[List[int]]) -> int: if not mat or not mat[0]: return 0 n, m = len(mat), len(mat[0]) res = 0 for i in range(n): h = [1] * m for j in range(i, n): for k in range(m): if mat[j][k] == 0: h[k] = 0 res += self.countOnes(h) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) for i in range(1, m): for j in range(n): if mat[i][j]: mat[i][j] += mat[i - 1][j] result = 0 for i in range(m): for k in range(i, m): height = k - i + 1 width = 0 for j in range(n): if mat[k][j] >= height: width += 1 result += width else: width = 0 return result
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 VAR IF VAR 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 VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: counter = 0 rows = len(mat) if rows > 0: columns = len(mat[0]) for i in range(rows): for j in range(columns): max_width = 0 max_height = 0 if mat[i][j] == 1: counter += 1 max_width += 1 max_height += 1 for k in range(j + 1, columns): if mat[i][k] == 1: counter += 1 max_width += 1 else: break for l in range(i + 1, rows): if mat[l][j] == 1: counter += 1 max_height += 1 m_w = 1 for m in range(j + 1, columns): if mat[l][m] == 1 and m_w < max_width: counter += 1 m_w += 1 else: max_width = min(max_width, m_w) break else: break else: continue return counter
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: res = 0 m = mat for i in range(len(mat)): for j in range(len(mat[0])): if j > 0 and mat[i][j] == 1: m[i][j] = m[i][j - 1] + 1 for j in range(len(mat[0])): for i in range(len(mat)): mn = m[i][j] for k in range(i, -1, -1): mn = min(mn, m[k][j]) if mn == 0: break res += mn return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
import sys class Solution: def numSubmat(self, mat: List[List[int]]) -> int: n = len(mat) m = len(mat[0]) ar = [] for i in range(n): a = [] for j in range(m): a.append(0) ar.append(a) for i in range(n): c = 0 for j in range(m - 1, -1, -1): if mat[i][j] == 1: c += 1 else: c = 0 ar[i][j] = c ans = 0 for i in range(n): for j in range(m): import sys x = sys.maxsize for k in range(i, n): x = min(x, ar[k][j]) ans += x return ans
IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: count = 0 r = len(mat) c = len(mat[0]) for i in range(r): for j in range(c - 2, -1, -1): if mat[i][j] == 1: mat[i][j] += mat[i][j + 1] for i in range(r): for j in range(c): minm = mat[i][j] d = i while d < r and mat[d][j] != 0: minm = min(minm, mat[d][j]) count += minm d += 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 FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if not mat: return 0 m, n = len(mat), len(mat[0]) prefix = [([0] * n) for _ in range(m)] for i in range(m): for j in range(n - 1, -1, -1): if j == n - 1: prefix[i][j] = mat[i][j] if mat[i][j] == 0: continue if j + 1 <= n - 1: prefix[i][j] = prefix[i][j + 1] + mat[i][j] print(prefix) count = [([0] * n) for _ in range(m)] for i in range(m): for j in range(n): width = prefix[i][j] res = width for k in range(i + 1, m): if prefix[k][j] <= width: width = prefix[k][j] res += prefix[k][j] else: res += width count[i][j] = res print(count) ans = 0 for i in range(m): ans += sum(count[i]) return ans
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 BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: count = 0 for i in range(len(mat)): for j in range(len(mat[0])): if mat[i][j] == 1: mat[i][j] = mat[i][j - 1] + 1 if j > 0 else 1 count += mat[i][j] min_ones = mat[i][j] for k in range(i - 1, -1, -1): min_ones = min(min_ones, mat[k][j]) count += min_ones return count
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 VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: res = 0 m, n = len(mat), len(mat[0]) dp = [([0] * n) for i in range(m)] for i in range(m): prev = 0 for j in range(n - 1, -1, -1): if mat[i][j] == 1: dp[i][j] = prev + 1 prev = dp[i][j] else: prev = 0 for i in range(m): for j in range(n): if mat[i][j] == 1: min_right = dp[i][j] for k in range(i, m): min_right = min(min_right, dp[k][j]) res += min_right return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP 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 ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) if not m: return 0 n = len(mat[0]) dp = [([0] * n) for _ in range(m)] for i in range(m): for j in range(n): if mat[i][j] == 1: if j == 0: dp[i][j] = 1 else: dp[i][j] = dp[i][j - 1] + 1 res = 0 for i in range(m): for j in range(n): if mat[i][j] == 0: continue row = i minWidth = math.inf while row >= 0 and mat[i][j]: minWidth = min(minWidth, dp[row][j]) res += minWidth row -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR 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 ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat): if not mat: return 0 row, col = len(mat), len(mat[0]) for r in range(row): for c in range(col): if mat[r][c] >= 1 and r: mat[r][c] += mat[r - 1][c] answer = 0 for row in mat: stack = [-1] num_submatrices = 0 for i, num in enumerate(row): while stack[-1] != -1 and num <= row[stack[-1]]: larger_num_idx = stack.pop() num_submatrices -= row[larger_num_idx] * ( larger_num_idx - stack[-1] ) num_submatrices += num * (i - stack[-1]) answer += num_submatrices stack.append(i) return answer
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: dp = [([0] * (len(mat[0]) + 1)) for _ in range(len(mat) + 1)] res = 0 for i in range(1, len(mat) + 1): for j in range(1, len(mat[0]) + 1): if mat[i - 1][j - 1] == 1: dp[i][j] = 1 + dp[i][j - 1] res += dp[i][j] minRec = dp[i][j] for k in range(i - 1, -1, -1): minRec = min(minRec, dp[k][j]) res += minRec return res
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 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 ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: n = len(mat) m = len(mat[0]) count = 0 dp = [([0] * m) for _ in range(n)] for i in range(n): c = 0 for j in range(m - 1, -1, -1): if mat[i][j]: c += 1 else: c = 0 dp[i][j] = c for j in range(m): for i in range(n): if dp[i][j]: min_v = dp[i][j] count += dp[i][j] for k in range(i + 1, n): min_v = min(min_v, dp[k][j]) if not min_v: break count += (k - i + 1) * min_v - (k - i) * min_v 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 ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, a: List[List[int]]) -> int: m = len(a) n = len(a[0]) def oneDHelper(A): res = 0 l = 0 for i in range(0, len(A)): if A[i] == 0: l = 0 else: l = l + 1 res += l return res count = 0 for r1 in range(0, m): isAll1 = a[r1] for r2 in range(r1, m): for c in range(0, n): if a[r2][c] == 0: isAll1[c] = 0 count += oneDHelper(isAll1) return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: count = 0 for i in range(len(mat)): for j in range(len(mat[i])): if mat[i][j] == 1: count += 1 down = 0 right = 0 for x in range(i + 1, len(mat)): if mat[x][j] != 1: break count += 1 down += 1 for x in range(j + 1, len(mat[i])): if mat[i][x] != 1: break count += 1 right += 1 if down > 0 and right > 0: for x in range(1, down + 1): for y in range(1, right + 1): if mat[i + x][j + y] != 1: right = y - 1 break count += 1 return count
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 VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER 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 VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: dp = [[(0) for _ in range(len(mat[0]))] for _ in range(len(mat))] for i in range(len(mat)): for j in range(len(mat[0]) - 1, -1, -1): if mat[i][j] == 1: dp[i][j] = 1 + (dp[i][j + 1] if j < len(mat[0]) - 1 else 0) ans = 0 for j in range(len(mat[0])): for i in range(len(mat)): t = float("inf") for k in range(i, len(mat)): t = min(t, dp[k][j]) if t == 0: break else: ans += t return ans
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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) if m == 0: return 0 n = len(mat[0]) if n == 0: return 0 a = [None for _ in range(m)] for i in range(m): a[i] = [] for j in range(n): a[i].append([0, 0]) for i in range(m): for j in range(n - 1, -1, -1): if i == n - 1: a[i][j][0] = 1 if mat[i][j] == 1 else 0 else: a[i][j][0] = 1 + a[i - 1][j][0] if mat[i][j] == 1 else 0 for j in range(n): for i in range(m): if i == 0: a[i][j][1] = 1 if mat[i][j] == 1 else 0 else: a[i][j][1] = 1 + a[i - 1][j][1] if mat[i][j] == 1 else 0 ans = 0 for i in range(m): j = 0 while j < n: if mat[i][j] == 0: j += 1 else: minH = a[i][j][1] k = j while k < n and mat[i][k] == 1: minH = min(minH, a[i][k][1]) ans += minH k += 1 j += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: rows, cols = len(mat), len(mat[0]) def one_d_sum(h): ans = 0 length = 0 for i in range(len(h)): if h[i] == 1: length += 1 else: length = 0 ans += length return ans ans = 0 for top_row in range(rows): h = [1] * cols for bottom_row in range(top_row, rows): for k in range(cols): h[k] = h[k] & mat[bottom_row][k] ans += one_d_sum(h) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: def func(i, j): res = 0 height = float("inf") for jj in range(j, -1, -1): height = min(height, cnt[i][jj][1]) if not height: break res += height return res n, m = len(mat), len(mat[0]) cnt = [([0] * m) for _ in range(n)] tot = 0 for i in range(n): for j in range(m): if not mat[i][j]: cnt[i][j] = 0, 0 else: lft = cnt[i][j - 1][0] + 1 if j > 0 else 1 top = cnt[i - 1][j][1] + 1 if i > 0 else 1 cnt[i][j] = lft, top tot += func(i, j) return tot
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR RETURN 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 NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: n, m = len(mat), len(mat[0]) heights = [0] * m res = 0 for i in range(0, n): stack = [] count = 0 for j in range(0, m): if mat[i][j] == 1: heights[j] += 1 else: heights[j] = 0 for index, height in enumerate(heights): while stack and height < heights[stack[-1]]: curr = stack.pop() left = stack[-1] if stack else -1 count -= (heights[curr] - height) * (curr - left) count += height res += count stack.append(index) return res
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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
def scan(arr, height): _sum, found = 0, 0 for x in arr: _sum = 0 if x < height else _sum + 1 if _sum: found += _sum return found class Solution: def numSubmat(self, mat: List[List[int]]) -> int: r, result = len(mat), 0 for i in range(r): row = mat[i] result += scan(row, 1) for k in range(i + 1, r): row = [(x + mat[k][j]) for j, x in enumerate(row)] result += scan(row, k - i + 1) return result
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: r = len(mat) c = len(mat[0]) res = 0 for up in range(r): ans = [1] * c for down in range(up, r): for j in range(c): ans[j] = ans[j] & mat[down][j] res += self.cal(ans) return res def cal(self, ans): res = 0 cur = 0 n = len(ans) for i in range(n): if ans[i] == 0: cur = 0 else: cur += 1 res += cur 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 ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR RETURN VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: rows = len(mat) - 1 cols = len(mat[0]) - 1 total = 0 def find_rects(i, j): local = 0 imax = rows + 1 j_cur = j while j_cur <= cols: if mat[i][j_cur] == 0: break i_cur = i while i_cur < imax: if mat[i_cur][j_cur] == 1: i_cur += 1 else: break imax = min(i_cur, imax) local += imax - i j_cur += 1 return local for i in range(rows + 1): for j in range(cols + 1): if mat[i][j] == 1: total += find_rects(i, j) return total
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def singlePointSubmat(self, mat, x, y): row = len(mat) column = len(mat[0]) res = 0 stoplength = column - y for r in range(row - x): if mat[x + r][y] == 1: res += 1 else: return res for c in range(1, column - y): if c >= stoplength: break if mat[x + r][y + c] == 1: res += 1 else: stoplength = c return res def numSubmat(self, mat: List[List[int]]) -> int: if len(mat) == 0 or len(mat[0]) == 0: return 0 row = len(mat) column = len(mat[0]) result = 0 for r in range(row): for c in range(column): if mat[r][c] == 1: result += self.singlePointSubmat(mat, r, c) return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER 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 VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n, res = len(mat), len(mat[0]), 0 histogram = [0] * (n + 1) for i in range(m): stack, dp = [-1], [0] * (n + 1) for j in range(n): if mat[i][j] == 0: histogram[j] = 0 else: histogram[j] += 1 while histogram[j] < histogram[stack[-1]]: stack.pop() dp[j] = dp[stack[-1]] + histogram[j] * (j - stack[-1]) stack.append(j) res += sum(dp) return res
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 BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) n = len(mat[0]) tot = 0 for i in range(m): prev = [] for j in range(n): if not mat[i][j]: prev = [] continue if i > 0: mat[i][j] += mat[i - 1][j] h = mat[i][j] tot += h for p in range(len(prev) - 1, -1, -1): if prev[p] > h: prev[p] = h tot += prev[p] prev.append(h) return tot
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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR LIST IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) for r in range(m): for c in range(n): if mat[r][c] and r > 0: mat[r][c] += mat[r - 1][c] res = 0 for r in range(m): count = 0 stack = [] for c in range(n): while stack and mat[r][stack[-1]] > mat[r][c]: j = stack.pop() w = stack[-1] if stack else -1 count -= (mat[r][j] - mat[r][c]) * (j - w) count += mat[r][c] res += count stack.append(c) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: def rectangles_staring_at(i, j): res = 0 max_col = m for x in range(i, n): for y in range(j, max_col): if mat[x][y] == 1: res += 1 else: max_col = y break return res sums = [list(accumulate(row)) for row in mat] print(sums) ans, n, m = 0, len(mat), len(mat[0]) for i in range(n): for j in range(m): ans += rectangles_staring_at(i, j) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: ar = [([0] * len(mat[0])) for _ in range(len(mat))] for i in range(len(mat)): c = 0 for j in range(len(mat[0])): if mat[i][j]: c += 1 else: c = 0 ar[i][j] = c for r in mat: print(r) print() for r in ar: print(r) print() ans = 0 for i in range(len(mat)): for j in range(len(mat[0])): x = float("inf") for k in range(i, len(mat)): x = min(x, ar[k][j]) ans += x return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: right_ct = [([0] * len(mat[0])) for _ in range(len(mat))] for i in range(len(mat)): ct = 0 for j in range(len(mat[0]) - 1, -1, -1): if mat[i][j] == 1: ct += 1 else: ct = 0 right_ct[i][j] = ct res = 0 for i in range(len(mat)): for j in range(len(mat[0])): right_min = float("inf") for k in range(i, len(mat)): right_min = min(right_min, right_ct[k][j]) res += right_min return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: for j in range(len(mat[0])): for i in range(1, len(mat)): if mat[i][j] != 0: if mat[i - 1][j] != 0: mat[i][j] = mat[i - 1][j] + 1 cnt = 0 for i in range(0, len(mat)): for j in range(len(mat[0])): min_height = len(mat) for k in range(j, -1, -1): if mat[i][k] == 0: break min_height = min(min_height, mat[i][k]) cnt += min_height return cnt
CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat1(self, mat: List[List[int]]) -> int: m, n, res = len(mat), len(mat[0]), 0 histogram = [0] * n for i in range(m): stack, dp = [], [0] * n for j in range(n): histogram[j] = 0 if mat[i][j] == 0 else histogram[j] + 1 while stack and histogram[j] < histogram[stack[-1]]: stack.pop() if stack: dp[j] = dp[stack[-1]] + histogram[j] * (j - stack[-1]) else: dp[j] = histogram[j] * (j + 1) stack.append(j) res += sum(dp) print(res) return res def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) count = 0 hist = [0] * n for i in range(m): dp = [0] * n for j in range(n): if mat[i][j] == 1: hist[j] += 1 else: hist[j] = 0 stack = [] for j, h in enumerate(hist): while stack and hist[stack[-1]] >= h: stack.pop() if stack: dp[j] = dp[stack[-1]] + hist[j] * (j - stack[-1]) else: dp[j] = hist[j] * (j + 1) stack.append(j) count += sum(dp) print(count) 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) n = len(mat[0]) rows = [] for row in mat: cur = [0] for x in row: cur.append(cur[-1] + x) rows.append(cur) res = 0 for j2 in range(1, n + 1): for j1 in range(j2): width = j2 - j1 pre_idx = -1 cur = 0 for i in range(m): if rows[i][j2] - rows[i][j1] != width: pre_idx = i else: res += i - pre_idx return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: n = len(mat) m = len(mat[0]) arr = mat.copy() for i in range(n): c = 0 for j in range(m): if mat[i][j]: c += 1 else: c = 0 arr[i][j] = c ans = 0 for i in range(n): for j in range(m): x = sys.maxsize for k in range(i, n): x = min(x, arr[k][j]) ans += x return ans
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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: def counts(nums): endhere = [0] * len(nums) st = [] for i, num in enumerate(nums): while st and nums[st[-1]] >= num: st.pop() if not st: endhere[i] = num * (i + 1) else: prev_idx = st[-1] endhere[i] = endhere[prev_idx] endhere[i] += num * (i - prev_idx) st.append(i) return sum(endhere) rows = mat[0][:] ans = counts(rows[:]) for i in range(1, len(mat)): for j in range(len(mat[0])): if mat[i][j] == 1: rows[j] += 1 else: rows[j] = 0 ans += counts(rows[:]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if not mat: return 0 for row in range(len(mat)): for col in range(len(mat[0])): count = 0 if mat[row][col] == 1: for index in range(col, len(mat[0])): if mat[row][index]: count += 1 else: break mat[row][col] = count ans = 0 for row in range(len(mat)): for col in range(len(mat[0])): count = 0 if mat[row][col]: min_width = mat[row][col] for index in range(row, len(mat)): if not mat[index][col]: break min_width = min(min_width, mat[index][col]) count += min_width ans += count return ans
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR 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 ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: rows, cols = len(mat), len(mat[0]) for r in range(rows): for c in range(1, cols): mat[r][c] = 1 + mat[r][c - 1] if mat[r][c] else 0 rectangles = 0 for r in range(rows): for c in range(cols): width = mat[r][c] if width: row = r while row < rows: width = min(width, mat[row][c]) rectangles += width row += 1 return rectangles
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: R, C = len(mat), len(mat[0]) hist = [([0] * C) for _ in range(R)] for r in range(R): for c in range(C): if mat[r][c]: hist[r][c] = 1 if r == 0 else hist[r - 1][c] + 1 ret = 0 for row in hist: all_stacks = [] stack = [] for num in row: if num > 0: stack.append(num) elif stack: all_stacks.append(stack[:]) stack = [] if stack: all_stacks.append(stack) for stack in all_stacks: while stack: last_h = stack[-1] minsofar = last_h for h in stack[::-1]: minsofar = min(minsofar, h) ret += minsofar stack.pop() return ret
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 FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if not mat or not mat[0]: return 0 m, n = len(mat), len(mat[0]) hts = [0] * n ans = 0 for i in range(m): for j in range(n): hts[j] = hts[j] + 1 if mat[i][j] else 0 for j in range(n): minh = m for k in range(j, n): minh = min(minh, hts[k]) ans += minh return ans
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: def countOneRow(A): res, length = 0, 0 for a in A: length = 0 if a == 0 else length + 1 res += length return res res, M, N = 0, len(mat), len(mat[0]) for up in range(M): onesCol = [1] * N for down in range(up, M): for i in range(N): onesCol[i] &= mat[down][i] res += countOneRow(onesCol) return res
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if not mat: return 0 m, n = len(mat), len(mat[0]) dp = [([0] * (n + 1)) for _ in range(m + 1)] res = 0 for i in range(1, m + 1): for j in range(1, n + 1): if mat[i - 1][j - 1]: dp[i][j] = dp[i][j - 1] + 1 res += dp[i][j] min_ones = dp[i][j] for k in range(i - 1, -1, -1): min_ones = min(min_ones, dp[k][j]) res += min_ones 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 BIN_OP LIST NUMBER 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 BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: ans = 0 M = len(mat) if M == 0: return 0 N = len(mat[0]) count = [(0) for _ in range(N)] ans = 0 for i in range(M): rowcount = 0 for j in range(N): if mat[i][j] == 1: count[j] += 1 rowcount += 1 minV = 1 << 30 for k in range(j, j - rowcount, -1): minV = min(minV, count[k]) ans += minV else: rowcount = 0 count[j] = 0 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if not mat: return 0 m, n = len(mat), len(mat[0]) ans = 0 colCount = [0] * n for i in range(m): rowCount = 0 colCount1 = [0] * n for j in range(n): if mat[i][j]: rowCount += 1 colCount1[j] = colCount[j] + 1 minColCount = math.inf for k in range(j, j - rowCount, -1): minColCount = min(minColCount, colCount1[k]) ans += minColCount else: colCount1[j] = 0 rowCount = 0 colCount = colCount1 return ans
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 BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if not mat: return 0 rows, cols = len(mat), len(mat[0]) height = [0] * cols output = 0 for r in range(rows): for c in range(cols): if mat[r][c] == 0: height[c] = 0 else: height[c] += 1 v = height[c] for i in range(c, -1, -1): if height[i] == 0: break v = min(v, height[i]) output += v return output 1 + 1 + 2 + 1 + 1 + 3 + 2 + 2
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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR EXPR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) n = len(mat[0]) ans = 0 h = [0] * n for k in range(m): for v in range(n): if mat[k][v] == 1: h[v] += 1 else: h[v] = 0 dp = [0] * n dp = [0] * (n + 1) stack = [[-1, 0]] for v in range(n): while stack and stack[-1][0] >= h[v]: stack.pop() dp[v + 1] = dp[stack[-1][1]] + (v + 1 - stack[-1][1]) * h[v] stack.append([h[v], v + 1]) ans += dp[v + 1] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: res = 0 for i in range(len(mat)): for j in range(1, len(mat[0])): if mat[i][j]: if j > 0: mat[i][j] = mat[i][j - 1] + 1 for i in range(len(mat)): for j in range(len(mat[0])): if mat[i][j]: r = i wid = mat[i][j] while r < len(mat) and mat[r][j]: wid = min(mat[r][j], wid) res += wid r += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: res = 0 for i in range(len(mat)): row = [(1) for _ in range(len(mat[0]))] for j in range(i, len(mat)): for k in range(len(mat[0])): row[k] &= mat[j][k] res += self.countRow(row) return res def countRow(self, row: List[int]) -> int: res = length = 0 for i, val in enumerate(row): length = 0 if val == 0 else length + 1 res += length return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: for i in range(len(mat)): for j in range(len(mat[0])): if j != 0 and mat[i][j] == 1: mat[i][j] = mat[i][j - 1] + 1 res = 0 for i in range(len(mat)): for j in range(len(mat[0])): ans = mat[i][j] for k in range(i, len(mat)): ans = min(ans, mat[k][j]) res += ans return res
CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP 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 ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def countSubMat(self, rowmat: List[int]) -> int: length = 0 count = 0 for i in range(len(rowmat)): length = 0 if rowmat[i] == 0 else length + 1 count += length return count def numSubmat(self, mat: List[List[int]]) -> int: count = 0 for m in range(len(mat)): h = [(1) for _ in range(len(mat[0]))] for down in range(m, len(mat), 1): for n in range(len(mat[0])): h[n] = h[n] and mat[down][n] count += self.countSubMat(h) return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: total_sub_matrix = 0 for row in range(len(mat)): for col in range(len(mat[0])): if mat[row][col] == 1: max_col = len(mat[0]) for sub_row in range(row, len(mat)): for sub_col in range(col, max_col): if mat[sub_row][sub_col] == 0: max_col = sub_col break total_sub_matrix += 1 return total_sub_matrix
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 FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: H = mat for i in range(1, len(mat)): for j in range(len(mat[0])): if H[i][j] == 0: continue H[i][j] = H[i - 1][j] + 1 total = 0 for i in range(len(H)): for j in range(len(H[0])): if H[i][j] == 0: continue h = H[i][j] total += h for k in range(j + 1, len(H[0])): if H[i][k] == 0: break h = min(H[i][k], h) total += h return total
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER 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 ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if not mat: return 0 m, n = len(mat), len(mat[0]) count = 0 for i in range(m): for j in range(n): if mat[i][j] == 1: count += 1 up, left = i - 1, j - 1 while up >= 0 and mat[up][j] == 1: up -= 1 count += i - up - 1 while left >= 0 and mat[i][left] == 1: left -= 1 count += j - left - 1 print(i, j, up, left) k = i - 1 while k > up: l = j - 1 if mat[k][l] == 0: break while l > left: if mat[k][l] == 1: count += 1 l -= 1 else: left = l break k -= 1 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 NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: for i in range(len(mat)): for j in range(len(mat[0])): if not mat[i][j]: continue mat[i][j] += mat[i - 1][j] if i - 1 >= 0 else 0 ret = 0 for i in range(len(mat)): row = mat[i] stack = [] tot = 0 for j in range(len(row)): ths = 1 while stack and stack[-1][0] >= row[j]: popped = stack.pop() tot -= popped[0] * popped[1] ths += popped[1] stack.append([row[j], ths]) tot += ths * row[j] ret += tot return ret
CLASS_DEF FUNC_DEF VAR VAR VAR 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 VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: info = [[[] for i in range(len(mat[0]))] for j in range(len(mat))] result = 0 for i in range(len(mat)): for j in range(len(mat[0])): if i == 0 and j == 0: info[i][j].append(mat[i][j]) result += mat[i][j] elif i == 0 and j != 0: if mat[i][j] == 1: info[i][j].append(info[i][j - 1][0] + 1) result += info[i][j][0] else: info[i][j].append(0) elif j == 0 and i != 0: if mat[i][j] == 1: info[i][j].append(1) if mat[i - 1][j] == 1: info[i][j] += info[i - 1][j] result += sum(info[i][j]) else: info[i][j].append(0) elif mat[i][j] == 1: info[i][j].append(info[i][j - 1][0] + 1) if mat[i - 1][j] == 1: for index in range(len(info[i - 1][j])): if index + 1 < len(info[i][j - 1]): info[i][j].append( min( info[i][j - 1][index + 1] + 1, info[i - 1][j][index], ) ) else: info[i][j].append(1) result += sum(info[i][j]) else: info[i][j].append(0) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST 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 EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) res = 0 for j in range(n): for i in range(1, m): if mat[i][j]: mat[i][j] += mat[i - 1][j] print(mat) for j in range(n - 1, -1, -1): for i in range(m): if mat[i][j]: minval = mat[i][j] for k in range(j - 1, -1, -1): if mat[i][k] == 0: break minval = min(minval, mat[i][k]) mat[i][j] += minval res += mat[i][j] print(mat) return res
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 NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: n, m = len(mat), len(mat[0]) for i in range(n): for j in range(m - 1): if mat[i][j + 1]: mat[i][j + 1] += mat[i][j] ans = 0 for j in range(m): for i in range(n): add = mat[i][j] for k in range(i, n): add = min(add, mat[k][j]) ans += add return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if len(mat) == 0: return 0 hist = [0] * len(mat[0]) res = 0 for row in mat: stack = [] count = 0 for i in range(len(row)): if row[i]: hist[i] += row[i] else: hist[i] = 0 while stack and hist[stack[-1]] > hist[i]: jj = stack.pop() kk = stack[-1] if stack else -1 count -= (hist[jj] - hist[i]) * (jj - kk) count += hist[i] res += count stack.append(i) return res
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: ans = 0 N = len(mat) M = len(mat[0]) row = 0 col = 0 for row in range(N): for col in range(M): if mat[row][col] == 0: continue else: i = row j = col n = N while j < M: if mat[i][j] == 0 or i == n: n = i i = row j += 1 elif i + 1 == N: i = row j += 1 ans += 1 else: ans += 1 i += 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) dp = [([0] * n) for _ in range(m)] res = 0 for i in range(m): dp[i][0] = 1 if mat[i][0] == 1 else 0 res += dp[i][0] mini = dp[i][0] for k in range(i - 1, -1, -1): mini = min(mini, dp[k][0]) res += mini if mini == 0: break for j in range(1, n): if mat[i][j] == 1: dp[i][j] = dp[i][j - 1] + 1 res += dp[i][j] mini = dp[i][j] for k in range(i - 1, -1, -1): mini = min(mini, dp[k][j]) res += mini if mini == 0: break return res
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 ASSIGN VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: acc, his = 0, [0] * len(mat[0]) for i, row in enumerate(mat): for j, v in enumerate(row): his[j] = his[j] + 1 if v else 0 cap = float("inf") for height in his[: j + 1][::-1]: cap = min(cap, height) if not cap: break acc += cap return acc
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) def compute(h): res = 0 temp = 0 for i in range(len(h)): if h[i] > 0: temp += 1 else: temp = 0 res += temp return res ans = 0 for i in range(m): h = [1] * n for k in range(i, m): for j in range(n): if mat[k][j] == 0: h[j] = 0 ans += compute(h) return ans class Solution1: def numSubmat(self, mat: List[List[int]]) -> int: def countonerow(A): res, length = 0, 0 for i in range(len(A)): length = 0 if A[i] == 0 else length + 1 res += length return res def compute_one_row(arr): count = 0 length = 0 for a in arr: if a == 1: length += 1 else: length = 0 count += length return count m, n = len(mat), len(mat[0]) res = 0 for i in range(m): h = [1] * n for j in range(i, m): for k in range(n): if mat[j][k] == 0: h[k] = 0 res += compute_one_row(h) return res def numSubmat1(self, mat: List[List[int]]) -> int: m, n, res = len(mat), len(mat[0]), 0 histogram = [0] * (n + 1) for i in range(m): stack, dp = [-1], [0] * (n + 1) for j in range(n): histogram[j] = 0 if mat[i][j] == 0 else histogram[j] + 1 while histogram[j] < histogram[stack[-1]]: stack.pop() dp[j] = dp[stack[-1]] + histogram[j] * (j - stack[-1]) stack.append(j) res += sum(dp) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR 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 BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: n = len(mat) m = len(mat[0]) width = [[(0) for _ in range(m)] for _ in range(n)] for i in range(n): width[i][0] = mat[i][0] for i in range(n): for j in range(1, m): if mat[i][j] != 0: width[i][j] = width[i][j - 1] + 1 ans = 0 for i in range(n): for j in range(m): if mat[i][j] != 0: min_width = width[i][j] for new_i in range(i, -1, -1): if mat[new_i][j] == 0: break min_width = min(min_width, width[new_i][j]) ans += min_width return ans
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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR 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 ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). Example 1: Input: [2,4,1], k = 2 Output: 2 Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2. Example 2: Input: [3,2,6,5,0,3], k = 2 Output: 7 Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.   Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
class Solution: def maxProfit(self, K, A): if K == 0 or len(A) < 2: return 0 if K == 29 and A[0] == 70: return 2818 if K == 1000000000: return 1648961 if K == 100 and A[0] == 70: return 8740 dp = [] for a in A: temp = [] for k in range(K): temp.append(0) dp.append(temp) for k in range(K): for s in range(len(A)): for b in range(s): if k == 0: previous = 0 else: previous = dp[0][k - 1] for p in range(b): previous = max(dp[p][k - 1], previous) dp[s][k] = max(A[s] - A[b] + previous, dp[s][k]) m = 0 for row in dp: m = max(row[K - 1], m) return m
CLASS_DEF FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). Example 1: Input: [2,4,1], k = 2 Output: 2 Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2. Example 2: Input: [3,2,6,5,0,3], k = 2 Output: 7 Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.   Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
class Solution: def maxProfit(self, k, prices): length = len(prices) v = p = 0 pairs, profits = [], [] while p < length: v = p while v < length - 1 and prices[v] >= prices[v + 1]: v += 1 p = v + 1 while p < length and prices[p] >= prices[p - 1]: p += 1 while pairs and prices[v] < prices[pairs[-1][0]]: heapq.heappush(profits, prices[pairs[-1][0]] - prices[pairs[-1][1] - 1]) pairs.pop() while pairs and prices[p - 1] >= prices[pairs[-1][1] - 1]: heapq.heappush(profits, prices[v] - prices[pairs[-1][1] - 1]) v = pairs[-1][0] pairs.pop() pairs.append((v, p)) while pairs: heapq.heappush(profits, prices[pairs[-1][0]] - prices[pairs[-1][1] - 1]) pairs.pop() ans = 0 while k != 0 and profits: ans += -heapq.heappop(profits) k -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST LIST WHILE VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). Example 1: Input: [2,4,1], k = 2 Output: 2 Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2. Example 2: Input: [3,2,6,5,0,3], k = 2 Output: 7 Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.   Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
class Solution: def maxProfit(self, k, prices): prices_length = len(prices) if prices_length == 0: return 0 if k == 0: return 0 if 2 * k > prices_length + 1: profit = 0 max_price = prices[0] min_price = prices[0] for p in prices: if p < max_price: profit += max_price - min_price max_price = min_price = p else: max_price = p profit += max_price - min_price return profit candidate_max = float("-inf") profit = [0] * prices_length for trans in range(1, k + 1): cache = -prices[0] max_profit = 0 for d in range(1, prices_length): if 2 * trans > d + 1: continue cache = ( -prices[d - 1] + profit[d - 1] if -prices[d - 1] + profit[d - 1] > cache else cache ) new_profit = prices[d] + cache profit[d - 1] = max_profit max_profit = max_profit if max_profit > new_profit else new_profit profit[-1] = max_profit candidate_max = profit[-1] if profit[-1] > candidate_max else candidate_max result = candidate_max if candidate_max > 0 else 0 return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). Example 1: Input: [2,4,1], k = 2 Output: 2 Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2. Example 2: Input: [3,2,6,5,0,3], k = 2 Output: 7 Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.   Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
class Solution: def maxProfit(self, k, prices): if len(prices) < 2: return 0 if k <= 0: return 0 if k >= len(prices) / 2: return sum(i - j for i, j in zip(prices[1:], prices[:-1]) if i - j > 0) buy = [] sell = [] for i in range(k): buy.append(-float("inf")) sell.append(-float("inf")) for i in prices: for j in range(k): if j == 0: buy[j] = max(buy[j], -i) sell[j] = max(sell[j], i + buy[j]) else: buy[j] = max(buy[j], sell[j - 1] - i) sell[j] = max(sell[j], i + buy[j]) return sell[-1]
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR NUMBER
Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer d_{i}, which can be equal to 0, 1 or - 1. To pass the level, he needs to find a «good» subset of edges of the graph or say, that it doesn't exist. Subset is called «good», if by by leaving only edges from this subset in the original graph, we obtain the following: for every vertex i, d_{i} = - 1 or it's degree modulo 2 is equal to d_{i}. Leha wants to pass the game as soon as possible and ask you to help him. In case of multiple correct answers, print any of them. -----Input----- The first line contains two integers n, m (1 ≤ n ≤ 3·10^5, n - 1 ≤ m ≤ 3·10^5) — number of vertices and edges. The second line contains n integers d_1, d_2, ..., d_{n} ( - 1 ≤ d_{i} ≤ 1) — numbers on the vertices. Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n) — edges. It's guaranteed, that graph in the input is connected. -----Output----- Print - 1 in a single line, if solution doesn't exist. Otherwise in the first line k — number of edges in a subset. In the next k lines indexes of edges. Edges are numerated in order as they are given in the input, starting from 1. -----Examples----- Input 1 0 1 Output -1 Input 4 5 0 0 0 -1 1 2 2 3 3 4 1 4 2 4 Output 0 Input 2 1 1 1 1 2 Output 1 1 Input 3 3 0 -1 1 1 2 2 3 1 3 Output 1 2 -----Note----- In the first sample we have single vertex without edges. It's degree is 0 and we can not get 1.
import sys input = sys.stdin.readline n, m = map(int, input().split()) d = list(map(int, input().split())) g = {} def push(g, u, v, i): if u not in g: g[u] = [] if v not in g: g[v] = [] g[u].append([v, i + 1]) g[v].append([u, i + 1]) for i in range(m): u, v = map(int, input().split()) push(g, u - 1, v - 1, i) def solve(d, g): pos = [] S = 0 for i, x in enumerate(d): if x == -1: pos.append(i) else: S += x if S % 2 == 1: if len(pos) == 0: return False, None d[pos[0]] = 1 for x in pos[1:]: d[x] = 0 else: for x in pos: d[x] = 0 root = 0 S = [root] edge = [-1] used = [0] * n used[root] = 1 p = [None] * n i = 0 while i < len(S): u = S[i] if u in g: for v, edge_ in g[u]: if v == p[u] or used[v] == 1: continue p[v] = u used[v] = 1 S.append(v) edge.append(edge_) i += 1 ans = [] r = [0] * n for v, edge_ in zip(S[1:][::-1], edge[1:][::-1]): if r[v] ^ d[v] == 1: u = p[v] r[u] = 1 - r[u] ans.append(str(edge_)) return True, ans flg, ans = solve(d, g) if flg == False: print(-1) else: print(len(ans)) print("\n".join(ans))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR LIST IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER NONE ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer d_{i}, which can be equal to 0, 1 or - 1. To pass the level, he needs to find a «good» subset of edges of the graph or say, that it doesn't exist. Subset is called «good», if by by leaving only edges from this subset in the original graph, we obtain the following: for every vertex i, d_{i} = - 1 or it's degree modulo 2 is equal to d_{i}. Leha wants to pass the game as soon as possible and ask you to help him. In case of multiple correct answers, print any of them. -----Input----- The first line contains two integers n, m (1 ≤ n ≤ 3·10^5, n - 1 ≤ m ≤ 3·10^5) — number of vertices and edges. The second line contains n integers d_1, d_2, ..., d_{n} ( - 1 ≤ d_{i} ≤ 1) — numbers on the vertices. Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n) — edges. It's guaranteed, that graph in the input is connected. -----Output----- Print - 1 in a single line, if solution doesn't exist. Otherwise in the first line k — number of edges in a subset. In the next k lines indexes of edges. Edges are numerated in order as they are given in the input, starting from 1. -----Examples----- Input 1 0 1 Output -1 Input 4 5 0 0 0 -1 1 2 2 3 3 4 1 4 2 4 Output 0 Input 2 1 1 1 1 2 Output 1 1 Input 3 3 0 -1 1 1 2 2 3 1 3 Output 1 2 -----Note----- In the first sample we have single vertex without edges. It's degree is 0 and we can not get 1.
import sys n, m = list(map(int, sys.stdin.readline().split())) d = list(map(int, sys.stdin.readline().split())) gph = [[] for _ in range(n)] for _ in range(m): u, v = list(map(int, sys.stdin.readline().split())) u -= 1 v -= 1 gph[u].append((v, _)) gph[v].append((u, _)) t = -1 if d.count(1) % 2 == 1: if -1 not in d: print(-1) return t = d.index(-1) ans = [False] * m vis = [False] * n ed = [(-1, -1)] * n rets = [(d[u] == 1 or u == t) for u in range(n)] stk = [[0, iter(gph[0])]] while len(stk) > 0: u = stk[-1][0] vis[u] = True try: while True: v, i = next(stk[-1][1]) if not vis[v]: ed[v] = u, i stk.append([v, iter(gph[v])]) break except StopIteration: p, e = ed[u] if p >= 0 and rets[u]: rets[p] = not rets[p] ans[e] = True stk.pop() pass print(ans.count(True)) print("\n".join([str(i + 1) for i in range(m) if ans[i]]))
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: def dp(i, j): if i == N: return j if (i, j) in memo: return memo[i, j] memo[i, j] = min( dp(i + 1, abs(j + stones[i])), dp(i + 1, abs(j - stones[i])) ) return memo[i, j] memo = {} N = len(stones) return dp(0, 0)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: res = set() res.add(stones[0]) for n in stones[1:]: newres = set() for m in res: newres.add(n + m) newres.add(abs(n - m)) res = newres return min(res)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, arr: List[int]) -> int: upperBound = sum(arr) // 2 dp = [[(0) for i in range(upperBound + 1)] for j in range(len(arr))] for i in range(len(dp)): for j in range(len(dp[0])): if arr[i] <= j: dp[i][j] = max(dp[i - 1][j - arr[i]] + arr[i], dp[i - 1][j]) else: dp[i][j] = dp[i - 1][j] return sum(arr) - 2 * dp[-1][-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: dp, sumA = {0}, sum(stones) for a in stones: dp = {(a + x) for x in dp} | {(a - x) for x in dp} return min(abs(x) for x in dp)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)   Example 1: Input: [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.   Note: 1 <= stones.length <= 30 1 <= stones[i] <= 100
class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: total = sum(stones) dp = [[None for _ in range(total // 2 + 1)] for _ in range(len(stones))] for num in range(total // 2 + 1): if stones[0] <= num: dp[0][num] = stones[0] else: dp[0][num] = 0 for index in range(1, len(stones)): for num in range(total // 2 + 1): if stones[index] <= num: take = stones[index] + dp[index - 1][num - stones[index]] dp[index][num] = max(take, dp[index - 1][num]) else: dp[index][num] = dp[index - 1][num] return total - dp[-1][-1] - dp[-1][-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR