[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

时间: 2024-08-29 22:16:15

[LintCode] Largest Rectangle in Histogram的相关文章

(每日算法)Leetcode -- Largest Rectangle in Histogram(最大实心矩形)

思路:如果时间复杂度要求是O(n 2 )的话,解法比较多也比较好理解.比如可以遍历,对于当前 i 位置上的立柱,计算出以这个i 立柱结尾的最大矩形,然后求出总的最大矩形. 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

(数组)Largest Rectangle in Histogram(栈解问题)

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 heigh

LeetCode: Largest Rectangle in Histogram [084]

[题目] 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 lar

84. Largest Rectangle in Histogram *HARD* 柱状图求最大面积 85. Maximal Rectangle *HARD*

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. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The large

Leetcode:Largest Rectangle in Histogram 最大矩形面积

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 heigh

C++]LeetCode: 133 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 larg

LeetCode &quot;Largest Rectangle in Histogram&quot; - TRICKY MONO-QUEUE

I got a DP solution first which is O(n^2). Apparently it is not a optimized one - think about: it is linear value space. There must be O(n) solution. And yes there is: http://fisherlei.blogspot.com/2012/12/leetcode-largest-rectangle-in-histogram.html

【leetcode刷题笔记】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

[LeetCode OJ] 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