
나의 풀이
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 0;
PriorityQueue<Integer> q = new PriorityQueue<>(Collections.reverseOrder()); // 1
for (int i : priorities) { // 2
q.add(i);
}
while (!q.isEmpty()) { // 3
for (int i = 0; i < priorities.length; i++) {
if (q.peek() == priorities[i]) { // 4
q.remove();
answer++;
if (location == i) { // 5
return answer;
}
}
}
}
return answer;
}
}
과정
- 내림차순으로 우선순위 큐를 선언
- 큐에 priorities 추가
- 큐가 빌 때까지 반복
- 큐의 최상위 헤드가 priorities[i]일때 q의 최상위 헤드를 삭제, answer을 증가
- location과 i가 같아지면 answer 반환
다른 사람 풀이
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 0;
int l = location;
Queue<Integer> que = new LinkedList<Integer>();
for(int i : priorities){
que.add(i);
}
Arrays.sort(priorities);
int size = priorities.length-1;
while(!que.isEmpty()){
Integer i = que.poll();
if(i == priorities[size - answer]){
answer++;
l--;
if(l <0)
break;
}else{
que.add(i);
l--;
if(l<0)
l=que.size()-1;
}
}
return answer;
}
}