Pattern 08 of 08 · monotonic-stack
Monotonic Stack & Top-K Heap
Maintain strictly increasing/decreasing stack invariants to resolve next greater/smaller elements or use heaps to maintain streaming top-K items.
- Builds on
- Two Pointers
- Unlocks
- The end of this branch.
See the invariant
Step through itPattern InvariantMonotonic Stack
Next Greater Element / Daily Temperatures (Days to Warmer Weather)
Step 1 of 12
Temperatures Array:
73°
Day 0ans: 074°
Day 1ans: 075°
Day 2ans: 071°
Day 3ans: 069°
Day 4ans: 072°
Day 5ans: 0Monotonic Stack
(Empty Stack)
Initial state: Empty monotonic decreasing stack. Result array initialized to 0s.
Speed: