https://school.programmers.co.kr/learn/courses/30/lessons/42587
import java.io.*;
import java.util.*;
class Node implements Comparable<Node>{
int index;
int value;
Node(int index , int value)
{
this.index = index;
this.value = value;
}
@Override
public String toString()
{
return index+" "+value;
}
@Override
public int compareTo(Node otherNode)
{
return otherNode.value - this.value;
}
}
class Solution {
public static void main(String[] args) {
solution(new int[]{2,1,3,2}, 2);
}
public static int solution(int[] priorities, int location) {
int answer = 0;
Queue<Node>Q = new LinkedList<>();
ArrayList<Node>list = new ArrayList<>();
for (int i = 0; i < priorities.length; i++) {
Q.offer(new Node(i, priorities[i]));
list.add(new Node(i, priorities[i]));
}
Collections.sort(list);
Boolean Flag = false;
int cnt = 0;
for(Node next : list)
{
while (!Q.isEmpty()) {
Node now = Q.poll();
if (next.value == now.value) {
cnt++;
if (now.index == location) {
Flag = true;
}
break;
}else{
Q.offer(now);
}
}
if (Flag == true) {
return cnt;
}
}
return answer;
}
}
작업 목록을 순회하며, 각 작업을 큐와 리스트에 추가한다.작업을 리스트를 기준으로 내림차순으로 정렬한다. 이렇게 하면 우선순위가 높은 작업이 리스트의 앞쪽에 위치하게 된다.리스트를 순회하며 작업을 큐에서 꺼내고, 현재 작업과 리스트의 작업을 비교한다.현재 작업의 우선순위와 리스트의 작업 우선순위가 같다면, 출력 작업의 순서(cnt)를 증가시키고, 현재 작업의 인덱스가 목표 작업 위치(location)와 같은지 확인한다.만약 목표 작업의 위치와 같다면, Flag를 true로 설정하고 반복문을 빠져나간다.Flag가 true일 경우, 현재까지 출력된 작업의 순서인 cnt를 반환한다.만약 Flag가 false라면, 모든 작업을 순회한 후에도 목표 작업이 출력되지 않은 것이므로 answer에는 0이 남아있다. 이 값을 반환한다.