https://school.programmers.co.kr/learn/courses/30/lessons/42587
단순한 큐 활용 문제..
해당 프로세스보다 우선순위가 높은 프로세스가 있는지 체크를 해야하므로
우선순위를 정렬하고, 해당 우선순위를 가진 프로세스가 처리될때마다 우선순위의 index값을 옮겼다.
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
Queue<Process> processes = new LinkedList();
for (int i = 0; i < priorities.length; i++) {
processes.add(Process.of(i, priorities[i]));
}
Arrays.sort(priorities);
int index = priorities.length - 1;
int cnt = 0;
while(!processes.isEmpty()) {
Process process = processes.poll();
if (priorities[index] == process.priority) {
index--;
cnt++;
if (process.location == location) return cnt;
} else {
processes.add(process);
}
}
return cnt;
}
static class Process {
int location;
int priority;
static Process of(int location, int priority) {
Process vo = new Process();
vo.location = location;
vo.priority = priority;
return vo;
}
}
}