이미지를 클릭하시면 문제 링크로 연결됩니다.
import java.util.Collections;
import java.util.PriorityQueue;
// 5일차 (연결리스트) - 프로그래머스 프로세스 문제 (스택/큐)
public class day05Prog42587 {
public static int solution(int[] priorities, int location) {
int answer = 0;
// 우선순위 큐 (내림차순)
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
// priorities 의 원소들을 우선순위 큐에 넣어준다.
for(int i : priorities) pq.add(i);
while(!pq.isEmpty()) { // 큐가 빌때까지 루프
for (int i = 0; i < priorities.length; i++) {
if(priorities[i] == pq.peek()){ // 배열의 우선순위와 큐의 우선순위가 같다면
pq.poll(); // 해당 큐를 제거해주고
answer++; // 카운트를 증가시킨다.
if (i == location) { // 이때, 찾는 location 이라면
return answer; // 답을 반환한다.
}
}
}
}
return answer;
}
}