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.
Brute force solution without stacks that passes some tests but only fails with time limit exceeded. So, I'd say logic is correct.
This is O(N!) algorithm, it generates all the possible arrangements of size 1, size 2, .... size N.
Absolutely not optimal but it uncovers the neccessary thought pattern to solve optimally.
size = 1
while size <= len(h):
for i in range(len(h)):
minHeight = float("inf")
if i + size > len(h):
break
for j in range(i,i+size):
minHeight = min(minHeight, h[j])
maxRectangleArea = max(maxRectangleArea, (minHeight * size))
size += 1
return maxRectangleArea
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 →
Brute force solution without stacks that passes some tests but only fails with time limit exceeded. So, I'd say logic is correct. This is O(N!) algorithm, it generates all the possible arrangements of size 1, size 2, .... size N. Absolutely not optimal but it uncovers the neccessary thought pattern to solve optimally.
def largestRectangle(h): maxRectangleArea = float("-inf")