forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0155-min-stack.ts
52 lines (45 loc) · 1.04 KB
/
0155-min-stack.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class MinStack {
stack: number[];
minstack: number[];
constructor() {
this.stack = [];
this.minstack = [];
}
push(val: number): void {
this.stack.push(val);
if (
val < this.minstack[this.minstack.length - 1] ||
this.minstack.length === 0
) {
this.minstack.push(val);
} else {
this.minstack.push(this.minstack[this.minstack.length - 1]);
}
}
pop(): void {
this.stack.pop();
this.minstack.pop();
}
top(): number {
return this.stack[this.stack.length - 1];
}
getMin(): number {
return this.minstack[this.minstack.length - 1];
}
}
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(val)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/
const minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();
minStack.pop();
minStack.top();
minStack.getMin();