[JavaScript/section 8] 04 - ๋ถ€๋ถ„์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ 04 - ๋ถ€๋ถ„์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ(DFS) ์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง€๋ฉด 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ์›์†Œ๋ฅผ ๊ฐ–๋Š” ์ง‘ํ•ฉ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ ๊ฐ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ์•„๋ž˜์˜ ์ถœ๋ ฅ์˜ˆ์ œ์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ, ๊ณต์ง‘ํ•ฉ์€ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ž…๋ ฅ์˜ˆ์ œ 3 ์ถœ๋ ฅ์˜ˆ์ œ 1 2 3 1 2 1 3 1 2 3 2 3 ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ๊ฐœ์ˆ˜๋Š” 2^N ์ด์ง€๋งŒ ๊ณต์ง‘ํ•ฉ์€ ์ถœ๋ ฅํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ -1์„ ํ•ด์ค€๋‹ค. N์ด 3์ด๋ฉด ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š” ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ๊ฐœ์ˆ˜๋Š” 7๊ฐœ๊ฐ€ ๋œ๋‹ค. ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ์ฐพ์•„๊ฐˆ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋ฉด์„œ ์ดํ•ด๋ฅผ ํ•˜๋ฉด ๋„์›€์ด ๋œ๋‹ค. ๐Ÿ“ ์†Œ์Šค ์ฝ”๋“œ function solution(n) { let answer = []; let ch = Array.from({ length: n + 1 }, () => 0..
_์„ฑํ˜ธ_
'๋ถ€๋ถ„์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก