Largest Rectangle in Histogram
Monotonic stack finds each bar's left/right boundary in O(n).
Problem Statement
Given an array of integers heights representing the histogram's bar heights where the width of each bar is 1, return the area of the largest rectangle in the histogram.
Examples
Example 1
Explanation: The rectangle spans bars 2-3 (heights 5 and 6), area = 5×2 = 10.
Example 2
Constraints
- 1 ≤ heights.length ≤ 10⁵
- 0 ≤ heights[i] ≤ 10⁴
Solutions
For each pair (i, j), the rectangle height is min(heights[i..j]) and width is j-i+1. O(n²) or O(n³).
Related Concepts
Deepen your understanding with these related topics from our AI Glossary:
Want to master the core concepts?
Our free AI Glossary covers 190+ topics — from Monotonic Stack to Dynamic Programming, Machine Learning, SQL, and more. Structured learning tracks for every level.
Stuck? Ask AI to explain it step by step.
Ask Claude, GPT-4o, or Gemini to debug your code, generate test cases, or walk through the intuition. 39+ models. Pay only on days you use it — no subscription required.
Free to start · No credit card required to explore
Get Started Free