Largest Rectangle in Histogram
Last updated
Last updated
Stack
, Dynamic Programming
, Divide and Conquer
Hard
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.
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
Given height = [2,1,5,6,2,3], return 10.
最基本的解法就是两重循环,遍历所有[i, j],寻找其中最矮bar,得出矩形面积,时间复杂度为O(n^2),不过这样的解法会得到TLE; 一个简单的改进是,只对合适的右边界(峰顶),往左遍历面积,这个优化只是比较有效的剪枝,算法仍然是O(n^2)。
而此题最佳解法则是利用一个Stack,主要思想是维护一个单调递增的栈(栈内存元素的下标),比较栈顶(下标对应元素)与当前元素,如果当前元素大于栈顶(下标对应元素)则入栈,否则一直出栈,并逐个计算面积(取最大值),直到栈顶(下标对应元素)小于当前元素。也就是说栈内(下标对应元素)都大于等于当前元素。
单调栈(stack)。维护一个单调递增栈,逐个将元素 push 到栈里。push 进去之前先把 >= 自己的元素 pop 出来。 每次从栈中 pop 出一个数的时候,就找到了往左数比它小的第一个数(当前栈顶)和往右数比它小的第一个数(即将入栈的数), 从而可以计算出这两个数中间的部分宽度 * 被pop出的数,就是以这个被pop出来的数为最低的那个直方向两边展开的最大矩阵面积。 因为要计算两个数中间的宽度,因此放在 stack 里的是每个数的下标。 https://www.jiuzhang.com/solution/largest-rectangle-in-histogram
理解Tips:相当于利用栈Stack,栈顶(下标对应元素)为矩形高度 (int h = height[stack.pop()];
),这个高度的矩形左右边界由栈中元素和当前index共同确定 (int w = stack.isEmpty() ? i : i - stack.peek() - 1;
)
对于i - stack.peek() - 1
的理解: 因为在获得矩形高度时,用了stack.pop(), 所以之后计算矩形宽度时,当前栈顶存的是左侧第一个比h
小的元素下标,而i
是右侧第一个比h
小的元素下标,因此w = i- stack.peek() - 1
.
如图所示,
图片来源:http://www.cnblogs.com/lichen782/p/leetcode_Largest_Rectangle_in_Histogram.html
需要注意的一点是,对原height[]的最后增加一位0,用来最终的i位置,确保所有的(下标对应的元素)都完成出栈(因为所有的元素都大于0)。
另附2003/2004 ACM University of Ulm Local Contest 题解之一:
Linear search using a stack of incomplete subproblems
We process the elements in left-to-right order and maintain a stack of information about started but yet unfinished subhistograms. Whenever a new element arrives it is subjected to the following rules. If the stack is empty we open a new subproblem by pushing the element onto the stack. Otherwise we compare it to the element on top of the stack. If the new one is greater we again push it. If the new one is equal we skip it. In all these cases, we continue with the next new element. If the new one is less, we finish the topmost subproblem by updating the maximum area w.r.t. the element at the top of the stack. Then, we discard the element at the top, and repeat the procedure keeping the current new element. This way, all subproblems are finished until the stack becomes empty, or its top element is less than or equal to the new element, leading to the actions described above. If all elements have been processed, and the stack is not yet empty, we finish the remaining subproblems by updating the maximum area w.r.t. to the elements at the top. For the update w.r.t. an element, we find the largest rectangle that includes that element. Observe that an update of the maximum area is carried out for all elements except for those skipped. If an element is skipped, however, it has the same largest rectangle as the element on top of the stack at that time that will be updated later. The height of the largest rectangle is, of course, the value of the element. At the time of the update, we know how far the largest rectangle extends to the right of the element, because then, for the first time, a new element with smaller height arrived. The information, how far the largest rectangle extends to the left of the element, is available if we store it on the stack, too. We therefore revise the procedure described above. If a new element is pushed immediately, either because the stack is empty or it is greater than the top element of the stack, the largest rectangle containing it extends to the left no farther than the current element. If it is pushed after several elements have been popped off the stack, because it is less than these elements, the largest rectangle containing it extends to the left as far as that of the most recently popped element. Every element is pushed and popped at most once and in every step of the procedure at least one element is pushed or popped. Since the amount of work for the decisions and the update is constant, the complexity of the algorithm is O(n) by amortized analysis.
Thanks to solution shared by @anton4 on LeetCode forum: https://leetcode.com/problems/largest-rectangle-in-histogram/discuss/28902/5ms-O(n)-Java-solution-explained-(beats-96)
基本思路就是对于每一个元素找到左右>= 该元素的下标,leftBound[], rightBound[]。
Basic idea is:
For any bar
i
the maximum rectangle is of widthr - l - 1
wherer
- is the rightmost index of the bar to the right with heighth[r] >= h[i]
, andl
- is the leftmost index of the bar to the left which heighth[l] >= h[i]
Then the area for the bari
is:h[i] * (r - l - 1)
The main trick is how to effectively calculate leftBound[] and rightBound[] arrays.
leftBound[]
stores the leftmost bar's index for each bar i
, which maintains height[l] >= height[i]
rightBound[]
stores the rightmost bar's index for each bar i
, which maintains height[r] >= height[i]
Trivial Solution
The trivial solution is to use O(n^2) solution and for each
i
element first find its left/right neighbour in the second inner loop just iterating back or forward.
Optimization using Memoization
The only line change shifts this algorithm from
O(n^2)
toO(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:
Naive Implementation - TLE
Pruning - AC (9 ms, faster than 89.45%)
O(n) Linear search using a stack of incomplete subproblems
*(Preferred) Another Stack implementation
(notice how boundary is handled: while (!stack.isEmpty() && (i == a.length || a[stack.peek()] > a[i]))
)
Another Implementation of Linear search using a stack of incomplete subproblems
Naive Implementation - calculate area by getting left bound and right bound for heights[i] (not reusing leftBound[] rightBound[]) - AC - (287 ms, faster than 16.64%)
Also check the follow-up solution for optimization using memoization
*Memoization by reusing the stored leftBound[], rightBound[] AC - 3ms faster than 96.33%