Analysis:
There should be two stacks, one for push and one for pop.
Rule 1: always push items into the store stack
Rule 2: pop from the pop stack, and if not, we should pop from store stack and push them into pop stack
Average time is O(1), since if one-time pop takes O(k), then the next k-time actions only take O(1).
Time Complexity:
- O(1)
Space Complexity:
- O(n)
Code
1 | import collections |