From 931fcae3ca7248dd793a50123671118a999f89b6 Mon Sep 17 00:00:00 2001 From: devwqc Date: Mon, 9 Sep 2024 02:43:57 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[=EC=A0=95=EB=B4=89=EC=B0=AC]=2010-1.=20?= =?UTF-8?q?=EA=B3=84=EB=8B=A8=EC=98=A4=EB=A5=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../index.js" | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git "a/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" "b/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" index 2fba9465..3f34f8cc 100644 --- "a/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" +++ "b/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" @@ -1,7 +1,38 @@ +/* +✅문제 제목: 계단오르기 + +✅문제 유형: DP + +✅문제 풀이 날짜: 2024-09-09 + +💡문제 분석 요약 + - 한 번에 한 계단 또는 두 계단씩 올라간다. + - N계단이 있을 때 올라갈 수 있는 방법의 수는 몇 가지인가? + +💡알고리즘 설계 + - ⭐ DFS 풀이. + - 재귀함수로 레벨(계단)을 매개변수로 받는다. + - 첫 호출 시 1을 인수로 넘겨준다. + - 레벨이 n보다 크거나 같으면 answer을 1더하고 return한다. + - 재귀함수를 레벨 + 1(한 계단), 레벨 + 2(두 계단)을 각각 호출한다. +*/ + function solution(n) { let answer = 0; + function DFS(L) { + if (L >= n) { + answer++; + return; + } + + DFS(L + 1); + DFS(L + 2); + } + + DFS(1); + return answer; } -console.log(solution(7)); +console.log(solution(7)); // 21 From 26f8139aeefd870d51e61178344203f072211829 Mon Sep 17 00:00:00 2001 From: devwqc Date: Mon, 9 Sep 2024 03:10:03 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[=EC=A0=95=EB=B4=89=EC=B0=AC]=2010-1.=20?= =?UTF-8?q?=EA=B3=84=EB=8B=A8=EC=98=A4=EB=A5=B4=EA=B8=B0=20DP=20=ED=92=80?= =?UTF-8?q?=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../index.js" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git "a/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" "b/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" index 3f34f8cc..6f218a74 100644 --- "a/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" +++ "b/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" @@ -36,3 +36,43 @@ function solution(n) { } console.log(solution(7)); // 21 + +/* +✅문제 제목: 계단오르기 + +✅문제 유형: DP + +✅문제 풀이 날짜: 2024-09-09 + +💡문제 분석 요약 + - 한 번에 한 계단 또는 두 계단씩 올라간다. + - N계단이 있을 때 올라갈 수 있는 방법의 수는 몇 가지인가? + +💡알고리즘 설계 + - ⭐ DP 풀이. + - 1번 계단을 가는 경우는 1가지. + - 2번 계단을 가는 경우는 2가지. + - 3번 계단을 가는 경우는 1번에서 가는 경우와 2번에서 가는 경우 즉, 1가지 + 2가지 = 3가지 + - 4번 계단을 가는 경우는 2번에서 가는 경우와 3번에서 가는 경우 즉, 2가지 + 3가지 = 5가지 + - ... + - n번 계단을 가는 경우는 n - 2번에서 가는 경우와 n - 1번에서 가는 경우. +*/ + +function solution(n) { + let answer = 0; + + const dy = Array.from({ length: n + 1 }, () => 0); + + dy[1] = 1; + dy[2] = 2; + + for (let i = 3; i <= n; i++) { + dy[i] = dy[i - 2] + dy[i - 1]; + } + + answer = dy[n]; + + return answer; +} + +console.log(solution(7)); // 21 From d9ab86b2a42a15bd0e494671f0a8f75ffa7a2fbb Mon Sep 17 00:00:00 2001 From: devwqc Date: Mon, 9 Sep 2024 03:22:25 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[=EC=A0=95=EB=B4=89=EC=B0=AC]=2010-1.=20?= =?UTF-8?q?=EA=B3=84=EB=8B=A8=EC=98=A4=EB=A5=B4=EA=B8=B0=20=EB=8B=A4?= =?UTF-8?q?=EB=A5=B8=20=ED=92=80=EC=9D=B4=20=ED=95=A8=EC=88=98=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\354\240\225\353\264\211\354\260\254/index.js" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" "b/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" index 6f218a74..f551daa6 100644 --- "a/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" +++ "b/10. Dynamic programming(\353\217\231\354\240\201\352\263\204\355\232\215\353\262\225)/01. \352\263\204\353\213\250\354\230\244\353\245\264\352\270\260/\354\240\225\353\264\211\354\260\254/index.js" @@ -58,7 +58,7 @@ console.log(solution(7)); // 21 - n번 계단을 가는 경우는 n - 2번에서 가는 경우와 n - 1번에서 가는 경우. */ -function solution(n) { +function solution2(n) { let answer = 0; const dy = Array.from({ length: n + 1 }, () => 0); @@ -75,4 +75,4 @@ function solution(n) { return answer; } -console.log(solution(7)); // 21 +console.log(solution2(7)); // 21