Skip to content

Commit 8bf6e50

Browse files
authored
Update Min Stack.java
1 parent 9389da7 commit 8bf6e50

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

Easy/Min Stack.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
class MinStack {
2-
3-
/** initialize your data structure here. */
4-
Stack<Integer> stack;
5-
Stack<Integer> min;
2+
private final Stack<int[]> stack;
3+
64
public MinStack() {
7-
stack = new Stack<>();
8-
min = new Stack<>();
9-
}
5+
this.stack = new Stack<>();
6+
}
107

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});
1411
}
1512

1613
public void pop() {
1714
stack.pop();
18-
min.pop();
1915
}
2016

2117
public int top() {
22-
return stack.peek();
18+
return stack.peek()[0];
2319
}
2420

2521
public int getMin() {
26-
return min.peek();
22+
return stack.peek()[1];
2723
}
2824
}
2925

3026
/**
3127
* Your MinStack object will be instantiated and called as such:
3228
* MinStack obj = new MinStack();
33-
* obj.push(x);
29+
* obj.push(val);
3430
* obj.pop();
3531
* int param_3 = obj.top();
3632
* int param_4 = obj.getMin();

0 commit comments

Comments
 (0)