[LintCode] Largest Rectangle in Histogram

Posted Push your limit!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LintCode] Largest Rectangle in Histogram相关的知识,希望对你有一定的参考价值。

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.

 
Analysis:
Q: Think about how you would solve this problem by hand?  
A:  Given the rectangle requirement, for two consecutive heights height[i] and height[i + 1], if height[i + 1] < height[i],
we know that the rectangle whose height is height[i] has just reached its right end as it can not use height[i + 1] anymore.
 
So we calculate the area of all rectangles whose right end is index i. This means we need to calculate the area of all 
rectangles until we hit a height that is even smaller than height[i + 1]. In this situation, we know we just hit the left possible
boundary index of a rectangle.
 
This implies maintaining a non-decreasing stack of heights.  We keep pushing bigger or the the same heights into this stack
until we hit a smaller height than previous. Then we keep poping heights that are strictly bigger than the current height and 
calculate rectangle areas until we hit the left boundary(top of the stack is <= the current height).
 
Repeat the above process until we‘ve iterated all heights.  Any heights left in the stack are non-decreasing, pop these indices 
out and calcuate their corresponding rectangle areas.
 
To make the calculation easier, instead of storing the actual height in the stack, their indices in the array are stored. This way 
it is easier to calculate the length of each rectangle by probing the top of the stack.
 
O(n) runtime, O(n) space 
 1 public class Solution {
 2     public int largestRectangleArea(int[] height) {
 3         if(height == null || height.length == 0){
 4             return 0;
 5         }
 6         Stack<Integer> idxStack = new Stack<Integer>();
 7         int currIdx = 0; int area = 0; int maxArea = 0;
 8         while(currIdx < height.length){
 9             if(idxStack.isEmpty() || height[currIdx] >= height[idxStack.peek()]){
10                 idxStack.push(currIdx);
11                 currIdx++;
12             }
13             else{
14                 int top = idxStack.pop();
15                 int leftBoundIdx = idxStack.isEmpty() == true ? -1 : idxStack.peek();
16                 int rightBoundIdx = currIdx - 1;
17                 area = height[top] * (rightBoundIdx - leftBoundIdx);
18                 maxArea = Math.max(maxArea, area);
19             }
20         }
21         while(!idxStack.isEmpty()){
22             int top = idxStack.pop();
23             int leftBoundIdx = idxStack.isEmpty() == true ? -1 : idxStack.peek();
24             int rightBoundIdx = currIdx - 1;
25             area = height[top] * (rightBoundIdx - leftBoundIdx);
26             maxArea = Math.max(maxArea, area);
27         }
28         return maxArea;
29     } 
30 }

 

 

Related Problems
Maximal Rectangle 
Max Tree
Min Stack
 
 

以上是关于[LintCode] Largest Rectangle in Histogram的主要内容,如果未能解决你的问题,请参考以下文章

[LintCode] Largest Rectangle in Histogram

Lintcode5 Kth Largest Element solution 题解

Kth Largest Element II Lintcode

179. Largest Number

Largest Submatrix(动态规划)

Largest Number