Summer Sale is Live! Unlock 40% OFF on All Job-Oriented Training Programs – Limited Time Only! Offer Ending in
D
H
M
S
Get Now
Problem Submissions Solution

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:

  1. Push: Add an integer x to the top of the stack.
  2. Pop: Remove and return the top element of the stack. If the stack is empty, return -1.
  3. 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.

Topics

Companies

Articles

Examples:

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
Topics:
Stack
Locked Content
Access Restricted: Please Login to access the code editor and test cases.