1/21
Loading...
🚀Start Monotonic Stack
We find each element's Next Greater Element. The stack holds indices that haven't found an answer yet; when a larger new element arrives, we pop and fill answers. result starts all -1.
Loading...
We find each element's Next Greater Element. The stack holds indices that haven't found an answer yet; when a larger new element arrives, we pop and fill answers. result starts all -1.
A monotonic stack is a stack maintained so its elements (usually indices) always stay in increasing or decreasing order. For Next Greater Element, we push indices while keeping the stored values in decreasing order from bottom to top (monotonically decreasing).