Skip to content

Instantly share code, notes, and snippets.

@bobfang1992
Created December 31, 2020 16:44
Show Gist options
  • Save bobfang1992/69453c6d942818e20f6d7f8a243da1a5 to your computer and use it in GitHub Desktop.
Save bobfang1992/69453c6d942818e20f6d7f8a243da1a5 to your computer and use it in GitHub Desktop.
Leetcode 84
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
stack = [-1]
result = 0
heights.append(0)
for i in range(len(heights)):
while stack and heights[stack[-1]] > heights[i]:
h = heights[stack.pop()]
w = i - stack[-1] - 1
# print(h, w)
result = max(result, h * w)
stack.append(i)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment