Largest Rectangle in Histogram

hard · monotonic-stack, stack, array

Largest Rectangle in Histogram

Given an array heights representing bar heights in a histogram, return the area of the largest rectangle that can be formed.

Function signature

func LargestRectangleArea(heights []int) int

Example

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

Constraints

  • 0 <= len(heights) <= 200_000
  • 0 <= heights[i] <= 1_000_000_000

Notes

  • Use a monotonic increasing stack of indices.
  • A sentinel height of 0 flushes remaining bars.
Run tests to see results
No issues detected