AI engineering glossary · Data structures algorithms

What is monotonic stack?

Monotonic stack: A stack kept in increasing or decreasing order by removing items that violate the order before each new push. It answers 'next greater element' style questions for all positions in O(n) total time.

Level: advancedTopic: Data structures algorithms

Monotonic stack: an example

For temperatures [30, 32, 31, 35], a stack finds that the next warmer day after 30 is 32 and after 31 is 35, each item pushed and popped once.

Related terms

More Data structures algorithms terms

Where you learn it

Monotonic stack is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.