# LC 232. Implement Queue using Stacks ### [Problem link](https://leetcode.com/problems/implement-queue-using-stacks/) ###### tags: `leedcode` `easy` `python` `c++` Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (<code>push</code>, <code>peek</code>, <code>pop</code>, and <code>empty</code>). Implement the <code>MyQueue</code> class: - <code>void push(int x)</code> Pushes element x to the back of the queue. - <code>int pop()</code> Removes the element from the front of the queue and returns it. - <code>int peek()</code> Returns the element at the front of the queue. - <code>boolean empty()</code> Returns <code>true</code> if the queue is empty, <code>false</code> otherwise. **Notes:** - You must use **only** standard operations of a stack, which means only <code>push to top</code>, <code>peek/pop from top</code>, <code>size</code>, and <code>is empty</code> operations are valid. - Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations. **Example 1:** ``` Input ["MyQueue", "push", "push", "peek", "pop", "empty"] [[], [1], [2], [], [], []] Output [null, null, null, 1, 1, false] Explanation MyQueue myQueue = new MyQueue(); myQueue.push(1); // queue is: [1] myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) myQueue.peek(); // return 1 myQueue.pop(); // return 1, queue is [2] myQueue.empty(); // return false ``` **Constraints:** - <code>1 <= x <= 9</code> - At most <code>100</code>calls will be made to <code>push</code>, <code>pop</code>, <code>peek</code>, and <code>empty</code>. - All the calls to <code>pop</code> and <code>peek</code> are valid. **Follow-up:** Can you implement the queue such that each operation is **<a href="https://en.wikipedia.org/wiki/Amortized_analysis" target="_blank">amortized</a>** <code>O(1)</code> time complexity? In other words, performing <code>n</code> operations will take overall <code>O(n)</code> time even if one of those operations may take longer. ## Solution 1 #### Python ```python= class MyQueue: def __init__(self): self.pushStack = [] self.popStack = [] def push(self, x: int) -> None: self.pushStack.append(x) def pop(self) -> int: self.peek() return self.popStack.pop() def peek(self) -> int: if not self.popStack: while self.pushStack: self.popStack.append(self.pushStack.pop()) return self.popStack[-1] def empty(self) -> bool: return not self.pushStack and not self.popStack ``` #### C++ ```cpp= class MyQueue { stack<int> st_in; stack<int> st_out; public: MyQueue() {} void push(int x) { st_in.push(x); } int pop() { if (st_out.empty()) { while (!st_in.empty()) { st_out.push(st_in.top()); st_in.pop(); } } int res = st_out.top(); st_out.pop(); return res; } int peek() { int res = this->pop(); st_out.push(res); return res; } bool empty() { return (st_in.empty() && st_out.empty()); } }; ``` >### Complexity >| | Time Complexity | Space Complexity | >| ----------- | ---------------------------------- | ---- | >| Solution 1 | push, empty: O(1), pop, peek: O(n) | O(n) | ## Note x