Skip to content

Commit b9f2303

Browse files
authored
Merge pull request #3011 from coopers/coopers-0084
Update 0084-largest-rectangle-in-histogram.py
2 parents ff15684 + 5706fa3 commit b9f2303

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Diff for: python/0084-largest-rectangle-in-histogram.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ def largestRectangleArea(self, heights: List[int]) -> int:
33
maxArea = 0
44
stack = [] # pair: (index, height)
55

6-
for i, h in enumerate(heights):
7-
start = i
8-
while stack and stack[-1][1] > h:
9-
index, height = stack.pop()
10-
maxArea = max(maxArea, height * (i - index))
11-
start = index
12-
stack.append((start, h))
6+
for right, short in enumerate(heights):
7+
start = right
8+
while stack and short < stack[-1][1]:
9+
left, tall = stack.pop()
10+
maxArea = max(maxArea, tall * (right - left))
11+
start = left
12+
stack.append((start, short))
1313

1414
for i, h in stack:
1515
maxArea = max(maxArea, h * (len(heights) - i))

0 commit comments

Comments
 (0)