← All problems

Largest Rectangle in Histogram

Hard stacksmonotonic-stack LeetCode ↗
Given an array of bar heights in a histogram, find the area of the largest rectangle that can be formed within the bars. The brute force is O(n^2); find the linear-time stack solution.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(n), Space O(n)

Common pitfalls

  • Width miscalculation (use current index - stack top - 1)
  • Forgetting the sentinel

Your solution

Log in to save your code and track progress.