[프로그래머스]stack/queue-프린터

snusun·2021년 2월 2일
0

Collections.reverseOrder()를 까먹어서 조금 애를 먹었지만 출력 찍어보며 해결.

맨 앞 요소가 가장 우선순위가 높은지 확인하고, 그 요소가 내가 확인하고자 하는 프린트인지 확인한다. 조건에 따라 location과 answer의 값을 달리하며 내가 출력하고자하는 프린트의 순서를 정한다.

import java.util.*;

class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 1;
        ArrayList<Integer> print = new ArrayList<>();
        PriorityQueue<Integer> prior = new PriorityQueue<>(Collections.reverseOrder());

        for(int i=0; i<priorities.length; i++){
            print.add(priorities[i]);
            prior.add(priorities[i]);
        }

        while(!print.isEmpty()){
            if(print.get(0) < prior.peek()){
                if(location==0){
                    location = print.size()-1;
                } else{
                    location--;
                }
                int p = print.remove(0);
                print.add(p);
            } else{
                if(location==0){
                    return answer;
                } else{
                    print.remove(0);
                    prior.poll();
                    answer++;
                    location--;
                }
            }
        }
        return answer;
    }
}
profile
대학생 근데 이제 컴공을 곁들인

0개의 댓글