Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 697 Bytes

leetcode_70.md

File metadata and controls

31 lines (23 loc) · 697 Bytes

LeetCode Problems

70. Climbing Stairs

class Solution {
    fun climbStairs(n: Int): Int {
        if (n < 3) return n
        var dp = IntArray(n).apply {
            // way to reach 1 stair : 1, 2 stair : 2 ( 1+1, 2 ) 
            set(0, 1)
            set(1, 2)
        }
        // can reach each stair by 2 ways ( dp[i-2] + dp[i-1] )
        // 1) way for dp[i-2] + jump 2steps
        // 2) way for dp[i-1] + jump 1steps
        for (i in 2..n-1) dp[i] = dp[i-1] + dp[i-2]
        return dp[n-1]
    }
}