.png)
하라는 대로만 구현하는 문제다.
당연히 파이썬 deque같은 걸 쓰면 더 편하다.
배열 재정리.
[1,1,9,1,1,1] =>
[[1,0], [1,1], [9,2], [1,3], [1,4], [1,5]]
로 인덱스값을 부여해서 배열을 다시 만들었다.
예제에선 location = 0이므로 [1,0]이 언제 출력되는지 알아내보자.
1보다 큰 값이 있으므로 맨 뒤로 넘긴다.
9를 출력하고 count를 1 증가한다. (첫 번째 문서가 출력됐으니까)
[1,3], [1,4], [1,5], [1,0], [1,1]이 남았다.
[1,0]이 나올 때까지 계속 출력하며 count를 증가시킨다.
location이 일치하는 숫자가 나오면 해당 count를 return하고 종료.
function solution(priorities, location) {
let count = 1;
priorities = priorities.map((_, i) => [_, i]);
while (priorities.length > 0) {
let head = priorities[0];
if (priorities.some((arr) => arr[0] > head[0])) {
priorities.splice(0, 1);
priorities.push(head);
} else {
if (head[1] === location) {
return count;
}
priorities.splice(0, 1);
count++;
}
}
}