-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode155.java
More file actions
49 lines (42 loc) · 1.05 KB
/
LeetCode155.java
File metadata and controls
49 lines (42 loc) · 1.05 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Deque;
import java.util.LinkedList;
public class LeetCode155 {
public static void main(String[] args) {
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
System.out.println(minStack.getMin());
minStack.pop();
System.out.println(minStack.top());
System.out.println(minStack.getMin());
}
}
class MinStack {
Deque<Integer> stack1;
Deque<Integer> stack2;
int minVal;
public MinStack() {
this.stack1 = new LinkedList<Integer>();
this.stack2 = new LinkedList<Integer>();
this.stack2.push(Integer.MAX_VALUE);
}
public void push(int val) {
stack1.push(val);
if (stack2.peek() > val) {
stack2.push(val);
} else {
stack2.push(stack2.peek());
}
}
public void pop() {
stack1.pop();
stack2.pop();
}
public int top() {
return stack1.peek();
}
public int getMin() {
return stack2.peek();
}
}