이중 우선순위 큐 (for JAVA)

import java.util.*;

class Solution {
    public int[] solution(String[] operations) {
            int[] answer;
        PriorityQueue<Integer> data = new PriorityQueue<>(); // 순차정렬
        PriorityQueue<Integer> data1 = new PriorityQueue<>(Collections.reverseOrder()); // 역정렬
        for (int i = 0;i< operations.length;i++) {
            if (operations[i].contains("I")) {
                String[] oper = operations[i].split(" ");
                data.add(Integer.parseInt(oper[1]));
                data1.add(Integer.parseInt(oper[1]));
            }
            if (data.isEmpty()) {
                continue;
            }
            if (operations[i].equals("D 1")) { // 최대값 삭제
                int max = data1.peek();
                data.remove(max);
                data1.remove(max);
            } else if (operations[i].equals("D -1")){ // 최소값 삭제
                int min = data.peek();
                data.remove(min);
                data1.remove(min);
            } 
        }
        if (data.size() == 0) {
            answer = new int[]{0,0};
        } else {
            answer = new int[2];
            answer[0] = data1.peek();
            answer[1] = data.peek();
        }
            return answer;  
    }
}

설명은 나중에 너무 졸려서 20000

좋은 웹페이지 즐겨찾기