Created
September 27, 2018 17:48
-
-
Save basekays/898e21566d18e933b0474a036fba3a9b 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
| const MinStack = function() { | |
| this.items = []; | |
| this.minValueTracker = []; | |
| }; | |
| MinStack.prototype.push = function(x) { | |
| const minValueTrackerLength = this.minValueTracker.length - 1; | |
| const latestMinValue = this.minValueTracker[minValueTrackerLength]; | |
| this.items.push(x); | |
| if (!this.minValueTracker.length) { | |
| this.minValueTracker.push(x); | |
| } else { | |
| if (x < latestMinValue) { | |
| this.minValueTracker.push(x); | |
| } else { | |
| this.minValueTracker.push(latestMinValue); | |
| } | |
| } | |
| }; | |
| MinStack.prototype.pop = function() { | |
| this.items.pop(); | |
| this.minValueTracker.pop(); | |
| }; | |
| MinStack.prototype.top = function() { | |
| return this.items[this.items.length - 1]; | |
| }; | |
| MinStack.prototype.getMin = function() { | |
| return this.minValueTracker[this.minValueTracker.length - 1]; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment