[JavaScript/section 6] 07 - ๊ต์œก๊ณผ์ • ์„ค๊ณ„
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ 07 - ๊ต์œก๊ณผ์ • ์„ค๊ณ„(ํ) ํ๋ฅผ ์ „๋ฐ˜์ ์œผ๋กœ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค. JavaScript์—์„œ ํ๋Š” ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ’€์ด ๋ฐฉ๋ฒ• ํ•„์ˆ˜ ๊ณผ๋ชฉ ์ˆœ์„œ๊ฐ€ ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— Array.from()์„ ์ด์šฉํ•ด ๋ฐฐ์—ด๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. plan์— ๋Œ€ํ•œ for ๋ฌธ์„ ๋Œ๋ฉด์„œ ํ•„์ˆ˜ ๊ณผ๋ชฉ์ธ ์ˆ˜์—…์ธ์ง€ ํ™•์ธํ•œ๋‹ค. ํ•„์ˆ˜ ๊ณผ๋ชฉ์ธ ์ˆ˜์—…์— ํ•ด๋‹นํ•œ๋‹ค๋ฉด, ์ฃผ์–ด์ง„ ํ•„์ˆ˜ ๊ณผ๋ชฉ ์ˆœ์„œ ์ค‘ ์ฒซ ๋ฒˆ์งธ ์ˆœ์„œ์ธ ๊ณผ๋ชฉ๊ณผ ๊ฐ™์€์ง€ ๋น„๊ตํ•œ๋‹ค. ์ˆ˜์—… ๊ณ„ํš์— ํ•„์ˆ˜ ๊ณผ๋ชฉ์ด ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— for ๋ฌธ์ด ๋๋‚˜๋ฉด ๋ฐฐ์—ด์— ๊ฐ’์ด ๋‚จ์•„์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๐Ÿ“ ํ’€์ด function solution(need, plan) { let answer = 'YES'; queue = Array.from(need); for (let x of pla..
[JavaScript/section 6] 06 - ๊ณต์ฃผ ๊ตฌํ•˜๊ธฐ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ 06 - ๊ณต์ฃผ ๊ตฌํ•˜๊ธฐ(ํ) ํ(queue) ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ž˜ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๊ฐ„๋‹จํžˆ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค. ํ’€์ด ๋ฐฉ๋ฒ• while ๋ฌธ์„ ์ด์šฉํ•ด ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 1์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. for ๋ฌธ์„ ์ด์šฉํ•ด k - 1๋ฒˆ ๋Œ๋ฉฐ shift()๋ฅผ ํ•œ ํ›„ ๋ฐฐ์—ด์— ์ž๋ฃŒ๋ฅผ ๋‹ค์‹œ ๋„ฃ์–ด์ค€๋‹ค. k๋ฒˆ์งธ ์ž๋ฃŒ์˜ ๊ฒฝ์šฐ ์ œ์™ธ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ shift()๋ฅผ ํ•œ๋‹ค. ๐Ÿ“ ํ’€์ด function solution(n, k) { let answer; let queue = Array.from({ length: n }, (v, i) => i + 1); while (queue.length !== 1) { for (let i = 1; i < k; i++) { queue.push(queue.shift()); } queue.shift(); } answer ..
_์„ฑํ˜ธ_
'ํ' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก