-문제링크
https://programmers.co.kr/learn/courses/30/lessons/42587?language=javascript
해당 문제의 집중해야 할 부분은 큐(Queue)를 사용해야 한다는 것이다.
function solution(priorities, location) {
var answer = 0;
var result = location;
while(priorities.length > 0) {
//뒷 수가 더 클 때
if(priorities.some(x => x > priorities[0])) {
priorities.push(priorities[0]) //뒤에 첫번째 수를 넣어주고
priorities.shift(); //앞에 수를 삭제한다.
//index 0번째가 몇번째로 나오는지 궁금할 때
if(result === 0) {
result += priorities.length - 1;
}else {
//index가 1이상일 때 한칸씩 땡겨준다.
result -= 1;
}
}else {
//맨 앞이 가장 클 때
answer += 1;
priorities.shift();
if(result === 0) {
break;
}else {
//다음번이 궁금할 때
result -= 1;
}
}
}
return answer;
}