-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinStack.java
46 lines (39 loc) · 1.09 KB
/
MinStack.java
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
package techQuestions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class MinStack {
public static void main(String[] args) {
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
System.out.println(minStack.getMin()); // should return -3
minStack.pop();
System.out.println(minStack.top()); // should return 0
System.out.println(minStack.getMin()); // should return -2
}
/** initialize your data structure here. */
List<Integer> list = new ArrayList<Integer>();
LinkedList<Integer> linked = new LinkedList<>();
public MinStack() {
}
public void push(int x) {
linked.addFirst(x);
list.add(x);
Collections.sort(list);
}
public void pop() {
int removedInt = linked.get(0);
linked.remove();
int listIndex = list.indexOf(removedInt);
list.remove(listIndex);
}
public int top() {
return linked.getFirst();
}
public int getMin() {
return list.get(0);
}
}