题目地址:点击打开链接
对于任意一个位于位置n的高度height,找在n前面的比height低的最接近n的数start,找在n后面比height高的最接近n的数end,如果max<height*(end-start-1),则更新max的值
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int i,size=height.size();
multimap<int,int> imap;
for(i=0;i<size;++i)
imap.insert(make_pair(height[i],i+1));
map<int,int> index;
index.insert(make_pair(0,1));
index.insert(make_pair(size+1,1));
map<int,int>::iterator iter=imap.begin();
int max_area=0;
while(iter!=imap.end())
{
int n=iter->second;
pair<map<int,int>::iterator,bool> pa=index.insert(make_pair(n,1));
map<int,int>::iterator it1=pa.first,it2=it1;
int start,end;
--it1;
start=it1->first;
++it2;
end=it2->first;
if((iter->first*(end-start-1))>max_area)
max_area=iter->first*(end-start-1);
++iter;
}
return max_area;
}
};