Largest Rectangular Area in a Histogram

Program for Largest Rectangular Area in a Histogram

You are provided with an array of integers called heights, where each element heights[i] represents the height of a bar in a histogram. The width of each bar is fixed at 1 unit. The histogram consists of several vertical bars placed side by side, creating a continuous chart.

Your goal is to determine the maximum area of a rectangle that can be formed using one or more consecutive bars in the histogram. The rectangle’s area is calculated by multiplying the height of the shortest bar in the selected range by the total number of bars considered (width).

You need to return the largest possible rectangular area that can be formed from any combination of bars in the histogram.

Car Fleet Problem

Constraints:

  • 1 <= heights.length <= 1000.
  • 0 <= heights[i] <= 1000

Program for Largest Rectangular Area in Histogram Solution

Recommendation for Time and Space Complexity –You should aim for a solution with O(n) time and O(n) space, where n is the size of the input array.

Hints for solving problems

Hint 1 :

A rectangle has a height and a width. Can you visualize how rectangles are formed in the given input? Considering one bar at a time might help. We can try to form rectangles by going through every bar and current bar’s height will be the height of the rectangle. How can you determine the width of the rectangle for the current bar being the height of the rectangle? Extending the current bar to the left and right might help determine the rectangle’s width.

Hint 2 :

For a bar with height h, try extending it to the left and right. We can see that we can’t extend further when we encounter a bar with a smaller height than h. The width will be the number of bars within this extended range. A brute force solution would be to go through every bar and find the area of the rectangle it can form by extending towards the left and right. This would be an O(n^2) solution. Can you think of a better way? Maybe precomputing the left and right boundaries might be helpful.

There are mainly 4 approach to solve this problem-

  1. Brute Force Method
  2. Divide and Conquer(Segment Tree) Method
  3. Stack Method
  4. Stack(Optimal) Method
  5. Stack(One Pass) Method

1. Brute Force Method

Check all possible rectangles by iterating through each bar and calculating the area, resulting in a time complexity of O(n^2).

  • Time complexity: O(n^2)
  • Space complexity: O(1)

Code

2. Divide and Conquer(Segment Tree) Method

Recursively divide the histogram into smaller parts, find the minimum bar in each part, and calculate the maximum area. This method uses a segment tree for efficiency.

  • Time complexity: O(n logn)
  • Space complexity: O(n)

Code

3. Stack Method

Use a stack to keep track of bars and calculate areas when a smaller bar is encountered, maintaining a time complexity of O(n).

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

4. Stack(Optimal) Method

Optimize the stack approach by adding a sentinel bar at the end to simplify area calculations, ensuring all remaining bars are processed in one pass.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

5. Stack(One Pass) Method

Traverse the entire histogram once while maintaining a stack to store indices, calculating the largest rectangle area in a single pass.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

More Articles