forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy path85_Maximal_Rectangle.py
More file actions
47 lines (39 loc) · 1.22 KB
/
85_Maximal_Rectangle.py
File metadata and controls
47 lines (39 loc) · 1.22 KB
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
# Solution 1: Using Histograms - Stack
class Solution(object):
def maximalRectangle(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if len(matrix) == 0 or len(matrix[0]) == 0:
return 0
heights = [0 for _ in matrix[0]]
maxRect = float('-inf')
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j] == "0":
heights[j] = 0
else:
heights[j] += 1
maxRect = max(maxRect, self.largestRectangleArea(heights))
return maxRect
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
heights.append(0)
stack = [-1]
finalArea = 0
for i in range(len(heights)):
while heights[i] < heights[stack[-1]]:
height = heights[stack.pop()]
width = i - stack[-1] - 1
finalArea = max(finalArea, height *width)
stack.append(i)
heights.pop()
return finalArea
sol = Solution()
input = [["1","0"]]
output = sol.maximalRectangle(input)
print('Res: ', output)