문제 보자마자 우선순위 큐를 생각했지만 이걸 어떻게 풀어야 할지 한시간 반동안 풀지를 못해서 여러검색을 시작하였음. 큐는 linkedlist로 풀어야 했다. poll() 함수와 offer() 함수를 적절히 이용하는 것인데, 스택과 다른점은 fifo라는 점이라 pop()역할을 하는 poll()이 처음에 넣었던 것을 반환한다는 것이다.
import java.util.*;
class Solution {
class Test {
int num;
int value;
Test(int num, int value) {
this.num = num;
this.value = value;
}
}
public int solution(int[] priorities, int location) {
int answer = 0;
LinkedList<Test> ll = new LinkedList<Test>();
for (int i = 0;i<priorities.length;i++) {
ll.offer(new Test(i,priorities[i]));
}
while(!ll.isEmpty()) {
Test test = ll.poll();
int leftLl = 0;
for(Test t:ll) {
if (test.value < t.value) {
ll.offer(test);
break;
} else {
leftLl++;
}
}
if (leftLl==ll.size() && test.num == location) {
return priorities.length - leftLl;
}
}
return answer;
}
}
leftLl 은 poll() 후 남아있는 값들을 모두 비교했을때 test인자보다 같거나 작으면 더해주는 것으로 현재 남아있는 인자들과 값이 같으면 이것이 남아있는 값중 가장 큰 값으로 보게 되며 이것의 입력순서가 반환하는 값과 같으면 푸시된 번호를 반환한다. 해당 번호는 원래 리스트의 개수 - 남은 값들의 개수 이다