링크
코딩테스트 연습 > 힙(Heap) > 이중우선순위큐
코드
import java.util.*;
class Solution {
public int[] solution(String[] operations) {
int[] answer = {0,0};
PriorityQueue <Integer> priorityQueueWithMax = new PriorityQueue<>(Comparator.reverseOrder());
PriorityQueue <Integer> priorityQueueWithMin = new PriorityQueue<>();
for (String operation : operations) {
String[] splitOther = operation.split(" ");
if (splitOther[0].equals("I")) {
priorityQueueWithMax.add(Integer.parseInt(splitOther[1]));
priorityQueueWithMin.add(Integer.parseInt(splitOther[1]));
}
if (splitOther[0].equals("D")) {
if (!priorityQueueWithMax.isEmpty()) {
if (splitOther[1].equals("1")) {
int max = priorityQueueWithMax.peek();
priorityQueueWithMax.remove(max);
priorityQueueWithMin.remove(max);
} else {
int min = priorityQueueWithMin.peek();
priorityQueueWithMax.remove(min);
priorityQueueWithMin.remove(min);
}
}
}
}
if (!priorityQueueWithMax.isEmpty()) {
answer[0] = priorityQueueWithMax.peek();
answer[1] = priorityQueueWithMin.peek();
}
return answer;
}
}