1.题目描述
Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]
.
The largest rectangle is shown in the shaded area, which has area = 10
unit.
Example:
Input: [2,1,5,6,2,3]
Output: 10
2.Solutions
For any bar i
the maximum rectangle is of width r - l - 1
where r - is the last coordinate of the bar to the right with height h[r] >= h[i]
and l - is the last coordinate of the bar to the left which height h[l] >= h[i]
So if for any i
coordinate we know his utmost higher (or of the same height) neighbors to the right and to the left, we can easily find the largest rectangle:
1 | int maxArea = 0; |
The main trick is how to effectively calculate lessFromRight
and lessFromLeft
arrays. The trivial solution is to use O(n^2) solution and for each i
element first find his left/right heighbour in the second inner loop just iterating back or forward:
1 | for (int i = 1; i < height.length; i++) { |
The only line change shifts this algorithm from O(n^2) to O(n) complexity: we don’t need to rescan each item to the left - we can reuse results of previous calculations and “jump” through indices in quick manner:
1 | while (p >= 0 && height[p] >= height[i]) { |
Here is the whole solution:
1 | public static int largestRectangleArea(int[] height) { |
使用栈:
For explanation, please see http://www.geeksforgeeks.org/largest-rectangle-under-histogram/
1 | public class Solution { |
OP’s Note: Two years later I need to interview again. I came to this problem and I couldn’t understand this solution. After reading the explanation through the link above, I finally figured this out again.
Two key points that I found helpful while understanding the solution:
- Do push all heights including 0 height.
i - 1 - s.peek()
uses the starting index whereheight[s.peek() + 1] >= height[tp]
, because the index on top of the stack right now is the first index left oftp
with height smaller than tp’s height.