What is a monotonic stack, and what kinds of problems does it solve?
A monotonic stack is a stack whose elements are kept in a fixed order by value, usually decreasing from bottom to top. Every time you push, you first pop the elements that would break that order. Because the order is maintained, the top of the stack is always the element you are most likely to need, which is what removes the repeated scanning that makes naive solutions quadratic.
The problems it solves share a shape: you are looking for, for each element, some information about the elements around it, and the answer for one element becomes known exactly when a later element arrives. The classics are the next greater element, the daily temperatures problem, and the largest rectangle in a histogram, plus the next smaller element variants of all of them.
The mechanism is that the pop condition encodes the answer. In the next greater element, you pop while the top is smaller than the current value, and every popped index gets the current value as its answer. Because the stack is ordered, only the top can violate the condition, so the while loop terminates immediately once it is satisfied.
The trade-off to name is that a monotonic stack needs random access to the values behind the indices it holds. For an array that is free, but for a linked list or a stream you would have to store the values alongside the indices. The second point is that monotonic stacks are harder to debug than the naive version, because the invariant has to hold at every step, and a slightly wrong comparison operator produces plausible but wrong answers rather than an error.