https://programmers.co.kr/learn/courses/30/lessons/42587
function solution(priorities, location) {
let count = 0;
const target = priorities[location];
const priorityNums = [...priorities].sort((a,b) => a - b);
priorityNums.__proto__.last = function() {
return this[this.length-1];
}
while(priorities.length > 0) {
const current = priorities.shift();
if (current < priorityNums.last()) {
priorities.push(current);
if (location === 0) {
location = priorities.length -1;
} else {
location--;
}
} else {
count++;
priorityNums.pop();
if (location === 0) {
break;
} else {
location--;
}
}
}
return count;
}
제한사항의 값이 크지 않아서 성능이 좋지 않아도 해결할 수 있는 문제인 것 같다. 파이썬처럼 배열에 -1로 마지막 인덱스를 조회할 수 있으면 좋을 것 같다는 생각을 하면서 그냥 last라는 메소드를 추가해봤다. 만약 Array에 원래 last가 있으면 문제가 있을 것 같기도 한데 짧은 코드니 추가를 해서 사용했다. 다른 코드에 영향을 안 받게 Proxy를 이용하는 방법도 좋을 것 같다.
알고리즘 자체는 문제에서 설명한 거의 그대로를 따라갔다.