728x90
๋ฐ์ํ
๐ 02 - ๋๋ค๋ฆฌ ๊ฑด๋๊ธฐ(DP)
์ฒ ์๋ ํ๊ต์ ๊ฐ๋๋ฐ ๊ฐ์ธ์ ๋ง๋ฌ๋ค. ๊ฐ์ธ์ N๊ฐ์ ๋๋ก ๋ค๋ฆฌ๋ฅผ ๋ง๋ค์ด ๋์๋ค. ์ฒ ์๋ ๋ ๋ค๋ฆฌ๋ฅผ ๊ฑด๋ ๋ ํ ๋ฒ์ ํ ์นธ ๋๋ ๋ ์นธ์ฉ ๊ฑด๋๋ฐ๋ฉด์ ๋๋ค๋ฆฌ๋ฅผ ๊ฑด๋ ์ ์๋ค. ์ฒ ์๊ฐ ๊ฐ์ธ์ ๊ฑด๋๋ ๋ฐฉ๋ฒ์ ๋ช๊ฐ์ง์ผ๊น?
๐ ์์ค ์ฝ๋
์ฐธ๊ณ ) ๊ณ๋จ์ค๋ฅด๊ธฐ
function solution(n) {
let answer = 0;
let dy = Array.from({ length: n + 2 }, () => 0);
dy[1] = 1;
dy[2] = 2;
for (let i = 3; i <= n + 1; i++) {
dy[i] = dy[i - 2] + dy[i - 1];
}
answer = dy[n + 1];
return answer;
}
console.log(solution(7));
'Algorithm > ์ธํ๋ฐ(inflearn)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript/section 10] 04 - ๋์ ๊ตํ (0) | 2022.12.22 |
---|---|
[JavaScript/section 10] 03 - ์ต๋ ๋ถ๋ถ ์ฆ๊ฐ ์์ด (0) | 2022.12.21 |
[JavaScript/section 10] 01 - ๊ณ๋จ์ค๋ฅด๊ธฐ (0) | 2022.12.17 |
[JavaScript/section 9] 07 - ์ฌ๋๋ผ ์์ผ๋๋(BFS) (0) | 2022.11.28 |
[JavaScript/section 9] 06 - ์ฌ๋๋ผ ์์ผ๋๋(DFS) (0) | 2022.11.24 |