๋ฌธ์ : https://school.programmers.co.kr/learn/courses/30/lessons/42587
์นดํ ๊ณ ๋ฆฌ: ํ
์ถ์ฒ: ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉ ํ ์คํธ ๊ณ ๋์ Kit, https://school.programmers.co.kr/learn/challenges?tab=algorithm_practice_kit
์ต๋ํ ์ ์ธํ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ๋ฒ์ผ๋ก ์ฝ๋๋ฅผ ์ง๋ ค๊ณ ๋ ธ๋ ฅํ๋ค.
์ฃผ์ด์ง ์ฐ์ ์์๋ค์ ๋ฐฐ์ด์ ๊ฐ ์์์ index์ ๋ฌถ์ด ์๋ก์ด ๋ฐฐ์ด queue
์ ์์ฑํ๋ค.
const queue = priorities.map((priority, index) => {
return { priority, index };
});
queue
์ ๋งจ ์ ์์์ priority
๊ฐ ๋๋จธ์ง ์์๋ค๋ณด๋ค ๊ฐ๊ฑฐ๋ ํฌ๋ฉด queue
์์ ๋ด๋ณด๋ด๊ณ , cnt
๋ฅผ 1 ์ฆ๊ฐ์ํจ๋ค. ์๋๋ผ๋ฉด, queue
์ ๋งจ ๋ค๋ก ์ด๋์ํจ๋ค.
๋ด๋ณด๋ด๋ ค๋ ์์์ index
๊ฐ location
๊ณผ ๊ฐ๋ค๋ฉด, cnt
๋ฅผ return ํ๊ณ ์ข
๋ฃํ๋ค.
function solution(priorities, location) {
const queue = priorities.map((priority, index) => {
return { priority, index };
});
let cnt = 1;
while (queue.length) {
if (queue.every((item) => item.priority <= queue[0].priority)) {
if (queue[0].index === location) {
return cnt;
} else {
queue.shift();
cnt++;
}
} else {
queue.push(queue.shift());
}
}
return cnt;
}