码迷,mamicode.com
首页 > 其他好文 > 详细

[LintCode] Largest Rectangle in Histogram

时间:2017-09-05 13:25:33      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:keep   dia   his   empty   hose   alt   represent   math   about   

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

标签:keep   dia   his   empty   hose   alt   represent   math   about   

原文地址:http://www.cnblogs.com/lz87/p/7477789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!