# 232. Implement Queue using Stacks

xiaoxiao2021-02-28  5

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty() -- Return whether the queue is empty. Notes: You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is empty operations are valid.Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

public class MyQueue { /** Initialize your data structure here. */ private Stack<Integer> stack = new Stack<Integer>(); public MyQueue() { } /** Push element x to the back of queue. */ public void push(int x) { Stack<Integer> stack2 = new Stack<Integer>(); while(!stack.isEmpty()){ stack2.push(stack.pop()); } stack.push(x); while(!stack2.isEmpty()){ stack.push(stack2.pop()); } } /** Removes the element from in front of queue and returns that element. */ public int pop() { return stack.pop(); } /** Get the front element. */ public int peek() { return stack.peek(); } /** Returns whether the queue is empty. */ public boolean empty() { return stack.isEmpty(); } }

private Stack<Integer> s1 = new Stack<>(); private Stack<Integer> s2 = new Stack<>(); // Push element x to the back of queue. public void push(int x) { if (s1.empty()) front = x; s1.push(x); } // Removes the element from in front of queue. public void pop() { if (s2.isEmpty()) { while (!s1.isEmpty()) s2.push(s1.pop()); } s2.pop(); } // Return whether the queue is empty. public boolean empty() { return s1.isEmpty() && s2.isEmpty(); } // Get the front element. public int peek() { if (!s2.isEmpty()) { return s2.peek(); } return front; }

push pop的平均时间复杂度都是O(1) 对于pop操作的时间复杂度分析:

Complexity Analysis

Time complexity: Amortized O(1), Worst-case O(n).

In the worst case scenario when stack s2 is empty, the algorithm pops n elements from stack s1 and pushes nelements to s2, where n is the queue size. This gives 2n operations, which is O(n). But when stack s2 is not empty the algorithm has O(1) time complexity. So what does it mean by Amortized O(1)? Please see the next section on Amortized Analysis for more information.

Space complexity : O(1).

Amortized Analysis

Amortized analysis gives the average performance (over time) of each operation in the worst case. The basic idea is that a worst case operation can alter the state in such a way that the worst case cannot occur again for a long time, thus amortizing its cost.

Consider this example where we start with an empty queue with the following sequence of operations applied:

push_1, push_2, \ldots, push_n, pop_1,pop_2 \ldots, pop_n

The worst case time complexity of a single pop operation is O(n). Since we have n pop operations, using the worst-case per operation analysis gives us a total of O(n^2) time.

However, in a sequence of operations the worst case does not occur often in each operation - some operations may be cheap, some may be expensive. Therefore, a traditional worst-case per operation analysis can give overly pessimistic bound. For example, in a dynamic array only some inserts take a linear time, though others - a constant time.

In the example above, the number of times pop operation can be called is limited by the number of push operations before it. Although a single pop operation could be expensive, it is expensive only once per n times (queue size), when s2 is empty and there is a need for data transfer between s1 and s2. Hence the total time complexity of the sequence is : n (for push operations) + 2*n (for first pop operation) + n - 1 ( for pop operations) which is O(2*n).This gives O(2n/2n) = O(1) average time per operation.