๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

42587. ํ”„๋ฆฐํ„ฐ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ”„๋ฆฐํ„ฐ


๋ฌธ์ œ ์œ ํ˜•๋‚œ์ด๋„๊ฑธ๋ฆฐ ์‹œ๊ฐ„ํ•ด๊ฒฐ ์œ ๋ฌด(โœ…/โŒ)
์Šคํƒ/ํlv.220๋ถ„โœ…

์„ค๊ณ„ ๋ฐฉ๋ฒ•#

  • ๋ฌธ์„œ์˜ย 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) ์—ฐ์‚ฐ ์—†์ด ํ˜„์žฌ ์š”์†Œ๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋” ๋†’์€ ์š”์†Œ๋ฅผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ์Œ.

์ฐธ๊ณ ์ž๋ฃŒ#