Min Stack LeetCode Solution

Last updated on February 22nd, 2024 at 04:05 am

Here, We see Min Stack LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Min Stack LeetCode Solution

Min Stack LeetCode Solution

Problem Statement

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Implement the MinStack class:

  • MinStack() initializes the stack object.
  • void push(int val) pushes the 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

Min Stack Leetcode Solution C++

class MinStack {
 public:
  void push(int x) {
    if (stack.empty())
      stack.emplace(x, x);
    else
      stack.emplace(x, min(x, stack.top().second));
  }
  void pop() {
    stack.pop();
  }
  int top() {
    return stack.top().first;
  }
  int getMin() {
    return stack.top().second;
  }
 private:
  stack<pair<int, int>> stack;  // {x, min}
};
Code language: C++ (cpp)

Min Stack Leetcode Solution Java

class MinStack {
    int min = Integer.MAX_VALUE;
    Stack<Integer> stack = new Stack<Integer>();
    public void push(int x) {
        if(x <= min){          
            stack.push(min);
            min=x;
        }
        stack.push(x);
    }
    public void pop() {
        if(stack.pop() == min) min=stack.pop();
    }
    public int top() {
        return stack.peek();
    }
    public int getMin() {
        return min;
    }
}
Code language: Java (java)

Min Stack Leetcode Solution JavaScript

var MinStack = function() {
  this.elements = [];
};
MinStack.prototype.push = function(x) {
  this.elements.push({
    value: x,
    min: this.elements.length === 0 ? x : Math.min(x, this.getMin()),
  });
};
MinStack.prototype.pop = function() {
  this.elements.pop();
};
MinStack.prototype.top = function() {
  return this.elements[this.elements.length - 1].value;
};
MinStack.prototype.getMin = function() {
  return this.elements[this.elements.length - 1].min;
};
Code language: JavaScript (javascript)

Min Stack Leetcode Solution Python

class MinStack:
    def __init__(self):
        self.stack= []
    def push(self, val):
        if not self.stack:self.stack.append((val,val)) 
        else:
            self.stack.append((val,min(val,self.stack[-1][1])))
    def pop(self):
        if self.stack: self.stack.pop()
    def top(self):
        if self.stack: return self.stack[-1][0]
        else: return None
    def getMin(self):
        if self.stack: return self.stack[-1][1]
        else: return None
Code language: Python (python)
Scroll to Top