Queue Using Two Stacks
Implement a queue using two stacks. Your implementation should support the following operations:
enqueue(value)- Add element to the queuedequeue()- Remove element from the queuepeek()- Get the front elementisEmpty()- Check if queue is empty
All operations should run in amortized O(1) time complexity.
Examples
Input: [["Queue","enqueue","enqueue","dequeue","peek"],[[],[1],[2],[],[]]]
Output: [null,null,null,1,2]
Input: [["Queue","dequeue"],[[],[]]]
Output: [null,null]
Hints
Use two stacks: one for input (push) and one for output (pop).
On dequeue/peek, if output stack is empty, transfer all elements from input stack.
This gives amortized O(1) per operation.
Queue Using Two Stacks
Implement a queue using two stacks. Your implementation should support the following operations: