Tutorialspoint
Problem
Solution
Submissions

Min Stack with O(1) Operations

Certification: Intermediate Level Accuracy: 100% Submissions: 1 Points: 10

Write a C++ program to design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class with the following operations:

Operations
  • MinStack(): initializes the stack object.
  • void push(int val): pushes element val onto the stack.
  • void pop(): removes the element on the top of the stack.
  • int top(): gets the top element of the stack.
  • int getMin(): retrieves the minimum element in the stack.
Example 1
  • Input: ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]]
  • Output: [null,null,null,null,-3,null,0,-2]
  • Explanation:
    • MinStack minStack = new MinStack();
    • minStack.push(-2);
    • minStack.push(0);
    • minStack.push(-3);
    • minStack.getMin(); // return -3
    • minStack.pop();
    • minStack.top(); // return 0
    • minStack.getMin(); // return -2
Example 2
  • Input: ["MinStack","push","push","getMin","push","getMin","pop","getMin"] [[],[5],[3],[],[1],[],[],[]]
  • Output: [null,null,null,3,null,1,null,3]
  • Explanation:
    • MinStack minStack = new MinStack();
    • minStack.push(5);
    • minStack.push(3);
    • minStack.getMin(); // return 3
    • minStack.push(1);
    • minStack.getMin(); // return 1
    • minStack.pop();
    • minStack.getMin(); // return 3
Constraints
  • -2³¹ ≤ val ≤ 2³¹ - 1
  • Methods pop, top and getMin operations will always be called on non-empty stacks
  • At most 3 * 10⁴ calls will be made to push, pop, top, and getMin
  • Time Complexity: O(1) for all operations
  • Space Complexity: O(n)
StackCapgeminiD. E. Shaw
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use an auxiliary stack to keep track of the current minimum element
  • When pushing an element, push the current minimum to the auxiliary stack as well
  • When popping an element, pop from both stacks
  • The top of the auxiliary stack will always contain the current minimum
  • Be careful about how to update the minimum when elements are popped

Steps to solve by this approach:

 Step 1: Create two stacks - one for storing actual values and another for tracking minimums
 Step 2: For each push operation, push the value to the data stack
 Step 3: For the min stack, push either the new value or the current minimum (whichever is smaller)
 Step 4: For each pop operation, pop from both stacks simultaneously
 Step 5: To get the top value, simply return the top of the data stack
 Step 6: To get the minimum value, simply return the top of the min stack
 Step 7: This approach ensures all operations have O(1) time complexity

Submitted Code :