-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution-class.ts
47 lines (42 loc) · 903 Bytes
/
solution-class.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
import { MinStackClass } from './types.d';
/*
* @lc app=leetcode id=155 lang=javascript
*
* [155] Min Stack
*/
// @lc code=start
/**
* initialize your data structure here.
*/
class MinStack implements MinStackClass {
private stack: number[];
private minStack: number[];
constructor() {
this.stack = [];
this.minStack = [Infinity];
}
push(x: number): void {
this.stack.push(x);
this.minStack.push(Math.min(x, this.getMin()));
}
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(x)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/
// @lc code=end
export { MinStack };