解题思路:
单调栈
代码参考了:
http://www.cnblogs.com/lichen782/p/leetcode_Largest_Rectangle_in_Histogram.html
提交代码:
class Solution {
public int largestRectangleArea(int[] heights) {
Stack<Integer> stack=new Stack<Integer>();
int[] h=new int[heights.length+1];
h=Arrays.copyOf(heights,heights.length+1);
int i=0;
int pos;
int curSize,maxSize=0;
while(i<h.length) {
if(stack.isEmpty()||h[stack.peek()]<=h[i])
stack.push(i++);
else {
pos=stack.pop();
curSize=stack.isEmpty()?h[pos]*i:h[pos]*(i-stack.peek()-1);
if(curSize>maxSize)
maxSize=curSize;
}
}
return maxSize;
}
}
运行结果: