day 60 84. 柱状图中最大的矩形
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。
class Solution { public int largestRectangleArea(int[] heights) { if (heights == null || heights.length == 0) { return 0; } Stack<Integer> stack = new Stack<>(); int max = 0; for (int i = 0; i <= heights.length; i++) { int cur = i == heights.length ? -1 : heights[i]; while (!stack.isEmpty() && cur <= heights[stack.peek()]) { int h = heights[stack.pop()]; int w = stack.isEmpty() ? i : i - stack.peek() - 1; max = Math.max(max, h * w); } stack.push(i); } return max; } }