We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fc64d1b commit dd09d65Copy full SHA for dd09d65
84-largest-rectangle-in-histogram/largest-rectangle-in-histogram.py
@@ -0,0 +1,17 @@
1
+class Solution:
2
+ def largestRectangleArea(self, heights: List[int]) -> int:
3
+ stack = []
4
+ heights.append(0) # Append a zero height at the end to ensure all elements in the stack are popped at the end
5
+
6
+ max_area = 0
7
+ i = 0
8
+ while i < len(heights):
9
+ if not stack or heights[i] >= heights[stack[-1]]:
10
+ stack.append(i)
11
+ i += 1
12
+ else:
13
+ top_index = stack.pop()
14
+ width = i if not stack else i - stack[-1] - 1
15
+ max_area = max(max_area, heights[top_index] * width)
16
17
+ return max_area
0 commit comments