Implement Min Stack
Difficulty: Medium
Acceptance: %
Points: 30.00
You are tasked with implementing a stack that supports the following operations while handling up to q queries:
- Push: Add an integer x to the top of the stack.
- Pop: Remove and return the top element of the stack. If the stack is empty, return -1.
- GetMin: Retrieve the smallest element in the stack in constant time (O(1)). If the stack is empty, return -1.
You will receive a series of queries, and each query will perform one of the following actions:
- 1 x: Add the integer x to the top of the stack.
- 2: Remove and return the top element of the stack. If the stack is empty, return -1.
- 3: Return the smallest element in the stack. If the stack is empty, return -1.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints: - 1 <= q <= 100
- 1 <= values on the stack <= 100
Companies: Amazon Microsoft Walmart Goldman Sachs Flipkart
Please log in to access the Submissions tab, where you can review your progress and explore code submissions from other participants.
Please log in to access the Solution tab and view detailed answers and explanations.
Please log in to access the Discussion tab and join conversations with other participants.