MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / MinStack

Class MinStack

stacks/min-stack.js:1–26  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class MinStack {
2 constructor() {
3 this.elements = [];
4 this.mins = [];
5 this.min = undefined;
6 }
7
8 push(element) {
9 this.elements.push(element);
10 if (element < this.min || this.min === undefined) {
11 this.min = element;
12 this.mins.push(element);
13 }
14 }
15
16 pop() {
17 if (this.elements.length > 0) {
18 const elementToPop = this.elements.pop();
19 if (elementToPop === this.min) {
20 this.mins.pop();
21 this.min = this.mins[this.mins.length - 1];
22 }
23 return elementToPop;
24 }
25 }
26}
27
28const myMinStack = new MinStack();
29

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected