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.
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
- NP-complete
- Open addressing
- Optimal substructure
- Overlapping subproblems
- P versus NP
- Polynomial time
- Prefix sum
- Prim's algorithm
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.