-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0316.py
23 lines (19 loc) · 865 Bytes
/
LC0316.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def maximalRectangle(self, matrix: List[List[str]]) -> int:
if not matrix:
return 0
rows, cols = len(matrix), len(matrix[0])
heights = [0] * (cols + 1) # Include an extra element for easier calculation
max_area = 0
for row in matrix:
for i in range(cols):
heights[i] = heights[i] + 1 if row[i] == '1' else 0
# Calculate max area using histogram method
stack = []
for i in range(len(heights)):
while stack and heights[i] < heights[stack[-1]]:
h = heights[stack.pop()]
w = i if not stack else i - stack[-1] - 1
max_area = max(max_area, h * w)
stack.append(i)
return max_area