The time complexity of enqueue and dequeue operation when using 2 stacks
If here enqueue using 2 stacks, then dequeue will use 1 stack is enough
Say, for enqueue we use reverse, push , reverse operations
- So, in reversing all element will push in another stack take O(n) time
- New element will be push ,take O(1) time
- Then push back to the 1st stack will take O(n) time
Now in case of dequeue
- just do pop the element from the front of stack(It will just take O(1) time)
_____________________________________________________________________
We can also do in reverse way, that take enqueue operation in O(1) time and dequeue O(n) time