프로그래머스-이중 우선 순위 큐

이호영·2022년 4월 9일
0
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;

class Solution {
    public int[] solution(String[] operations) {
     int[] answer = new int[2];
        PriorityQueue<Integer> minQueue = new PriorityQueue<>();
        PriorityQueue<Integer> maxQueue = new PriorityQueue<>(Collections.reverseOrder());
        for (String c : operations) {
            String[] temp = c.split(" ");
            if (temp[0].equals("I")) {
                int value = Integer.parseInt(temp[1]);
                minQueue.add(value);
                maxQueue.add(value);
            } else {
                if (temp[1].equals("-1")) {
                    if (minQueue.isEmpty())
                        continue;
                    while (true) {
                        if (minQueue.isEmpty())
                            break;
                        int a = minQueue.poll();
                        if (maxQueue.contains(a)) {
                            break;
                        }
                    }
                } else {
                    if (maxQueue.isEmpty())
                        continue;
                    while (true) {
                        if (maxQueue.isEmpty())
                            break;
                        int a = maxQueue.poll();
                        if (minQueue.contains(a)) {
                            break;
                        }
                    }
                }
            }
        }
        while (true) {
            if (!minQueue.isEmpty()) {
                int a = minQueue.poll();
                if (maxQueue.contains(a)) {
                    answer[1] = a;
                    break;
                }
            } else {
                break;
            }
        }
        while (true) {
            if (!maxQueue.isEmpty()) {
                int a = maxQueue.poll();
                if (minQueue.contains(a)) {
                    answer[0] = a;
                    break;
                }
            } else {
                break;
            }
        }
        return answer;
    }
}

0개의 댓글