Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

84. Min Stack

MediumDay 25 of 60Stack & Queue

Design a MinStack class that supports push(val), pop(), top() and getMin(), where getMin() returns the smallest element currently in the stack. Every operation must run in O(1) time. pop, top and getMin are only called on a non-empty stack.

Example 1
Input: ops = ["MinStack","push","push","push","getMin","pop","top","getMin"], args = [[],[-2],[0],[-3],[],[],[],[]]
Output: [null,null,null,null,-3,null,0,-2]
After popping -3, the minimum goes back to -2.
Constraints
  • -2^31 <= val <= 2^31 - 1
  • At most 3 * 10^4 calls in total
Hints
Frequently asked at: Amazon, Microsoft, Bloomberg, Goldman Sachs
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for ₹200/ 30 min