Problem 50 of 198HardLeetCode

Largest Rectangle in Histogram

Stack

Asked at 12 companies

7 recent

  • Amazon
  • Google
  • Microsoft
  • tcs
  • Bloomberg
  • Accenture
  • Meta
  • thoughtspot
  • Coupang
  • Myntra
+2 more
  • Visa
  • BitGo

Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.

  1. 1Basics
  2. 2Paper
  3. 3Code it
  4. 4Watch
  5. 5Visualize
  6. 6Accepted
  7. 7Review

Go step by step: try it yourself before the video and the visual. Progress saves on this device.

0 / 160 pointsSolved (Hard) 0/70Reviews 0/50Mastered 0/25Practice steps 0/15

Step 1

Know these before you start

Practise each one here, or tick what you already know

Concept video
Learn ›Practise ✎Learn ›Practise ✎Learn ›Practise ✎

Step 2

Try it on paper

5–10 minutes, no code yet

In the wild · Undo and the back button: Every action is pushed on a stack; undo pops the latest one. Code editors use a stack the same way to check that brackets match.

About this problem

Last-in, first-out. Perfect when the next item depends on the most recent unfinished one.

Try it in the editor below for 20–30 minutes: passing its tests counts as solved and starts your reviews. The full statement is on LeetCode.

  1. Example 1

    Input
    heights = [2, 1, 5, 6, 2, 3]
    Output
    10
    Walk through it step by step(14 steps · shows the approach, so try first)
    1. Keep a of bars with rising heights. When a shorter bar arrives, the taller bars on the stack can't extend further right — pop each and measure the widest rectangle it could make.
    2. Push bar 0 (height 2) — the stack still rises.
    3. Bar 1 is shorter, so bar 2 stops here. It spans 1 bar wide: 2 × 1 = 2 — new best!
    4. Push bar 1 (height 1) — the stack still rises.
    5. Push bar 2 (height 5) — the stack still rises.
    6. Push bar 3 (height 6) — the stack still rises.
    7. Bar 2 is shorter, so bar 6 stops here. It spans 1 bar wide: 6 × 1 = 6 — new best!
    8. Bar 2 is shorter, so bar 5 stops here. It spans 2 bars wide: 5 × 2 = 10 — new best!
    9. Push bar 4 (height 2) — the stack still rises.
    10. Push bar 5 (height 3) — the stack still rises.
    11. End of bars, so bar 3 stops here. It spans 1 bar wide: 3 × 1 = 3.
    12. End of bars, so bar 2 stops here. It spans 4 bars wide: 2 × 4 = 8.
    13. End of bars, so bar 1 stops here. It spans 6 bars wide: 1 × 6 = 6.
    14. The largest rectangle has area 10.

    ▶ Watch these steps animate

  2. Example 2

    Just one item
    Input
    heights = [0]
    Output
    0
  3. Example 3

    All the same
    Input
    heights = [1, 1, 1, 1]
    Output
    4

Code it here

Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor

Step 6

Get it accepted

All test cases pass

Step 7

Review it so it sticks

Re-solve from memory after

The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.

Your notes

Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.

Solve it yourself before you look

Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.

Hidden: optimal solution

Stuck? One hint at a time.

    Srujan's notes aren't here yet. When he solves this one in his journal, his own writeup and solutions show up on this page too.