Largest Rectangle in Histogram

Given an array 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: Input: heights = [2,1,5,6,2,3] Output: 10 Explanation: The largest rectangle is formed by the bars 5 and 6, with area 10. Pattern focus: Monotonic Increasing Stack for Span / boundary discovery.

Input Format

heights = histogram bar heights

Output Format

maximum rectangle area in the histogram

Constraints

  • 1 <= heights.length <= 10^5
  • 0 <= heights[i] <= 10^4

Examples

Example 1:

Input:

heights = [2,1,5,6,2,3]

Output:

10

Explanation:

The rectangle covering heights 5 and 6 with width 2 gives the best area.

Example 2:

Input:

heights = [2,4]

Output:

4

Explanation:

The tallest single bar may be optimal when the width is small.

Loading...
Largest Rectangle in Histogram