Skip to content

Commit cc5824e

Browse files
authored
Solution Climbing Stairs
1 parent acd7746 commit cc5824e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

climbing-stairs/river20s.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* 풀이: n번째 계단에 도달하는 경우는 다음 두 가지로 나뉜다.
3+
* 1. (n-1)번째 계단에서 한 계단 오르는 경우
4+
* 2. (n-2)번째 계단에서 두 계단 오르는 경우
5+
* 따라서, n개의 계단을 올라가는 방법의 경우의 수 F(n)은
6+
* F(n-1)과 F(n-2)의 합과 같다.
7+
* 시간 복잡도: O(n)
8+
* 공간 복잡도: O(1)
9+
*/
10+
class Solution {
11+
public int climbStairs(int n) {
12+
if(n == 1) return 1;
13+
14+
int step1 = 1;
15+
int step2 = 2;
16+
17+
for (int i = 3; i <= n; i++) {
18+
int temp = step1 + step2;
19+
step1 = step2;
20+
step2 = temp;
21+
}
22+
23+
return step2;
24+
}
25+
}
26+

0 commit comments

Comments
 (0)