Solving Min Stack in Javascript. Please try yourself first to solve the problem and submit your implementation to LeetCode before looking into solution.

Problem Description

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

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.


MinStack minStack = new MinStack();
minStack.getMin(); --> Returns -3.
minStack.pop();; --> Returns 0.
minStack.getMin(); --> Returns -2.

See the full details of the problem Min Stack at LeetCode

Originally posted at: