-
Notifications
You must be signed in to change notification settings - Fork 0
/
155.ts
38 lines (33 loc) · 890 Bytes
/
155.ts
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
class MinStack {
private stack: number[];
private minStack: number[];
constructor() {
this.stack = []
this.minStack = []
}
push(val: number): void {
this.stack.push(val)
if (this.minStack.length == 0 || this.minStack[this.minStack.length - 1] >= val) this.minStack.push(val)
}
pop(): void {
const element = this.stack.pop()
if (this.minStack[this.minStack.length - 1] == element) this.minStack.pop()
}
top(): number {
return this.stack[this.stack.length - 1]
}
getMin(): number {
return this.minStack[this.minStack.length - 1]
}
}
const minStack = new MinStack();
minStack.push(0);
minStack.push(1);
minStack.push(0);
console.log(minStack.getMin());
; // return -3
minStack.pop();
console.log(minStack.top());
; // return 0
console.log(minStack.getMin());
; // return -2