>>>>>>
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int maxarea=0;
stack<int>s;
for(int i=0;i<height.size();i++)
{
if(s.empty())
s.push(i);
else
{
while(!s.empty())
{
if(height[s.top()]<=height[i])
{
s.push(i);
break;
}
else
{
int index=s.top();
s.pop();
int leftwidth=s.empty()?index:index-s.top()-1;
int rightwidth=i-index-1;
int a=height[index]*(leftwidth+rightwidth+1);
maxarea=max(maxarea,a);
}
}
}
if(s.empty())
s.push(i);
}
int rightindex=s.empty()?0:s.top()+1;
while(!s.empty())
{
int index=s.top();
s.pop();
int leftwidth=s.empty()?index:index-s.top()-1;
int rightwidth=rightindex-index-1;
int a=height[index]*(leftwidth+rightwidth+1);
maxarea=max(maxarea,a);
}
return maxarea;
}
};