https://school.programmers.co.kr/learn/courses/30/lessons/42628
PriorityQueue를 min, max값을 담는 걸로 두개 작성해서
최대값 뺄때는 maxQ.poll(), 최솟값을 뺄때는 minQ.poll을 한다.
import java.util.*;
class Solution {
public int[] solution(String[] operations) {
PriorityQueue<Integer> minQ = new PriorityQueue<>();
PriorityQueue<Integer> maxQ = new PriorityQueue<>((a, b) -> b - a);
int size = 0;
for(String operation : operations){
String[] op = operation.split(" ");
switch(op[0]){
case "I" -> {
minQ.add(Integer.parseInt(op[1]));
maxQ.add(Integer.parseInt(op[1]));
size++;
}
case "D" -> {
if(op[1].equals("1")){
if(maxQ.size() == 0) continue;
maxQ.poll();
if(--size == 0){
minQ.clear();
maxQ.clear();
}
} else{
if(minQ.size() == 0) continue;
minQ.poll();
if(--size == 0){
minQ.clear();
maxQ.clear();
}
}
}
}
}
int[] answer = new int[2];
answer[0] = (maxQ.size() == 0) ? 0 : maxQ.peek();
answer[1] = (minQ.size() == 0) ? 0 : minQ.peek();
return answer;
}
}