Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: - void push(int x) Pushes element x to the back of the queue. - int pop() Removes the element from the front of the queue and returns it. - int peek() Returns the element at the front of the queue. - boolean empty() Returns true if the queue is empty, false otherwise.
Example 1
Input: operations = ["MyQueue","push","push","peek","pop","empty"], args = [[],[1],[2],[],[],[]]
Output: [null,null,null,1,1,false]
Explanation: 1 and 2 are pushed. peek() and pop() both return 1 because a queue serves the oldest element first. 2 is still inside, so empty() is false.
Example 2
Input: operations = ["MyQueue","push","pop"], args = [[],[1],[]]
Output: [null,null,1]
Explanation: The only element, 1, is pushed and then popped.
Example 3
Input: operations = ["MyQueue","push","push","push","pop","pop","pop"], args = [[],[1],[2],[3],[],[],[]]
Output: [null,null,null,null,1,2,3]
Explanation: 1, 2 and 3 are pushed and come out in the same order, first in first out.
Use an input stack for pushes and an output stack for pops. Transfer from input to output only when output is empty, reversing the order.
Run checks all cases above. Submit evaluates all test cases.