← All templates

Monotonic Stack

Pro

Use when you need to find a nearest boundary efficiently (instead of scanning left/right for every element).

Keywords for identifying Monotonic Stack Problems

→ “Next / previous greater or smaller”

→ “First element to the left / right that is …”

→ “Nearest / closest …”

→ “Span / distance until …”

→ “Visible until blocked by …”

→ “Boundary / limit where condition breaks”

→ “For each index, find where something stops”

→ “for each element, find the first bigger/smaller on one side”

Unlock the Monotonic Stack template

Get the full code template and worked LeetCode examples for Monotonic Stack — plus every other pattern in the library — with a one-time $29 lifetime upgrade.

Upgrade to ProSee free Binary Search template →
Monotonic Stack · Templates · Grind Patterns