풀이
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 0;
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
for(int p : priorities){
queue.add(p);
}
while(!queue.isEmpty()){
for(int i = 0; i < priorities.length; i++){
if(queue.peek() == priorities[i]){
queue.poll();
answer++;
if(location == i) return answer;
}
}
}
return answer;
}
}
- 우선 순위 큐를 사용하고 내림 차 순으로 등록.
- 인쇄 목록을 큐에 넣어준다.
- 큐가 빌때까지 반복.
💡 ex → priorities{ 1, 1, 9, 1, 1, 1} / queue{9, 1, 1, 1, 1, 1}
- 큐에 처음 값과 priorities 값을 비교하여 같으면 큐에서 값을 뽑고 answer++를 해준다.
- 만약 값과 함께 위치까지 같으면 answer를 리턴해준다.