Skip to content

Instantly share code, notes, and snippets.

@PantherHawk
Created December 7, 2017 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PantherHawk/402989fdb3f081c338b3be1a09f81e05 to your computer and use it in GitHub Desktop.
Save PantherHawk/402989fdb3f081c338b3be1a09f81e05 to your computer and use it in GitHub Desktop.
var MinStack = function() {
this.stack = [];
this.min;
};
MinStack.prototype.push = function(x) {
this.stack.push(x);
if (!this.min && this.min !== 0) {
this.min = x;
} else if (x < this.min) {
this.min = x;
}
};
MinStack.prototype.pop = function() {
let temp = this.stack.pop();
this.min = this.stack[0];
for (var i in this.stack) {
this.min = Math.min(this.stack[i], this.min);
}
};
MinStack.prototype.top = function() {
return this.stack[this.stack.length - 1]
};
MinStack.prototype.getMin = function() {
return this.min;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment