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.
Here's my solution in C++. It's a little messy, but it works alright :)
longlargestRectangle(vector<int>h){intlargestArea=0;intcurrentArea=0;intlengthWithH=0;for(inti=0;i<h.size();i++)// Check each height{if(i>0&&h.at(i)==h.at(i-1))// if height is same as previous, no need to checkcontinue;lengthWithH=1;// count selfboolleft=true;boolright=true;for(intj=1;j<h.size();j++){if(!left&&!right)// if no more to check, break loop{break;}if(left){if(i-j>=0&&h.at(i-j)>=h.at(i))// check if building left is tall enough, and in boundslengthWithH++;elseleft=false;}if(right){if(i+j<h.size()&&h.at(i+j)>=h.at(i))// check if building right is tall enough, and in boundslengthWithH++;elseright=false;}}currentArea=h.at(i)*lengthWithH;// find areaif(currentArea>largestArea)// if new area is larger, set it to largestlargestArea=currentArea;}returnlargestArea;}
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 →
Here's my solution in C++. It's a little messy, but it works alright :)