Learning pathsA
Data structures

Stacks and monotonic stacks

Remember unresolved candidates.

Understand the problem

A monotonic stack stores indices whose answer has not yet been found. On a larger value, pop smaller unresolved values and calculate their distance to the current index.

Make it concrete

Daily temperatures [70,73,72,75] produce waits [1,2,1,0]. Each index is pushed and popped at most once.

Trade-offs and pitfalls

Nested loops can still be O(n) when total pushes and pops are bounded.

Check your understanding

Explain why indices are more useful than just values.

Practice this topic

Your study notes