# Maximum area rectangle in a histogram

Tags: , , , ,

A histogram is a diagram consisting of rectangles whose area is proportional to the frequency of a variable and whose width is equal to the class interval. Below is an example of a histogram.

Given a histogram, whose class interval is 1, find maximum area rectangle in it. Let me explain the problem in more details.

In the histogram above, there are at least 6 rectangles with areas 2, 1,5,6,2, and 3. Are there more rectangles? Yes, we can make more rectangles by combining some of these rectangles. A few are shown below.

Apparently, the largest area rectangle in the histogram in the example is 2 x 5 = 10 rectangle. The task is to find a rectangle with maximum area in a given histogram. The histogram will be given as an array of the height of each block, in the example, input will be [2,1,5,6,2,3].

## Maximum area rectangle: thoughts

First insight after looking at the rectangles above is: block can be part of a rectangle with a height less than or equal to its height. For each block of height h[i], check what all blocks on the left can be part of a rectangle with this block. All the blocks on the left side with a height greater than the current block height can be part of such a rectangle.
Similarly, all the blocks on the right side with a height greater than the current block height can be part of such a rectangle.
Idea is to calculate leftLimit and rightLimit and find the area `(rightLimit - leftLimit) * h[i]`.
Check if this area is greater than previously known area, then update the maximum area else, continue to the next block.

```class Solution {
public int largestRectangleArea(int[] heights) {

if(heights.length == 0) return 0;
int maxArea = Integer.MIN_VALUE;

for(int i=0; i<heights.length; i++){
//Find the left limit for current block
int leftLimit = findLeftLimit(heights, i);

//Find the right limit for current block
int rightLimit = findRightLimit(heights, i);

int currentArea = (rightLimit - leftLimit-1) * heights[i];
maxArea = Integer.max(maxArea, currentArea);
}

return maxArea;
}

private int findLeftLimit(int [] heights, int index){
int j = index-1;
while (j >= 0 && heights[j] >= heights[index]) j--;

return j;
}

private int findRightLimit(int [] heights, int index){
int j = index+1;
while (j < heights.length && heights[j] >= heights[index])
j++;

return j;
}
}
```

The time complexity of the implementation is O(n2); we will left and right of each block which will take n operations, we do it for n blocks and hence the complexity is quadratic. Can we optimize the time complexity?

If `heights[j] >= heights[i]` and leftLimit of index j is already known, can we safely say that it will also be the leftLimit of index i as well?
Can we say the same thing for rightLimit well? Answers to all the questions are yes. If we store the left and right limit for all indices already seen, we can avoid re-calculating them.

```class Solution {
public int largestRectangleArea(int[] heights) {

if(heights.length == 0) return 0;

int maxArea = Integer.MIN_VALUE;

//Finds left limit for each index, complexity O(n)
int [] leftLimit = getLeftLimits(heights);
//Find right limit for each index, complexity O(n)
int [] rightLimit = getRightLimits(heights);

for(int i=0; i<heights.length; i++){
int currentArea =
(rightLimit[i] - leftLimit[i] -1) * heights[i];
maxArea = Integer.max(maxArea, currentArea);
}

return maxArea;
}

private int[] getLeftLimits(int [] heights){

int [] leftLimit = new int[heights.length];
leftLimit[heights.length-1] = -1;

for(int i=0; i<heights.length; i++) {
int j = i - 1;
while (j >= 0 && heights[j] >= heights[i]) {
j = leftLimit[j];
}
leftLimit[i] = j;
}
return leftLimit;
}

private int[] getRightLimits (int [] heights){

int [] rightLimit = new int[heights.length];
rightLimit[heights.length-1] = heights.length;

for(int i=heights.length-2; i>=0; i--){
int j = i+1;
while(j<heights.length
&& heights[j] > heights[i]){
j = rightLimit[j];
}
rightLimit[i] = j;
}
return rightLimit;
}
}
```

The array `leftLimit`contains at index i the closest index j to the left of i such that `height[j] < height[i]`. You can think about each value of the array as a pointer (or an arrow) pointing to such `j` for every i. How to calculate `leftLimit[i]`? Just point the arrow one to the left and if necessary just follow the arrows from there, until you get to proper j. The key idea here to see why this algorithm runs in O(n) is to observe that each arrow is followed at most once.

### Largest area rectangle: stack-based solution

There is a classic method to solve this problem using the stack as well. Let’s see if we can build a stack-based solution using the information we already have. Let’s we do not calculate the area of the rectangle which includes the bar when we are processing it. When should we process it? Where should this bar be put on? If we want to create a rectangle with a height of this bar, we should find the left and right boundaries of such a rectangle. We should put this bar on a stack.
Now when you are processing bar j if height[j] is less than the bar on the top of the stack, we pop out the bar at the top. Why? Because this is the first bar on the right which has a height less than the height of the bar at top of the stack. This means if we want to make a rectangle with a height of the bar at the top of the stack, this index means the right boundary. This also gives away that all the blocks on the stack are in increasing order, as we never put a block which has a height less than the height of block at the top on to the stack. It means the next bar on the stack is the first bar which has a height lower than the bar at the top. To calculate the area of the rectangle with height as h[top], we need to take width as current index `j - stack.peek() - 1`

So the idea is that:

1. For each bar, take its height as the rectangle’s height. Then find the left and right boundaries of this rectangle.
2. The second top bar in the stack is always the first bar lower than the top bar on the stack on the left.
3. The bar that j points to is always the first bar lower than the top bar in the stack on the right.
4. After step 2 and 3, we know the left and right boundaries, then know the width, then know the area.
```private int maxAreaUsingStack(int[] heights){

Stack<Integer> s = new Stack<>();

int maxArea = 0;
for(int i=0; i<=heights.length; i++){
//Handling the last case
int h = i == heights.length ? 0 : heights[i];
while(!s.empty() && h < heights[s.peek()]){
int top = s.pop();
int leftLimit = s.isEmpty() ? -1 : s.peek();
int width = i-leftLimit-1;

int area = width * heights[top];
maxArea = Integer.max(area, maxArea);
}
s.push(i);
}
return maxArea;
}
```
The time complexity of the code is `O(n)` with an additional space complexity of `O(n)` If you are preparing for a technical interview in companies like Amazon, Facebook, etc and want help with preparation, please register for a coaching session with us.