Implement a last in first out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal queue (push, top, pop, and empty).
Implement the MyStack class:
void push(int x) Pushes element x to the top of the stack.
int pop() Removes the element on the top of the stack and returns it.
int top() Returns the element on the top of the stack.
boolean empty() Returns true if the stack is empty, false otherwise.
Notes:
You must use only standard operations of a queue, which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue), as long as you use only a queue's standard operations.
At most 100 calls will be made to push, pop, top, and empty.
All the calls to pop and top are valid.
Follow-up: Can you implement the stack such that each operation is amortized O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer. You can use more than two queues.
Solution:
1. two queues, always push to empty queue, to that the empty queue's first element is the one we want to pop. push other queue's elements to the tail of the empty queue as well
class MyStack {
/** Initialize your data structure here. */
Queue<Integer> q1;
Queue<Integer> q2;
public MyStack() {
q1 = new ArrayDeque();
q2 = new ArrayDeque();
}
/** Push element x onto stack. */
public void push(int x) {
if (q1.isEmpty()) {
q1.offer(x);
while (!q2.isEmpty()) {
q1.offer(q2.poll());
}
} else {
q2.offer(x);
while (!q1.isEmpty()) {
q2.offer(q1.poll());
}
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
if (!q2.isEmpty()) {
return q2.poll();
}
return q1.poll();
}
/** Get the top element. */
public int top() {
if (!q2.isEmpty()) {
return q2.peek();
}
return q1.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return q1.isEmpty() && q2.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
2. always append to non empty queue, so the end of that queue is the result. when popping, move the elements before result to the other queue, and return the last element
class MyStack {
/** Initialize your data structure here. */
Queue<Integer> q1;
Queue<Integer> q2;
public MyStack() {
q1 = new ArrayDeque();
q2 = new ArrayDeque();
}
/** Push element x onto stack. */
public void push(int x) {
if (!q1.isEmpty()) {
q1.offer(x);
} else {
q2.offer(x);
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
if (!q1.isEmpty()) {
int size = q1.size();
for (int i = 0; i < size - 1; i ++) {
q2.offer(q1.poll());
}
return q1.poll();
} else {
int size = q2.size();
for (int i = 0; i < size - 1; i ++) {
q1.offer(q2.poll());
}
return q2.poll();
}
}
/** Get the top element. */
public int top() {
if (!q1.isEmpty()) {
int size = q1.size();
for (int i = 0; i < size - 1; i ++) {
q2.offer(q1.poll());
}
int res = q1.poll();
q2.offer(res);
return res;
} else {
int size = q2.size();
for (int i = 0; i < size - 1; i ++) {
q1.offer(q2.poll());
}
int res = q2.poll();
q1.offer(res);
return res;
}
}
/** Returns whether the stack is empty. */
public boolean empty() {
return q1.isEmpty() && q2.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
3. one queue
class MyStack {
/** Initialize your data structure here. */
Queue<Integer> q1;
public MyStack() {
q1 = new ArrayDeque();
}
/** Push element x onto stack. */
public void push(int x) {
q1.offer(x);
for (int i = 0; i < q1.size() - 1; i ++) {
q1.offer(q1.poll());
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return q1.poll();
}
/** Get the top element. */
public int top() {
return q1.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return q1.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/