class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
heights.insert(heights.begin(), 0); // 哨兵
int ret = 0 ;
stack<int> sta;
sta.push(0);
for (int i = 1; i < heights.size(); ++i){
while (heights[i] < heights[sta.top()]){
int value = heights[sta.top()];
sta.pop();
ret = max (ret, value * (i - sta.top() - 1));
}
sta.push(i);
}
while (sta.size() > 1){
int value = heights[sta.top()];
sta.pop();
ret = max (ret, value * ((int)heights.size() - sta.top() - 1));
}
return ret;
}
};