题目:
思路:
https://blog.csdn.net/qq_28468707/article/details/103682528
https://www.jianshu.com/p/2b9a36a548fa 清晰
代码:
public int largestRectangleArea(int[] heights) {int[] heightadd = new int[heights.length + 1];for (int i=0;i<heights.length;i++) {heightadd[i] = heights[i];}heightadd[heights.length] = 0; // 为了末尾计算,Stack<Integer> stack = new Stack<>();stack.push(0);int maxArea = 0;for (int i=0;i<heightadd.length;i++) {while (!stack.isEmpty() && heightadd[stack.peek()] > heightadd[i]) { // 开始计算int high = stack.pop();int width = stack.isEmpty() ? i : i - stack.peek() - 1; int area = width * heights[high]; // 这里高和宽容易出错,注意maxArea = Math.max(maxArea, area);}stack.push(i);}return maxArea;}