#C13291. Largest Rectangle in Histogram
Largest Rectangle in Histogram
Largest Rectangle in Histogram
You are given a histogram where the width of each bar is 1 and the heights of the bars are given by an array heights. Your task is to compute the area of the largest rectangle that fits entirely within the histogram.
The area of a rectangle is calculated by:
where the height is determined by the smallest bar in the rectangle and the width is the number of contiguous bars included.
For example, given the histogram with heights = [2, 1, 5, 6, 2, 3], the largest rectangle has an area of 10.
inputFormat
The first line contains an integer n representing the number of bars in the histogram.
The second line contains n space-separated integers representing the heights of the histogram bars.
Example:
6
2 1 5 6 2 3
outputFormat
Output a single integer representing the area of the largest rectangle that can be formed in the histogram.
Example:
10
1
5
5
</p>