Implementing Stacks & Queues
What enhancement does this Stack implementation provide?
class Stack {
constructor() {
this.minStack = [];
this.mainStack = [];
}
push(element) {
this.mainStack.push(element);
if (!this.minStack.length || element <= this.minStack[this.minStack.length - 1]) {
this.minStack.push(element);
}
}
pop() {
if (this.mainStack.pop() === this.minStack[this.minStack.length - 1]) {
this.minStack.pop();
}
}
getMin() {
return this.minStack[this.minStack.length - 1];
}
}