Skip to content

Commit 8bb51fb

Browse files
committed
climbing stairs
1 parent 48366a6 commit 8bb51fb

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

climbing-stairs/eunhwa99.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// n=2 (1,1), (2) -> 2 가지
2+
// n=3 (n=2, 1), (n=1, 2) -> 2 + 1 = 3가지
3+
// n=4 (n=3, 1), (n=2, 2) -> 3 + 2 = 5가지
4+
// n=5 (n=4, 1) , (n=3, 2)
5+
// n=k (n=k-1, 1), (n=k-2, 2)
6+
7+
class Solution {
8+
public int climbStairs(int n) {
9+
int[] cntArray = new int[n + 1];
10+
cntArray[0] = 1;
11+
cntArray[1] = 1;
12+
for (int i = 2; i <= n; ++i) { // 시간 복잡도: O(n)
13+
cntArray[i] = cntArray[i - 1] + cntArray[i - 2];
14+
}
15+
return cntArray[n];
16+
}
17+
}

0 commit comments

Comments
 (0)