Implement Queue using Stacks - Stack - Easy - LeetCode
💻 coding

Implement Queue using Stacks - Stack - Easy - LeetCode

2 min read 338 words
2 min read
ShareWhatsAppPost on X
  • 1The queue is implemented using two stacks to manage the push and pop operations effectively.
  • 2The pop and peek operations transfer elements from one stack to another when needed, ensuring correct order.
  • 3The time complexity for operations is O(n) due to potential element transfers between stacks.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The queue is implemented using two stacks to manage the push and pop operations effectively."

Implement Queue using Stacks - Stack - Easy - LeetCode

Implement the following operations of a queue using stacks.

push(x) -- Push element x to the back of the queue. pop() -- Removes the element from in front of the 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 top, peek/pop from top, size, and is empty operations are valid. Depending on your language, the 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).

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); myQueue.push(2); myQueue.peek(); // return 1 myQueue.pop(); // return 1 myQueue.empty(); // return False

Constraints:

1 <= x < 10 At most 100 calls will be made to push, pop, peek, and empty.

public class MyQueue {

 Stack<int> stack1;
 Stack<int> stack2;
 /** Initialize your data structure here. */
 public MyQueue() {
 stack1 = new Stack<int>();
 stack2 = new Stack<int>();
 }
 
 /** Push element x to the back of queue. */
 public void Push(int x) {
 stack1.Push(x);
 }
 
 /** Removes the element from in front of queue and returns that element. */
 public int Pop() {
 if(stack2.Count()<1){
 while(stack1.Count()>0){
 stack2.Push(stack1.Pop());
 }
 }
 
 return stack2.Pop();
 }
 
 /** Get the front element. */
 public int Peek() {
 if(stack2.Count<1){
 while(stack1.Count()>0){
 stack2.Push(stack1.Pop());
 }
 }
 return stack2.Peek();
 }
 
 /** Returns whether the queue is empty. */
 public bool Empty() {
 return stack1.Count()<1 && stack2.Count()<1;
 }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.Push(x);
 * int param_2 = obj.Pop();
 * int param_3 = obj.Peek();
 * bool param_4 = obj.Empty();
 */

Time Complexity: O(n)

Space Complexity: O(n)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 3 October 2020 · 2 min read · 338 words

Part of AskGif Blog · coding

You might also like