Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
10 unit.[2,1,5,6,2,3],10.
int largestRectangleArea(vector<int> &height) { int maxarea = 0, index = 0, n = height.size(); stack<int> s; while(index < n){ if(s.empty() || height[s.top()] <= height[index]) s.push(index++); else{ int topIndex = s.top();s.pop(); int topOfArea = height[topIndex]*(s.empty() ? index : index-s.top()-1); maxarea = max(topOfArea,maxarea); } } while(!s.empty()){ int topIndex = s.top();s.pop(); int topOfArea = height[topIndex]*(s.empty() ? index : index-s.top()-1); maxarea = max(topOfArea,maxarea); } return maxarea; }

Leetcode Largest Rectangle in Histogram,布布扣,bubuko.com
Leetcode Largest Rectangle in Histogram
原文:http://www.cnblogs.com/xiongqiangcs/p/3811114.html