File tree 1 file changed +45
-0
lines changed
1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ class MinStack {
2
+ stack : number [ ] ;
3
+ minStack : number [ ] ;
4
+
5
+ constructor ( ) {
6
+ this . stack = [ ] ;
7
+ this . minStack = [ ] ;
8
+ }
9
+
10
+ push ( val : number ) : void {
11
+ this . stack . push ( val ) ;
12
+
13
+ if ( this . minStack . length === 0 || this . minStack [ this . minStack . length - 1 ] >= val ) {
14
+ this . minStack . push ( val ) ;
15
+ }
16
+ }
17
+
18
+ pop ( ) : void {
19
+ const popVal = this . stack . pop ( ) ;
20
+
21
+ if ( popVal === this . minStack [ this . minStack . length - 1 ] ) {
22
+ this . minStack . pop ( ) ;
23
+ }
24
+ }
25
+
26
+ top ( ) : number {
27
+ return this . stack [ this . stack . length - 1 ] ;
28
+ }
29
+
30
+ getMin ( ) : number {
31
+ return this . minStack [ this . minStack . length - 1 ] ;
32
+ }
33
+ }
34
+
35
+ /**
36
+ this.stack = 2, 0, 3
37
+ this.minStack = 2, 0
38
+
39
+ * Your MinStack object will be instantiated and called as such:
40
+ * var obj = new MinStack()
41
+ * obj.push(val)
42
+ * obj.pop()
43
+ * var param_3 = obj.top()
44
+ * var param_4 = obj.getMin()
45
+ */
You can’t perform that action at this time.
0 commit comments