File tree Expand file tree Collapse file tree 1 file changed +17
-10
lines changed Expand file tree Collapse file tree 1 file changed +17
-10
lines changed Original file line number Diff line number Diff line change 11class MinStack {
2- int minimum = Integer .MAX_VALUE ;
3- Stack <Integer > stack = new Stack <Integer >();
2+ private int minVal ;
3+ private Stack <Integer > s ;
4+
5+ /** initialize your data structure here. */
6+ public MinStack () {
7+ minVal = Integer .MAX_VALUE ;
8+ s = new Stack <>();
9+ }
410
511 public void push (int x ) {
6- if (x <= minimum ) {
7- stack .push (minimum );
8- minimum = x ;
12+ if (x <= minVal ) {
13+ s .push (minVal );
14+ minVal = x ;
915 }
1016
11- stack .push (x );
17+ s .push (x );
1218 }
1319
1420 public void pop () {
15- if (stack .pop () == minimum ) {
16- minimum = stack .pop ();
21+ int topOfStack = s .pop ();
22+ if (minVal == topOfStack ) {
23+ minVal = s .pop ();
1724 }
1825 }
1926
2027 public int top () {
21- return stack .peek ();
28+ return s .peek ();
2229 }
2330
2431 public int getMin () {
25- return minimum ;
32+ return minVal ;
2633 }
2734}
You can’t perform that action at this time.
0 commit comments