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.
#!/bin/python3importmathimportosimportrandomimportreimportsys## Complete the 'largestRectangle' function below.## The function is expected to return a LONG_INTEGER.# The function accepts INTEGER_ARRAY h as parameter.#deflargestRectangle(heights):# Write your code herestack=list()index=0largest_rectangle=0whileindex<len(heights):if(notstack)or(heights[stack[-1]]<=heights[index]):stack.append(index)index+=1else:top_of_stack=stack.pop()area=(heights[top_of_stack]*((index-stack[-1]-1)ifstackelseindex))largest_rectangle=max(largest_rectangle,area)whilestack:top_of_stack=stack.pop()area=(heights[top_of_stack]*((index-stack[-1]-1)ifstackelseindex))largest_rectangle=max(largest_rectangle,area)returnlargest_rectangleif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')n=int(input())h=list(map(int,input().rstrip().split()))result=largestRectangle(h)fptr.write(str(result)+'\n')fptr.close()
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 →
Largest Rectangle solution in Python