classSolution: defmaximalRectangle(self, matrix: List[str]) -> int: defmaximalhist(heights): heights = [0] + heights + [0] res, stack = 0, [0] for i inrange(1, len(heights)): while stack and heights[i] <= heights[stack[-1]]: tmp = stack.pop() if stack: weith = i - stack[-1] - 1 if (t := heights[tmp] * weith) > res: res = t stack.append(i) return res ifnot matrix: return0 m, n = len(matrix), len(matrix[0]) pre = [0] * n ans = 0 for i inrange(m): for j inrange(n): # if else 缩减 if matrix[i][j] != '0': pre[j] += int(matrix[i][j]) else: pre[j] = 0 ans = max(ans, maximalhist(pre)) return ans
classSolution: defasteroidCollision(self, asteroids: List[int]) -> List[int]: n = len(asteroids) stack = [0] for i inrange(1, n): while stack and asteroids[stack[-1]] > 0and asteroids[i] < 0: a = asteroids[stack[-1]] b = asteroids[i] if a + b < 0: stack.pop() elif a + b > 0: break elif a + b == 0: stack.pop() break else: stack.append(i) return [asteroids[i] for i in stack]
classSolution: defdailyTemperatures(self, temperatures: List[int]) -> List[int]: n = len(temperatures) stack, ans = [0], [0] * n for i inrange(1, n): while stack and temperatures[stack[-1]] < temperatures[i]: tmp = stack.pop() ans[tmp] = i - tmp stack.append(i) return ans