import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 1;
// 내림차순으로 정리하는 우선순위 큐 선언
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for(int num : priorities){
pq.offer(num);
}
// pq가 비어있을 때 까지
while(!pq.isEmpty()){
for(int i=0; i<priorities.length; i++){
// 최상단의 값이 대기목록(priorities) 배열에서의 값과 같을때
if(pq.peek()==priorities[i]){
// 원하는 위치(location)가 대기목록(priorities) 배열의 i이기 때문에 같을 때 return
if(i==location){
return answer;
}
// 원하는 위치(location)가 아니기 때문에 최상단의 값을 버려주고 순서(answer)++;
answer++;
pq.poll();
}
}
}
return answer;
}
}
PriorityQueue
우선순위 큐
PriorityQueue<Integer> pq = new PriorityQueue<>();
import java.util.PriorityQueue -> import
숫자가 작을수록 우선순위를 높게 매김 -> 값의 크기를 오름차순으로 정렬해서 큐에 넣어줌
Collections.reverseOrder()를 이용해 내림차순을 우선순위로 큐에 넣어줄수 있음