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.
public static long largestRectangle(List<Integer> h) {
//1. Iterate the list of height. Set the max height of the building as first element.
// then check the height difference to the adjacent building.
// If the height is same or more, then go to next building.
//Each time find the max rectangle and update the value.
long maxRectangle = 0L;
for(int i = 0; i < h.size(); i++){
int maxHt = h.get(i);
int maxlen = 1;
for(int j = i+1; j < h.size(); j++){
if(h.get(j) - h.get(j-1) < 0){
if(maxHt > h.get(j))maxHt = h.get(j);
}
maxlen+= 1;
if(maxRectangle < maxHt*maxlen) maxRectangle = maxHt*maxlen;
}
}
return maxRectangle;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Largest Rectangle
You are viewing a single comment's thread. Return to all comments →
Java 8 Solution: