티스토리 뷰

Link

코딩테스트 연습 - 프린터

Code

import java.util.*;
class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0 ;
        PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
        for(int i : priorities){
            queue.add(i);
        }


        int count = 0 ;
        while(!queue.isEmpty()){
            for(int i =0 ; i < priorities.length; i++){
                if(priorities[i] == queue.peek()){
                    count++;
                    queue.poll();
                    if(i == location){
                        answer =  count;
                        queue.clear();
                        break;
                    }
                }

            }
        }
        return answer;
    }
}

Explain

👉
1. 우선순위가 높은 순으로 찾기 위해 우선순위 큐를 사용 2. 우선순위큐에서 priorities 순서대로 같은 것을 찾으면서 우선순위큐 하나씩 지운다 3. 만약 location과 priorities의 순서가 같다면 모두 종료