class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int maxArea = 0;
stack<int> heights;
stack<int> indices;
heights.push(0);
indices.push(-1);
height.push_back(0);
for (vector<int>::size_type i = 0; i < height.size(); ++i)
{
if (height[i] > heights.top())
{
heights.push(height[i]);
indices.push(i);
}
else if (height[i] < heights.top())
{
int index = -1;
while (height[i] < heights.top())
{
index = indices.top();
int area = (i - index) * heights.top();
maxArea = max(area, maxArea);
heights.pop();
indices.pop();
}
if (height[i] > heights.top())
{
heights.push(height[i]);
indices.push(index);
}
}
}
return maxArea;
}
};
参考: http://blog.csdn.net/beiyetengqing/article/details/8258232