Largest Rectangle in Histogram
HARDDescription
Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
Example 1:
Input: heights = [2,1,5,6,2,3] Output: 10 Explanation: The above is a histogram where width of each bar is 1. The largest rectangle is shown in the red area, which has an area = 10 units.
Example 2:
Input: heights = [2,4] Output: 4
Constraints:
1 <= heights.length <= 1050 <= heights[i] <= 104
Approaches
Checkout 3 different approaches to solve Largest Rectangle in Histogram. Click on different approaches to view the approach and algorithm in detail.
Brute Force Approach
The most straightforward approach is to consider every possible rectangle. We can iterate through all possible pairs of bars and treat them as the left and right boundaries of a rectangle. For each pair, we find the minimum height within that range and calculate the area. The maximum area found among all pairs is the result.
Algorithm
- Initialize
maxArea = 0. - Iterate through each bar
ifrom0ton-1. - For each bar
i, treat it as the minimum height of a potential rectangle. - Expand to the left from
ito find the first bar shorter thanheights[i]. This determines the left boundary. - Expand to the right from
ito find the first bar shorter thanheights[i]. This determines the right boundary. - Calculate the width using the left and right boundaries.
- Calculate the area:
heights[i] * width. - Update
maxAreawith the maximum area found so far. - Return
maxArea.
This method iterates through each bar of the histogram and considers it as the bar of minimum height for a potential largest rectangle. For each bar i with height h = heights[i], we need to find the maximum possible width. This width is determined by extending to the left and right from bar i as long as the bars encountered are taller than or equal to h.
The algorithm proceeds as follows:
- Initialize a variable
maxAreato 0. - Iterate through the
heightsarray with an indexifrom 0 ton-1, wherenis the number of bars. - For each bar
i, we expand outwards to find the boundaries of the largest rectangle that hasheights[i]as its height.- Find the left boundary: Start a pointer
lfromiand move left (l--) as long asl >= 0andheights[l] >= heights[i]. - Find the right boundary: Start a pointer
rfromiand move right (r++) as long asr < nandheights[r] >= heights[i].
- Find the left boundary: Start a pointer
- The width of this rectangle is
r - l - 1. - Calculate the area:
area = heights[i] * (r - l - 1). - Update
maxArea = max(maxArea, area). - After iterating through all bars,
maxAreawill hold the area of the largest rectangle.
public class Solution {
public int largestRectangleArea(int[] heights) {
int maxArea = 0;
int n = heights.length;
for (int i = 0; i < n; i++) {
int left = i;
while (left - 1 >= 0 && heights[left - 1] >= heights[i]) {
left--;
}
int right = i;
while (right + 1 < n && heights[right + 1] >= heights[i]) {
right++;
}
int width = right - left + 1;
maxArea = Math.max(maxArea, heights[i] * width);
}
return maxArea;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- Requires no extra space.
- Inefficient for large inputs due to its quadratic time complexity.
- Leads to 'Time Limit Exceeded' on most online judges.
Code Solutions
Checking out 4 solutions in different languages for Largest Rectangle in Histogram. Click on different languages to view the code.
using System ; using System.Collections.Generic ; using System.Linq ; public class Solution { public int LargestRectangleArea ( int [] height ) { var stack = new Stack < int >(); var result = 0 ; var i = 0 ; while ( i < height . Length || stack . Any ()) { if (! stack . Any () || ( i < height . Length && height [ stack . Peek ()] < height [ i ])) { stack . Push ( i ); ++ i ; } else { var previousIndex = stack . Pop (); var area = height [ previousIndex ] * ( stack . Any () ? ( i - stack . Peek () - 1 ) : i ); result = Math . Max ( result , area ); } } return result ; } }Video Solution
Watch the video walkthrough for Largest Rectangle in Histogram
Similar Questions
5 related questions you might find useful
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.