Leetcode - Implement Stack using Queues
155. Min Stack[E]
https://leetcode.com/problems/min-stack/
Description
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) – Push element x onto stack.
- pop() – Removes the element on top of the stack.
- top() – Get the top element.
- getMin() – Retrieve the minimum element in the stack.
Example 1:
1 | Input |
Constraints:
- Methods
pop,topandgetMinoperations will always be called on non-empty stacks.
Solution
1 |
225. Implement Stack using Queues[E]
https://leetcode.com/problems/implement-stack-using-queues/
Description
Implement the following operations of a stack using queues.
- push(x) – Push element x onto stack.
- pop() – Removes the element on top of the stack.
- top() – Get the top element.
- empty() – Return whether the stack is empty.
Example:
1 | MyStack stack = new MyStack(); |
Notes:
- You must use only standard operations of a queue – which means only
push to back,peek/pop from front,size, andis emptyoperations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Solution
https://leetcode.com/problems/implement-stack-using-queues/solution/
1 |
232. Implement Queue using Stacks[E]
https://leetcode.com/problems/implement-queue-using-stacks/
Description
Implement the following operations of a queue using stacks.
- push(x) – Push element x to the back of queue.
- pop() – Removes the element from in front of queue.
- peek() – Get the front element.
- empty() – Return whether the queue is empty.
Example:
1 | MyQueue queue = new MyQueue(); |
Notes:
- You must use only standard operations of a stack – which means only
push to top,peek/pop from top,size, andis emptyoperations are valid. - Depending on your language, 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).
Solution
946. Validate Stack Sequences[M]
https://leetcode.com/problems/validate-stack-sequences/
Description
Given two sequences pushed and popped with distinct values, return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.
Example 1:
1 | Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1] |
Example 2:
1 | Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2] |
Note:
0 <= pushed.length == popped.length <= 10000 <= pushed[i], popped[i] < 1000pushedis a permutation ofpopped.pushedandpoppedhave distinct values.
Solution
1 |





