Skip to main content

Monotonic Stack

Learn Monotonic Stack (Stack, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

What is a Monotonic Stack? A stack maintained so its elements stay in strictly increasing (or decreasing) order from bottom to top - enforced by POPPING any element that would break that order before pushing a new one. It's the go-to tool whenever a problem asks "what's the next/previous element that's bigger/smaller than me?" across a whole array, in O(n) total instead of O(n²). Increasing stack (bottom→top gets bigger): pop while the top is ≥ the incoming element - used for "next SMALLER element". Decreasing stack (bottom→top gets smaller): pop while the top is ≤ the...

More lessons in Stack