Skip to content

Instantly share code, notes, and snippets.

@pjcodesjs
Created March 12, 2023 05:16
Show Gist options
  • Save pjcodesjs/6e3a0a3a55ec990e40bab3a4e2789f72 to your computer and use it in GitHub Desktop.
Save pjcodesjs/6e3a0a3a55ec990e40bab3a4e2789f72 to your computer and use it in GitHub Desktop.
class Stack {
constructor() {
this.items = [];
this.min = Infinity;
}
push(element) {
if (element < this.min) {
this.min = element;
}
this.items.push(element);
}
pop() {
const element = this.items.pop();
if (element === this.min) {
this.min = Infinity;
for (const item of this.items) {
if (item < this.min) {
this.min = item;
}
}
}
return element;
}
getMin() {
return this.min;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment