-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (35 loc) · 1.39 KB
/
Solution.java
File metadata and controls
39 lines (35 loc) · 1.39 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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
class Solution {
public int[] solution(String[] operations) {
int[] answer = {};
PriorityQueue<Integer> minPQ = new PriorityQueue<>();
PriorityQueue<Integer> maxPQ = new PriorityQueue<>(Collections.reverseOrder());
List<Integer> numbers = new ArrayList<>();
for(String operation : operations){
String [] ops = operation.split(" ");
if(ops[0].equals("I")){
numbers.add(Integer.valueOf(ops[1]));
minPQ.add(Integer.valueOf(ops[1]));
maxPQ.add(Integer.valueOf(ops[1]));
} else {
if(ops[1].charAt(0) == '1' && !numbers.isEmpty()){ // 최댓값
int maxValue = maxPQ.poll();
numbers.remove(Integer.valueOf(maxValue));
minPQ.remove(Integer.valueOf(maxValue));
} else if(ops[1].charAt(0) != '1' && !numbers.isEmpty()){
int minValue = minPQ.poll();
numbers.remove(Integer.valueOf(minValue));
maxPQ.remove(Integer.valueOf(minValue));
}
}
}
if(numbers.isEmpty()){
return new int[]{0,0};
} else {
return new int[]{maxPQ.poll(), minPQ.poll()};
}
}
}