| description |
|---|
Stack problems usually come in the form of monotonic stack problems |
- Top/peek:
$$O(1)$$ - Push:
$$O(1)$$ - Pop:
$$O(1)$$ - Search:
$$O(n)$$ - isEmpty:
$$O(1)$$
- 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