Facebook Pixel

Largest Rectangle in Histogram

You are given an array heights where heights[i] is the height of the i-th bar in a histogram. Every bar has width 1 and the bars sit side by side.

Find the area of the largest rectangle that fits entirely inside the histogram. The rectangle can span any range of consecutive bars, and its height is limited by the shortest bar in that range.

For any bar, the widget shades the widest rectangle capped at that bar's height — the answer is the largest such rectangle over the whole histogram.

Input & Output
Input
heights — the height of each width-1 bar, left to right
Output
the area of the largest rectangle that fits inside the histogram

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro