Largest Rectangle in Histogram:https://leetcode.com/problems/largest-rectangle-in-histogram/description/
public int largestRectangleArea(int[] height) {
int len = height.length;
Stack<Integer> s = new Stack<Integer>();
int maxArea = 0;
for(int i = 0; i <= len; i++){
int h = (i == len ? 0 : height[i]);
if(s.isEmpty() || h >= height[s.peek()]){
s.push(i);
}else{
int tp = s.pop();
maxArea = Math.max(maxArea, height[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
i--;
}
}
return maxArea;
}
对于每个bar,我们计算以此bar高度为矩形高的矩形面积,其中面积最大值即为所求值。这里我们用到了Stack类,当前index值减去堆栈中顶部index即为矩阵的长。
详解-vector,stack,queue,deque:https://www.cnblogs.com/tanfd/p/6138692.html