Created
December 7, 2017 00:26
-
-
Save PantherHawk/402989fdb3f081c338b3be1a09f81e05 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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