class Solution:
def maximalRectangle(self, matrix) -> int:
if len(matrix) == 0:
return 0
res = 0
m, n = len(matrix), len(matrix[0])
heights = [0] * n
for i in range(m):
for j in range(n):
if matrix[i][j] == '0':
heights[j] = 0
else:
heights[j] = heights[j] + 1
res = max(res, self.largestRectangleArea(heights))
return res
def largestRectangleArea(self, heights):
heights.append(0)
stack = []
res = 0
for i in range(len(heights)):
while stack and heights[i] < heights[stack[-1]]:
s = stack.pop()
res = max(res, heights[s] * ((i - stack[-1] - 1) if stack else i))
stack.append(i)
return res