← StackDSA 1.5.B
Monotonic Stack
Keep the stack increasing or decreasing; pop while the incoming element breaks the order — each pop is the answer for next-greater/next-smaller, in O(n).
Keep the stack increasing or decreasing; pop while the incoming element breaks the order — each pop is the answer for next-greater/next-smaller, in O(n).
Notes from readers
Comments — via GitHub