Asked about The high-frequency patterns

What is a monotonic stack for?

An answer that works

Next-greater and next-smaller element questions in linear time. You keep the stack ordered, and when a new element breaks the order you have found the answer for everything it displaces.

Reading an answer is not being able to give one. The section that teaches this is The high-frequency patterns, 16 hours of Data structures & algorithms.

Learn this properly