Skip to interview prep
Apexed
Problem list118 / 125

118. Largest Rectangle in a Histogram

HardStacks

heights gives the heights of adjacent bars, each 1 unit wide. Return the area of the largest rectangle that fits entirely inside the histogram.

Example 1

Input: heights = [2, 1, 5, 6, 2, 3]
Output: 10
Explanation: Bars of height 5 and 6 give a 5 × 2 rectangle.

Constraints

  • 1 ≤ heights.length ≤ 10⁵
  • 0 ≤ heights[i] ≤ 10⁴

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 6 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Largest Rectangle in a Histogram — coding interview practice — Apexed