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

2020. 5. 8. 20:36프로그래머스/LEVEL 3

 

import java.util.*;

class Solution {
    public int[] solution(String[] operations) {
        int[] answer = new int[2];
        
        List<Integer> list = new ArrayList<>();
        
        for(int i=0; i<operations.length; i++){
            String[] temp = operations[i].split(" ");
            if(temp[0].equals("I")){
                list.add(Integer.parseInt(temp[1]));
                Collections.sort(list);
            }else{
                if(list.size()!=0){
                if(temp[1].charAt(0)!='-'){
                    list.remove(list.size()-1);
                }else{
                    list.remove(0);
                }
            }
            }
        }
        
        if(list.size()==0){
            answer[1]=0;
            answer[0]=0;
        }else{
            answer[1] = list.get(0);
            answer[0] = list.get(list.size()-1);
        }
    
        return answer;
    }
}

우선순위 큐를 사용하지 않고 리스트를 이용하여 풀었다.

리스트를 사용할 때는 리스트가 비었을때와 아닐때를 나눠 주는 것이 중요

'프로그래머스 > LEVEL 3' 카테고리의 다른 글

[프로그래머스] 베스트 앨범  (0) 2020.05.08
[프로그래머스] 디스크 컨트롤러  (0) 2020.05.02