Created
September 27, 2018 05:01
-
-
Save basekays/0b1cd5d2f672c905b4270901fb403ba7 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.items = []; | |
| this.auxItems = []; | |
| }; | |
| MinStack.prototype.push = function(x) { | |
| const firstAuxItem = this.auxItems[this.auxItems.length - 1]; | |
| this.items.push(x); | |
| if (!this.auxItems.length) { | |
| this.auxItems.push(x); | |
| } else { | |
| if (x < firstAuxItem) { | |
| this.auxItems.push(x); | |
| } else { | |
| this.auxItems.push(firstAuxItem); | |
| } | |
| } | |
| }; | |
| MinStack.prototype.pop = function() { | |
| this.items.pop(); | |
| this.auxItems.pop(); | |
| }; | |
| MinStack.prototype.getMin = function() { | |
| return this.auxItems[this.auxItems.length - 1]; | |
| }; | |
| var newStack = new MinStack(); | |
| newStack.push(4); | |
| newStack.push(10); | |
| newStack.push(1); | |
| newStack.push(2); | |
| newStack.push(1524); | |
| newStack.pop(); | |
| newStack.pop(); | |
| newStack.pop(); | |
| newStack.getMin(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment