Stacks
Stack problems usually come in the form of monotonic stack problems
Last updated
Stack problems usually come in the form of monotonic stack problems
Last updated
Top/peek:
Push:
Pop:
Search:
isEmpty:
Empty stack
Stack with one item
Stack with two items
pop()
is LIFO so if order of insertion matters, reverse the popped values
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
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