给定 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; } }
标签:int,heights,60,柱状图,矩形,day,84 From: https://www.cnblogs.com/libertylhy/p/17364909.html