155 Min Stack

Problem Statement

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

Implement the MinStack class:

You must implement a solution with O(1) time complexity for each function.

Input
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[|],[-2],[0],[-3],[],[],[],[]]

Output
[null,null,null,null,-3,null,0,-2]

Approach

We use two stacks, one as the "result" stack and one to keep track of minimum Values.
Push: If the minStack is empty and the current value is smaller/equal than the last element on the minStack we push the value onto it.
Pop: We pop from the stack and if that value equals the last value on the minStack we also pop from the minStack.

The other operations are straightforward

Solution

var MinStack = function() {
  this.stack = []
  this.minStack = []
};

MinStack.prototype.push = function(val) {
  this.stack.push(val)
  if (!this.minStack.length || val <= this.minStack[this.minStack.length - 1]) {
    this.minStack.push(val)
  }
};

MinStack.prototype.pop = function() {
  let x = this.stack.pop()
  if (x === this.minStack[this.minStack.length - 1]) {
    this.minStack.pop()
  }
};

MinStack.prototype.top = function() {
  return this.stack[this.stack.length - 1]
};

MinStack.prototype.getMin = function() {
  return this.minStack[this.minStack.length - 1]
};

Questions

References

LC

Stack