|
| 1 | +<h2><a href="https://leetcode.com/problems/implement-queue-using-stacks">Implement Queue using Stacks</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>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>).</p> |
| 2 | + |
| 3 | +<p>Implement the <code>MyQueue</code> class:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>void push(int x)</code> Pushes element x to the back of the queue.</li> |
| 7 | + <li><code>int pop()</code> Removes the element from the front of the queue and returns it.</li> |
| 8 | + <li><code>int peek()</code> Returns the element at the front of the queue.</li> |
| 9 | + <li><code>boolean empty()</code> Returns <code>true</code> if the queue is empty, <code>false</code> otherwise.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p><strong>Notes:</strong></p> |
| 13 | + |
| 14 | +<ul> |
| 15 | + <li>You must use <strong>only</strong> 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.</li> |
| 16 | + <li>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.</li> |
| 17 | +</ul> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input</strong> |
| 24 | +["MyQueue", "push", "push", "peek", "pop", "empty"] |
| 25 | +[[], [1], [2], [], [], []] |
| 26 | +<strong>Output</strong> |
| 27 | +[null, null, null, 1, 1, false] |
| 28 | + |
| 29 | +<strong>Explanation</strong> |
| 30 | +MyQueue myQueue = new MyQueue(); |
| 31 | +myQueue.push(1); // queue is: [1] |
| 32 | +myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) |
| 33 | +myQueue.peek(); // return 1 |
| 34 | +myQueue.pop(); // return 1, queue is [2] |
| 35 | +myQueue.empty(); // return false |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Constraints:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>1 <= x <= 9</code></li> |
| 43 | + <li>At most <code>100</code> calls will be made to <code>push</code>, <code>pop</code>, <code>peek</code>, and <code>empty</code>.</li> |
| 44 | + <li>All the calls to <code>pop</code> and <code>peek</code> are valid.</li> |
| 45 | +</ul> |
| 46 | + |
| 47 | +<p> </p> |
| 48 | +<p><strong>Follow-up:</strong> Can you implement the queue such that each operation is <strong><a href="https://en.wikipedia.org/wiki/Amortized_analysis" target="_blank">amortized</a></strong> <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.</p> |
0 commit comments