-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminStack.js
More file actions
40 lines (34 loc) · 699 Bytes
/
Copy pathminStack.js
File metadata and controls
40 lines (34 loc) · 699 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
console.log('Problem 2 — Min Stack (Basic)');
class MinStack {
constructor() {
this.stack = [];
this.minStack = [];
}
push(x) {
this.stack.push(x);
let min =
this.minStack.length === 0
? x
: Math.min(x, this.minStack[this.minStack.length - 1]);
this.minStack.push(min);
}
pop() {
this.stack.pop();
this.minStack.pop();
}
peek() {
return this.stack[this.stack.length - 1];
}
getMin() {
return this.minStack[this.minStack.length - 1];
}
}
let ms = new MinStack();
ms.push(5);
ms.push(3);
ms.push(7);
console.log(ms.getMin()); // 3
// ms.pop();
// ms.pop();
// console.log(ms.getMin()); // 5
console.log(ms.peek());