Advertisement

Implement Stack using Queues - LeetCode 225 Solution

Implement Stack using Queues - Complete Solution Guide

Implement Stack using Queues is LeetCode problem 225, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack ( 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

Detailed Explanation

The problem asks you to implement a stack data structure using only queues. A stack follows the Last-In-First-Out (LIFO) principle, meaning the last element added is the first one removed. The challenge is to mimic this behavior using queues, which follow the First-In-First-Out (FIFO) principle. You need to implement four methods: `push(x)` (adds element x to the stack), `pop()` (removes and returns the top element), `top()` (returns the top element without removing it), and `empty()` (checks if the stack is empty). The constraint is that you can only use standard queue operations: enqueue (push to back), dequeue (pop from front), peek (view the front element), size, and isEmpty.

Solution Approach

The solution utilizes two queues, `q1` and `q2`. `push(x)` simply adds the element `x` to `q1`. `pop()` and `top()` involve transferring elements from one queue to another. To access the top element, all but the last element in `q1` are moved to `q2`. The last element in `q1` is then either returned and removed (`pop()`) or returned without removal (`top()`). Then the queues are swapped to restore `q1` as the primary queue. This ensures the next operation accesses the correct top element. `empty()` simply checks if `q1` is empty.

Step-by-Step Algorithm

  1. **push(x):** Add element x to queue `q1`.
  2. **pop():** 1. While `q1` has more than one element, move elements from `q1` to `q2`. 2. Remove and return the front element of `q1` (the top element). 3. Swap `q1` and `q2`.
  3. **top():** 1. While `q1` has more than one element, move elements from `q1` to `q2`. 2. Get the front element of `q1` (the top element). 3. Move the top element from `q1` to `q2`. 4. Swap `q1` and `q2`.
  4. **empty():** Return `true` if `q1` is empty, `false` otherwise.

Key Insights

  • Using two queues allows us to effectively simulate LIFO behavior using FIFO queues.
  • The key idea is to move all but the last element from one queue to the other to access the 'top' element efficiently.
  • Optimizing for `top()` and `pop()` operations to minimize queue element transfers is crucial for performance.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Stack, Design, Queue.

Companies

Asked at: DE Shaw, Google, Oracle.