Leetcode 225: Implement Stack using Queues
Design a stack that supports LIFO (Last In First Out) operations using only one queue. Implement standard stack operations: push, pop, top, and empty.
Problem
Approach
Steps
Complexity
Input: The input consists of a series of commands and their corresponding arguments for stack operations.
Example: Input: ["MyStack", "push", "push", "top", "pop", "empty"], [[], [5], [7], [], [], []]
Constraints:
• 1 <= x <= 9
• At most 100 operations will be performed.
• All pop and top operations will be valid (i.e., the stack will not be empty).
Output: The output is an array of results corresponding to the executed operations.
Example: Output: [null, null, null, 7, 7, false]
Constraints:
• The results must match the order of operations executed.
Goal: Simulate stack operations using only one queue and standard queue methods.
Steps:
• Use a single queue to store stack elements.
• When pushing an element, enqueue it and then rotate the queue to maintain the LIFO order.
• For pop and top operations, return or remove the front element of the queue.
• Check if the queue is empty for the empty operation.
Goal: The implementation must adhere to the following constraints:
Steps:
• Only standard queue operations (enqueue, dequeue, front, size, isEmpty) are allowed.
• A single queue is used for implementation.
Assumptions:
• The queue supports all required operations natively or through simulation.
• Input commands and arguments are always valid and within constraints.
• Input: Input: ["MyStack", "push", "push", "top", "pop", "empty"], [[], [3], [8], [], [], []]
• Explanation: Commands initialize a stack, push 3 and 8, check the top element (8), pop the top (8), and check if the stack is empty (false). Output: [null, null, null, 8, 8, false]
• Input: Input: ["MyStack", "push", "top", "pop", "empty"], [[], [6], [], [], []]
• Explanation: Commands initialize a stack, push 6, check the top element (6), pop the top (6), and check if the stack is empty (true). Output: [null, null, 6, 6, true]
Approach: Use a single queue to simulate stack behavior. Maintain LIFO order by rotating the queue after each push operation.
Observations:
• A queue follows FIFO order, while a stack requires LIFO order.
• Rotating the queue ensures that the most recently added element is always at the front.
• By rotating elements after every push, the front of the queue will behave as the top of the stack.
• Pop and top operations can be handled directly via the queue's front method.
Steps:
• Initialize an empty queue.
• For push operation: Enqueue the new element, then rotate the queue (dequeue and enqueue existing elements).
• For pop operation: Dequeue and return the front element of the queue.
• For top operation: Return the front element of the queue without removing it.
• For empty operation: Check if the queue is empty.
Empty Inputs:
• Operations on an empty stack (e.g., calling pop or top).
Large Inputs:
• Performing 100 operations (the maximum limit).
Special Values:
• Pushing and popping the same element repeatedly.
Constraints:
• Ensure the implementation works for all edge cases within the problem's constraints.
queue<int> que;
MyStack() {
}
void push(int x) {
que.push(x);
for(int i=0;i<que.size()-1;++i){
que.push(que.front());
que.pop();
}
}
int pop() {
int x = que.front();
que.pop();
return x;
}
int top() {
return que.front();
}
bool empty() {
return que.empty();
}
};
/**
* 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();
* bool param_4 = obj->empty();
1 : Variable Declaration
queue<int> que;
Declares a queue of integers `que` to be used to store elements in the stack.
2 : Constructor
MyStack() {
Defines the constructor of the `MyStack` class, initializing the stack object. Here, no additional initialization is done.
3 : Push Method Start
void push(int x) {
Defines the `push` method, which inserts an element `x` onto the stack.
4 : Queue Push
que.push(x);
Pushes the element `x` onto the queue.
5 : Queue Rearrangement
for(int i=0;i<que.size()-1;++i){
This loop rearranges the queue so that the most recently added element appears at the front, mimicking stack behavior.
6 : Queue Rotation
que.push(que.front());
Moves the front element of the queue to the back.
7 : Queue Pop
que.pop();
Removes the front element from the queue, completing the rotation process.
8 : Pop Method Start
int pop() {
Defines the `pop` method, which removes and returns the top element from the stack.
9 : Pop Element
int x = que.front();
Retrieves the front element of the queue, which corresponds to the top of the stack.
10 : Pop Operation
que.pop();
Removes the front element from the queue.
11 : Pop Method End
return x;
Returns the element `x` that was popped from the stack.
12 : Top Method Start
int top() {
Defines the `top` method, which returns the top element of the stack without removing it.
13 : Top Element
return que.front();
Returns the front element of the queue, which is the top element of the stack.
14 : Empty Method Start
bool empty() {
Defines the `empty` method, which checks whether the stack is empty.
15 : Empty Check
return que.empty();
Returns true if the queue is empty, meaning the stack is also empty.
Best Case: O(1) for pop, top, and empty; O(n) for push.
Average Case: O(n) for push due to rotation.
Worst Case: O(n) for push when the queue has many elements.
Description: Push operation requires rotating the queue, resulting in O(n) time complexity.
Best Case: O(n)
Worst Case: O(n)
Description: The queue stores all elements, requiring O(n) space.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus