[JavaScript/section 8] 11 - ํŒฉํ† ๋ฆฌ์–ผ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ 11 - ํŒฉํ† ๋ฆฌ์–ผ(DFS) ์ž์—ฐ์ˆ˜ N์„ ์ž…๋ ฅํ•˜๋ฉด N! ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. N! = N * (N - 1) * (N - 2) * ... * 1์ด๋‹ค. ๋งŒ์•ฝ N์ด 5๋ผ๋ฉด 5! = 5 * 4 * 3 * 2 * 1 = 120์ด๋‹ค. ๐Ÿ‘ ํ’€์ด ๋ฐฉ๋ฒ• ์žฌ๊ท€๋ฅผ ๋Œ๋ฉด์„œ n์ด 1๊ณผ ๊ฐ™๋‹ค๋ฉด 1์„ returnํ•œ๋‹ค. function solution(n) { let answer; function DFS(n) { if (n === 1) return 1; else return n * DFS(n - 1); } answer = DFS(n); return answer; } console.log(solution(5));
_์„ฑํ˜ธ_
'ํŒฉํ† ๋ฆฌ์–ผ' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก