Largest Rectangle in Histogram (Variant)

viaLeetCode

Problem: Given an array of integers representing the heights of bars in a histogram where the width of each bar is 1, find the area of the largest rectangle that can be formed within the histogram.

Constraints: Array length up to 10^5, heights >= 0.

Example: heights = [2,1,5,6,2,3] -> largest rectangle area = 10.

Approach: Use a monotonic increasing stack of indices. For each bar, pop from the stack while the top has a greater height than the current bar, computing the area using the popped height and the width spanned between the new stack top and the current index. O(n) time, O(n) space.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account