File tree 1 file changed +10
-14
lines changed 1 file changed +10
-14
lines changed Original file line number Diff line number Diff line change 1
1
class MinStack {
2
-
3
- /** initialize your data structure here. */
4
- Stack <Integer > stack ;
5
- Stack <Integer > min ;
2
+ private final Stack <int []> stack ;
3
+
6
4
public MinStack () {
7
- stack = new Stack <>();
8
- min = new Stack <>();
9
- }
5
+ this .stack = new Stack <>();
6
+ }
10
7
11
- public void push (int x ) {
12
- stack .push ( x );
13
- min .push (Math . min ( x , min . isEmpty () ? Integer . MAX_VALUE : min . peek ()) );
8
+ public void push (int val ) {
9
+ int min = stack .isEmpty () ? val : Math . min ( val , stack . peek ()[ 1 ] );
10
+ stack .push (new int []{ val , min } );
14
11
}
15
12
16
13
public void pop () {
17
14
stack .pop ();
18
- min .pop ();
19
15
}
20
16
21
17
public int top () {
22
- return stack .peek ();
18
+ return stack .peek ()[ 0 ] ;
23
19
}
24
20
25
21
public int getMin () {
26
- return min .peek ();
22
+ return stack .peek ()[ 1 ] ;
27
23
}
28
24
}
29
25
30
26
/**
31
27
* Your MinStack object will be instantiated and called as such:
32
28
* MinStack obj = new MinStack();
33
- * obj.push(x );
29
+ * obj.push(val );
34
30
* obj.pop();
35
31
* int param_3 = obj.top();
36
32
* int param_4 = obj.getMin();
You can’t perform that action at this time.
0 commit comments