Skip to content

Commit 03aecc1

Browse files
author
jinvicky
committed
climbing stairs solution
1 parent 842cba9 commit 03aecc1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int climbStairs(int n) {
3+
// n=1μΌλ•Œ 방법은 1κ°€μ§€λ‹€.
4+
// n=2μΌλ•Œ 방법은 2κ°€μ§€λ‹€.
5+
// n=3μΌλ•Œ 방법은 3κ°€μ§€λ‹€.
6+
// n=4μΌλ•Œ 방법은 5κ°€μ§€λ‹€.
7+
// 1,1,1,1
8+
// 1,1,2
9+
// 1,2,1
10+
// 2,1,1
11+
// 2,2
12+
// dp μ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ 4λŠ” 2와 3의 방법 개수λ₯Ό μž¬ν™œμš©ν•΄μ„œ dp[n] = dp[n-2] + dp[n-1] 을 λ„μΆœν•  수 μžˆλ‹€.
13+
// 단 n이 1,2,3일 λ•Œμ˜ 값을 λ¨Όμ € μ…‹νŒ…ν•œλ‹€.
14+
15+
if (n <= 3)
16+
return n;
17+
18+
int[] dp = new int[n];
19+
dp[0] = 1;
20+
dp[1] = 2;
21+
dp[2] = 3;
22+
23+
for (int i = 3; i < n; i++) {
24+
dp[i] = dp[i - 1] + dp[i - 2];
25+
}
26+
27+
return dp[n - 1];
28+
}
29+
}

0 commit comments

Comments
Β (0)