레벨 2
https://programmers.co.kr/learn/courses/30/lessons/42587
이 문제는 우선순위 큐를 활용해서 푸는 문제이다.
문제가 어려워 다른 답안을 참고해서 풀었다.
다시 제대로 풀어보고 이해하도록 하자.
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 1; // 출력순서. 몇번째로 출력되는가 ?
// 우선순위 큐에 문서 우선순위 저장 (우선순위 내림차순 정렬)
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for(int priority:priorities){
pq.add(priority);
}
// 우선순위 높은 순으로 기존 배열에서 일치하는 문서 찾기
while(!pq.isEmpty()){
for(int i=0;i<priorities.length;i++){
if(pq.peek()==priorities[i]){
// 우선순이ㅜ큐에서 읽은 요소가 location에 위치한 요소일 경우 출력순서리턴
if(i==location)
return answer;
answer++;
pq.poll(); // 값을 찾았을 때만 큐에서 빼야함
}
}
}
return answer;
}
}
https://bada744.tistory.com/102
https://youngest-programming.tistory.com/319