Created
June 29, 2019 18:07
-
-
Save ajinkyajawale14499/1f5056f8d89355c1e518d14d2daa0b09 to your computer and use it in GitHub Desktop.
largest histogram solution using 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
def largestRectangleArea(self, height): | |
height.append(0) | |
stack = [-1] | |
ans = 0 | |
for i in xrange(len(height)): | |
while height[i] < height[stack[-1]]: | |
h = height[stack.pop()] | |
w = i - stack[-1] - 1 | |
ans = max(ans, h * w) | |
stack.append(i) | |
height.pop() | |
return ans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment