Skip to content

Commit dd09d65

Browse files
committed
Time: 691 ms (31.60%) | Memory: 31.2 MB (56.63%) - LeetSync
1 parent fc64d1b commit dd09d65

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)