blob: 0b0853e65ed98fe01b59d7f45c1f3180ab123199 (
plain)
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
|
# 84. Largest Rectangle in Histogram
"""
given an array of integers 'heights' representing the histogram's bar height
where the width of each bar is 1, return the area of the largest rectangle
"""
class Solution(object):
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
heights.append(0)
stack, ans = [-1], 0
for i in range(len(heights)):
while heights[i] < heights[stack[-1]]:
h = heights[stack.pop()]
w = i - stack[-1] - 1
ans = max(ans, h * w)
stack.append(i)
heights.pop()
return ans
if __name__ == "__main__":
obj = Solution()
print(obj.largestRectangleArea(heights=[2, 1, 5, 6, 2, 3]))
print(obj.largestRectangleArea(heights=[2, 4]))
|