class MaxStack {
constructor() {
this.results = [];
this.sorted = [];
}
pop() {
const val = this.results.pop();
this.sorted.splice(this.sorted.lastIndexOf(val), 1);
return val;
}
push(x) {
this.results.push(x);
this.sorted = [...this.results].sort((x, y) => x - y);
}
top() {
return this.results[this.results.length - 1];
}
popMax() {
const val = this.sorted.pop();
this.results.splice(this.results.lastIndexOf(val), 1);
return val;
}
peekMax() {
return this.sorted[this.sorted.length - 1];
}
}