코딩 테스트

(Java) 프로그래머스 프린터

로승리 2022. 1. 16. 22:57

간단해 보이는 듯 하지만 은근히 생각해야 할게 많아서 꽤나 재미있게 풀었던 문제였다.

 


최종 코드

import java.util.Collections;
import java.util.PriorityQueue;
class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
       // 내림차순으로 우선순위 큐 만들기
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());

        for(int n : priorities){
            pq.offer(n);
        }

        // 큐가 비어있지 않다면
        while(!pq.isEmpty()){
            // 큐에서 나오는 값과 매칭되는 경우를 탐색.
            for(int i = 0;i<priorities.length;i++){
                // 값만 일치하는 경우 해당 문서 출력.
                if(pq.peek() == priorities[i] ){
                    pq.poll();
                    answer++;
                    // 값과 인덱스가 모두 일치하면 반환
                    if(location == i ) return answer;
                }
            }
        }
        return answer;
    }
}