complexitystacks
Can a monotonic stack answer next-greater queries?
Anonymous
keep the stack increasing or decreasing according to the question. For next-greater queries, pop while the current value is greater than the top; each item enters and leaves once, so the scan is O(n). the pop condition is the part to get right.
0