Stacks
Stack problems usually come in the form of monotonic stack problems
Runtime analysis
Top/peek:
Push:
Pop:
Search:
isEmpty:
Corner cases
Empty stack
Stack with one item
Stack with two items
Take noteβ¦
pop()
is LIFO so if order of insertion matters, reverse the popped values
Techniques
Monotonic stack
Push elements while monotonically increasing/decreasing. Then, keep popping the elements once the reverse occurs until either empty or incoming element is eventually increasing/decreasing
The final stack can represent the overall values
Mathematical equation parsing
Stacks are useful for problems like evaluating equations as you can push numbers onto the stack and operations just need to pop the top two elements of the stack
Last updated