링크
PriorityQueue 우선 순위큐
import java.util.Queue;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Collections;
class Solution {
public int solution(int[] priorities, int location) {
//순서를 저장해둘 큐하나
Queue<Integer> queue = new LinkedList<>();
//최댓값을 저장해두도록 Collect을 이용하여 내림차순 정렬을한
//우선 순위큐 하나
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Collections.reverseOrder());
//길이만큼 돌면서 우선순위큐에는 값을
//큐에는 해당 값의 위치 값을 넣어줍니다.
//왜 위치값이냐?
//location이 위치니 비교하다가 위치에
//해당하는 것이 최대 값이 라고 한다면 찾았으니 멈춰야하기
for (int i = 0; i < priorities.length; i++) {
queue.offer(i);
priorityQueue.offer(priorities[i]);
}
int answer = 0;
while (!queue.isEmpty()) {
//큐에 맨 앞을 뺍니다.
int currentProcess = queue.poll();
//해당 순서의 값이 우선순위 큐에서
//최대 값과 같다면 실행되었으니 +1을하고
//우선순위 큐에서 빼줍니다.
if (priorities[currentProcess] == priorityQueue.peek()) {
answer++;
priorityQueue.poll();
//찾던 순서와 큐의 순서가 같다면
//드디어 찾았군요 종료합시다.
if (currentProcess == location)
break;
} else {
//만약 최댓값이 아니라면 다시 맨뒤에 넣어서 찾기를 반복합니다.
queue.offer(currentProcess);
}
}
return answer;
}
}