Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.Notes:
push to top
, peek/pop from top
, size
, and is empty
operations are valid.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:
1 <= x <= 9
100
calls will be made to push
, pop
, peek
, and empty
.pop
and peek
are valid.Follow-up: Can you implement the queue such that each operation is amortizedO(1)
time complexity? In other words, performing n
operations will take overall O(n)
time even if one of those operations may take longer.
class MyQueue {
Stack<Integer> input = new Stack();
Stack<Integer> output = new Stack();
public void push(int x) {
input.push(x);
}
public void pop() {
peek();
output.pop();
}
public int peek() {
if (output.empty())
while (!input.empty())
output.push(input.pop());
return output.peek();
}
public boolean empty() {
return input.empty() && output.empty();
}
}
class Queue {
stack<int> input, output;
public:
void push(int x) {
input.push(x);
}
void pop(void) {
peek();
output.pop();
}
int peek(void) {
if (output.empty())
while (input.size())
output.push(input.top()), input.pop();
return output.top();
}
bool empty(void) {
return input.empty() && output.empty();
}
};
class Queue(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.inStack, self.outStack = [], []
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.inStack.append(x)
def pop(self):
"""
:rtype: nothing
"""
self.move()
self.outStack.pop()
def peek(self):
"""
:rtype: int
"""
self.move()
return self.outStack[-1]
def empty(self):
"""
:rtype: bool
"""
return (not self.inStack) and (not self.outStack)
def move(self):
"""
:rtype nothing
"""
if not self.outStack:
while self.inStack:
self.outStack.append(self.inStack.pop())
In our experience, we suggest you solve this Implement Queue using Stacks LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Implement Queue using Stacks LeetCode Solution
I hope this Implement Queue using Stacks LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>