Monotonic Stack Pattern
A Monotonic Stack is a stack that maintains elements in either strictly increasing or strictly decreasing order. This pattern is used to find the next greater/smaller element efficiently.
When to Use This Pattern
- Finding next greater/smaller element
- Finding previous greater/smaller element
- Problems involving temperature, stock prices, or heights
- Largest rectangle in histogram problems
Key Concepts
- Monotonically Increasing: Elements increase from bottom to top
- Monotonically Decreasing: Elements decrease from bottom to top