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.
For example:
Consider the histogram with heights [2, 1, 5, 6, 2, 3]
. The largest rectangle has an area of 10 (height 5, width 2, between indices 2 and 3).
Another example:
Consider the histogram with heights [2, 4]
. The largest rectangle has an area of 4 (height 2, width 2).
Can you devise an algorithm to efficiently find the largest rectangular area in a given histogram?
Constraints:
1 <= heights.length <= 10^5
0 <= heights[i] <= 10^4
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem involves checking every possible rectangle that could exist within the histogram. We essentially look at every possible width and height combination that the histogram allows and determine the rectangle's area. By comparing all of these areas, we can find the largest one.
Here's how the algorithm would work step-by-step:
def largest_rectangle_in_histogram_brute_force(heights):
largestArea = 0
numberOfBars = len(heights)
for rightEdge in range(numberOfBars):
# Each bar considered as right edge
for leftEdge in range(rightEdge, -1, -1):
minimumHeight = heights[leftEdge]
for currentBar in range(leftEdge, rightEdge + 1):
#Find shortest bar, height of rectangle
minimumHeight = min(minimumHeight, heights[currentBar])
width = rightEdge - leftEdge + 1
area = minimumHeight * width
# Check if the area is the largest
if area > largestArea:
largestArea = area
return largestArea
The goal is to find the biggest rectangular area within a histogram. We use a clever technique to keep track of potential rectangles, building them up as we go and figuring out their area when we find a boundary.
Here's how the algorithm would work step-by-step:
def largestRectangleArea(heights):
max_area = 0
stack = []
heights_length = len(heights)
for i in range(heights_length + 1):
# Add a zero to the end to pop all elements in the stack
while stack and (i == heights_length or heights[stack[-1]] > heights[i]):
height = heights[stack.pop()]
# Calculate width, if stack is empty, width starts from 0
if stack:
width = i - stack[-1] - 1
else:
width = i
max_area = max(max_area, height * width)
stack.append(i)
return max_area
Case | How to Handle |
---|---|
Null or empty input array | Return 0 immediately as no rectangle can be formed. |
Input array with a single element | Return the value of the single element as the area. |
Input array with all elements being zero | Return 0, as the largest possible rectangle area is 0. |
Input array with all elements being identical and non-zero | The largest rectangle area will be the value of the element multiplied by the array length. |
Input array sorted in ascending order | The algorithm should correctly calculate areas based on heights multiplied by varying widths. |
Input array sorted in descending order | The algorithm correctly handles monotonically decreasing histograms. |
Large input array to test for time complexity | A stack-based approach should have O(n) time complexity. |
Input array containing very large integer values, potentially leading to integer overflow when calculating area | Use a larger data type (long) for area calculations to prevent overflow. |