Skip to content

Instantly share code, notes, and snippets.

@ajinkyajawale14499
Created June 29, 2019 18:07
Show Gist options
  • Save ajinkyajawale14499/1f5056f8d89355c1e518d14d2daa0b09 to your computer and use it in GitHub Desktop.
Save ajinkyajawale14499/1f5056f8d89355c1e518d14d2daa0b09 to your computer and use it in GitHub Desktop.
largest histogram solution using stack
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