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.
- Prepare
- Data Structures
- Stacks
- Largest Rectangle
- Discussions
Largest Rectangle
Largest Rectangle
Sort by
recency
|
612 Discussions
|
Please Login in order to post a comment
def largestRectangle(h):
why my approach is incorrect?
def largestRectangle(h): i=0 largestArea = 0 height = math.inf while h: if h[-1]>height: h.pop(-1) else: height= h.pop(-1) i+=1 area = height*i if area>largestArea: largestArea = area return largestArea
find the previous smaller element and the next smaller element in O(n) time
total time requires 2 pass over the vector. so O(n) for total time.
My code is working, but can anyone please explain to me in a simple way what this problem has to do with stacks?