forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClimbingStairs.kt
42 lines (35 loc) · 1007 Bytes
/
ClimbingStairs.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* You are climbing a staircase. It takes n steps to reach the top.
* Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
* [Source](https://leetcode.com/problems/climbing-stairs/)
*/
@UseCommentAsDocumentation
private fun climbStairs(n: Int): Int {
val memo = Array<Int?>(n + 1) { null }
return climbStairs(n, memo)
}
private fun climbStairs(n: Int, memo: Array<Int?>): Int {
if (n <= 0) return 0
if (n == 1) return 1
if (n == 2) return 2
val memoized = memo[n]
if (memoized != null) {
return memoized
}
val value = climbStairs(n - 1, memo) + climbStairs(n - 2, memo)
memo[n] = value
return value
}
fun main() {
climbStairs(44)
// 1 step + 1 step
// 2 steps
climbStairs(n = 2) shouldBe 2
// 1 step +1 step +1 step
// 1 step +2 steps
// 2 steps +1 step
climbStairs(n = 3) shouldBe 3
}