-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStack.java
More file actions
34 lines (28 loc) · 649 Bytes
/
MinStack.java
File metadata and controls
34 lines (28 loc) · 649 Bytes
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
import java.util.Stack;
public class MinStack {
private Stack<Integer> nums;
private Stack<Integer> mins;
/** initialize your data structure here. */
public MinStack() {
nums = new Stack<>();
mins = new Stack<>();
}
public void push(int x) {
nums.push(x);
if (mins.isEmpty() || x <= mins.peek()) {
mins.push(x);
}
}
public void pop() {
int top = nums.pop();
if (mins.peek() == top) {
mins.pop();
}
}
public int top() {
return nums.peek();
}
public int getMin() {
return mins.peek();
}
}