알고리즘 풀이/프로그래머스
[level2] 프로그래머스 - 프린터(JAVA)
데롱디롱
2021. 9. 17. 01:01
728x90
[ 풀이 방법 ]
- 인쇄 중요도가 높은 문서부터 차례대로 뽑는다. => 우선순위 큐
- queue에서 하나씩 꺼낼때마다 priorities[i]에서 그 값을 찾고 answer을 증가시킨다.
이때, i가 만약 location이라면 answer을 리턴한다.
[ 전체 코드 ]
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 1;
PriorityQueue<Integer> queue = new PriorityQueue<Integer>(Comparator.reverseOrder());
for(int n : priorities)
queue.offer(n);
while(!queue.isEmpty()) {
for(int i = 0; i < priorities.length; i++) {
if(queue.peek() == priorities[i]) {
if(location == i)
return answer;
answer++;
queue.poll();
}
}
}
return answer;
}
}