알고리즘

[프로그래머스/Java] 이중우선순위큐

데메즈 2022. 1. 10. 22:45
728x90
반응형

https://programmers.co.kr/learn/courses/30/lessons/42628?language=java 

 

코딩테스트 연습 - 이중우선순위큐

 

programmers.co.kr

 

import java.util.*;

class Solution {
    public int[] solution(String[] operations) {
        int[] answer = {0, 0};
        
        PriorityQueue<Integer> minq = new PriorityQueue();
        PriorityQueue<Integer> maxq = new PriorityQueue(Comparator.reverseOrder());
        
        for(String s : operations){
            String[] temp = s.split(" ");
            
            if(temp[0].equals("I")){
                minq.offer(Integer.parseInt(temp[1]));
                maxq.offer(Integer.parseInt(temp[1]));
            } else if(minq.isEmpty()){
                continue;
            } else if(temp[1].equals("1")){
                int max = maxq.peek();
                minq.remove(max);
                maxq.remove(max);
            } else {
                int min = minq.peek();
                minq.remove(min);
                maxq.remove(min);
            }
        }
        
        if(!minq.isEmpty()){
            answer[0] = maxq.poll();
            answer[1] = minq.poll();
        } 
        
        return answer;
    }
}
728x90
반응형