Skip to content

Commit 5b8aa27

Browse files
author
이연수
committed
Climbing Stairs
1 parent 9192ae9 commit 5b8aa27

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

climbing-stairs/EcoFriendlyAppleSu.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode_study
2+
3+
/**
4+
* 계단에 올라갈 수 있는 경우의 수 구하는 방법
5+
* 시간 복잡도: O(n)
6+
* -> 주어진 횟수 만큼 반복 진행
7+
* 공간 복잡도: O(k)
8+
* -> 주어진 계단 수 만큼 횟수를 저장할 공간 필요
9+
*/
10+
fun climbStairs(n: Int): Int {
11+
val step = IntArray(n+1)
12+
13+
if (n == 1) {
14+
return 1
15+
}
16+
step[1] = 1
17+
step[2] = 2
18+
19+
for (i in 3 until step.size) {
20+
step[i] = step[i-1] + step[i-2]
21+
}
22+
23+
return step[n]
24+
}

0 commit comments

Comments
 (0)