We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def largestRectangle(h):
# Write your code here
stack = [0]
area = 0
top = 0
h.append(0)
for i in range(1, len(h)):
if h[i] >= h[stack[-1]]:
stack.append(i)
else:
while stack and h[i] < h[stack[-1]]:
last = stack.pop()
if stack:
area = (i - stack[-1]-1) * h[last]
else:
area = i * h[last]
if area > top:
top = area
stack.append(i)
return top
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Largest Rectangle
You are viewing a single comment's thread. Return to all comments →
def largestRectangle(h): # Write your code here stack = [0] area = 0 top = 0 h.append(0) for i in range(1, len(h)):