import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
Queue<int[]> queue = new LinkedList<>();
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); // 우선순위 큐 (내림차순)
for (int i = 0; i < priorities.length; i++) {
queue.add(new int[] {i, priorities[i]});
pq.add(priorities[i]);
}
int answer = 0;
int cnt = 0;
while (!queue.isEmpty()) {
int[] current = queue.poll();
if (current[1] == pq.peek()) { // 현재 문서가 가장 높은 우선순위일 경우
pq.poll();
cnt++;
if (current[0] == location) answer = cnt;
} else {
queue.add(current); // 우선순위가 낮으면 다시 뒤로 보냄
}
}
return answer;
}
}