84. 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.

For example,
Given heights = [2,1,5,6,2,3],
return 10.

b'
\n\n

Summary

\n

We need to find the rectangle of largest area that can be formed by using the given bars of histogram.

\n

Solution

\n
\n

Approach #1 Brute Force [Time Limit Exceeded]

\n

Firstly, we need to take into account the fact that the height of the rectangle\nformed between any two bars will always be limited by the height of the shortest\nbar lying between them which can be understood by looking at the figure below:

\n

\n

Thus, we can simply start off by considering every possible\npair of bars and finding the area of the rectangle formed between them using the\nheight of the shortest bar lying between them as the height\n and the spacing between\nthem as the width of the rectangle. We can thus, find the required rectangle with the\n maximum area.

\n\n

Complexity Analysis

\n
\n
• Time complexity : . We have to find the minimum height bar lying\nbetween every pair .
• \n
• Space complexity : . Constant space is used.
• \n
\n
\n

Approach #2 Better Brute Force[Time Limit Exceeded]

\n

Algorithm

\n

We can do one slight modification in the previous approach to optimize it to some extent.\n Instead of taking every possible pair and then finding the bar of minimum height\n lying between them everytime, we can find the bar of minimum height for\n current pair by using the minimum height bar of the previous pair.

\n

In mathematical terms, , where \n refers to the height of the th bar.

\n\n

Complexity Analysis

\n
\n
• \n

Time complexity : . Every possible pair is considered

\n
• \n
• \n

Space complexity : . No extra space is used.

\n
• \n
\n
\n

Approach #3 (Divide and Conquer Approach) [Time Limit Exceeded]

\n

Algorithm

\n

This approach relies on the observation that the rectangle with maximum area will be\nthe maximum of:

\n
\n
1. \n

The widest possible rectangle with height equal to the height of the shortest bar.

\n
2. \n
3. \n

The largest rectangle confined to the left of the shortest bar(subproblem).

\n
4. \n
5. \n

The largest rectangle confined to the right of the shortest bar(subproblem).

\n
6. \n
\n

Let\'s take an example:

\n
[6, 4, 5, 2, 4, 3, 9]\n
\n

Here, the shortest bar is of height 2. The area of the widest rectangle using this\n bar as height is 2x8=16. Now, we need to look for cases 2 and 3 mentioned above.\n Thus, we repeat the same process to the left and right of 2. In the left of 2, 4\n is the minimum, forming an area of rectangle 4x3=12. Further, rectangles of area 6x1=6 and\n 5x1=5 exist in its left and right respectively. Similarly we find an area of 3x3=9, 4x1=4 and 9x1=9\n to the left of 2. Thus, we get 16 as the correct maximum area. See the figure below for further clarification:

\n

\n\n

Complexity Analysis

\n
\n
• \n

Time complexity :

\n

Average Case:.

\n

Worst Case: . If the numbers in the array are sorted,\n we don\'t gain the advantage of divide and conquer.

\n
• \n
• \n

Space complexity : . Recursion with worst case depth .

\n
• \n
\n
\n

Approach #4 (Better Divide and Conquer) [Accepted]

\n

Algorithm

\n

You can observe that in the Divide and Conquer Approach, we gain the advantage, since\n the large problem is divided into substantially smaller subproblems. But, we won\'t gain\n much advantage with that approach if the array happens to be sorted in either\n ascending or descending order, since every time we need to find the minimum number in a\n large subarray . Thus, the overall complexity becomes in the worst case.\n We can reduce the time complexity by using a Segment Tree to find the minimum every time which\n can be done in time.

\n

\n

Complexity Analysis

\n
\n
• \n

Time complexity : . Segment tree takes \n times.

\n
• \n
• \n

Space complexity : . Space required for Segment Tree.

\n
• \n
\n
\n

Approach #5 (Using Stack) [Accepted]

\n

Algorithm

\n

In this approach, we maintain a stack. Initially, we push a -1 onto the stack to mark the end.\nWe start with the leftmost bar and keep\npushing the current bar\'s index onto the stack until we get two successive numbers\n in descending order, i.e. until we get . Now, we start popping the\n numbers from the stack until we hit a number on the stack such that .\n Every time we pop, we find out the area of rectangle formed using the current element as the height\n of the rectangle and the difference between the the current element\'s index pointed to in the original array and the element as the width\n i.e. if we pop an element and i is the current index to which we are pointing in the original array, the current area of the rectangle will be considered as .

\n

Further, if we reach the end of the array, we pop all the elements of the\n stack and at every pop, this time we use the following equation to find the area:\n , where refers to the\n element just popped. Thus, we can get the area of the\n of the largest rectangle by comparing the new area found everytime.

\n

The following example will clarify the process further:\n [6, 7, 5, 2, 4, 5, 9, 3]

\n

!?!../Documents/84_Largest_Rectangle.json:1000,563!?!

\n\n

Complexity Analysis

\n
\n
• \n

Time complexity : . numbers are pushed and popped.

\n
• \n
• \n

Space complexity : . Stack is used.

\n
• \n
\n

Analysis written by: @vinod23

\n
'