1def largestRectangleArea(heights):
2 stack = []
3 max_area = 0
4
5 for i in range(len(heights)):
6 while stack and heights[i] < heights[stack[-1]]:
7 height = heights[stack.pop()]
8 width = i if not stack else i - stack[-1] - 1
9 current_area = height * width
10 max_area = max(max_area, current_area)
11
12 stack.append(i)
13
14 while stack:
15 height = heights[stack.pop()]
16 width = len(heights) if not stack else len(heights) - stack[-1] - 1
17 current_area = height * width
18 max_area = max(max_area, current_area)
19
20 return max_area