42587. ํ๋ฆฐํฐ
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ํ๋ฆฐํฐ
| ๋ฌธ์ ์ ํ | ๋์ด๋ | ๊ฑธ๋ฆฐ ์๊ฐ | ํด๊ฒฐ ์ ๋ฌด(โ /โ) |
|---|---|---|---|
| ์คํ/ํ | lv.2 | 20๋ถ | โ |
์ค๊ณ ๋ฐฉ๋ฒ#
๋ฌธ์์
index๋ฅผ ๋ด๊ณ ์๋ ํ์prioritiesํ๋ฅผ ๋์์ ๊ด๋ฆฌํจ.๋ฌธ์ ์ ๋์์๋ ์ค๋ช ๊ทธ๋๋ก ๊ตฌํํจ.
์ฝ๋#
function solution(priorities, location) { const waiting = priorities.map((_, idx) => idx); const finished = [];
while (priorities.length) { const cur = priorities.shift(); const curIdx = waiting.shift(); if (priorities.some((priority) => priority > cur)) { priorities.push(cur); waiting.push(curIdx); continue; } finished.push(curIdx); }
return finished.findIndex((el) => el === location) + 1;}์๊ฐ ๋ณต์ก๋#
์ฐ์ ์์ ๋ฐฐ์ด์ ์ํํ๋ฉฐ (n) ์ฐ์ ์์ ๋ฐฐ์ด ๋ด์ ํ์ฌ ์์๋ณด๋ค ์ฐ์ ์์๊ฐ ๋ ๋์ ์์๊ฐ ์๋์ง ๊ฒ์ (n) => O(n^2)
์ถ๊ฐ์ ์ผ๋ก ํ๊ฐ ์ต์ ํ๋์ด ์์ง ์์์ shift ์ฐ์ฐ ๋ง๋ค O(n)
=> O(n^2)
์ด๋ ค์ ๋ ์ #
๋ฌธ์ ์ค๋ช ์ ๋ฐ๋ผ์ ์ฝ๊ฒ ํ ์ ์์์.
O(n^2) ์ฐ์ฐ ์์ด ํ์ฌ ์์๋ณด๋ค ์ฐ์ ์์๊ฐ ๋ ๋์ ์์๋ฅผ ๊ฒ์ํ ์ ์๋์ง ๋ชจ๋ฅด๊ฒ ์.