Created
May 8, 2020 02:36
-
-
Save ZhouYang1993/8190d960997eb58bd5e5d9fdbf2d60bb to your computer and use it in GitHub Desktop.
Monotonic Stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def largestRectangleArea(self, heights: List[int]) -> int: | |
| ret = 0 | |
| mono_stack = [] | |
| heights.append(0) | |
| for i, v in enumerate(heights): | |
| while mono_stack and heights[mono_stack[-1]] > v: | |
| height = heights[mono_stack[-1]] | |
| length = i - mono_stack[-1] | |
| ret = max(ret, height * length) | |
| mono_stack.pop() | |
| mono_stack.append(i) | |
| return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment