Prerequisites
Before reading this article, you need to learn:
This article provides more variations of the monotonic stack template. Some classic problems discussed later can be abstracted into the following standard scenarios. You can directly copy and paste the code to solve them.
Variants of Monotonic Stack Template
Next Greater Element
Next Greater or Equal Element
Next Smaller Element
Next Smaller or Equal Element
Previous Greater Element
Previous Greater or Equal Element
Previous Smaller Element
Previous Smaller or Equal Element
Exercises
Next Greater Node In Linked List
Solution
Number of Visible People in a Queue
Solution
Final Prices With a Special Discount in a Shop
Solution
Online Stock Span
Solution
Remove K Digits
Solution