Skip to content

Instantly share code, notes, and snippets.

@basekays
Created September 27, 2018 05:01
Show Gist options
  • Select an option

  • Save basekays/0b1cd5d2f672c905b4270901fb403ba7 to your computer and use it in GitHub Desktop.

Select an option

Save basekays/0b1cd5d2f672c905b4270901fb403ba7 to your computer and use it in GitHub Desktop.
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