-
Notifications
You must be signed in to change notification settings - Fork 2
/
085 Maximal Rectangle.py
51 lines (45 loc) · 1.5 KB
/
085 Maximal Rectangle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'''
85 Maximal Rectangle
https://leetcode.com/problems/maximal-rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
Example:
Input:
[
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]
Output: 6
'''
class Solution:
def largestRectangleArea(self, oriHeights: List[int]) -> int:
heights = oriHeights + [0]
stack = []
maxArea = 0
for i, h in enumerate(heights):
while stack and h < heights[stack[-1]]:
j = stack.pop()
recHeight = heights[j]
recWidth = i - 1 - stack[-1] if stack else i
maxArea = max(maxArea, recHeight * recWidth)
stack.append(i)
return maxArea
def maximalRectangle(self, matrix: List[List[str]]) -> int:
'''
Cannot use DP method of 221. Maximal Square
Use 84 Largest Rectangle in Histogram.
'''
rowCount = len(matrix)
if not rowCount:
return 0
colCount = len(matrix[0])
if not colCount:
return 0
heights = [0] * colCount
maxArea = 0
for i in range(rowCount):
for j in range(colCount):
heights[j] = 0 if matrix[i][j] == '0' else heights[j] + 1
maxArea = max(maxArea, self.largestRectangleArea(heights))
return maxArea