Climb the stair in a DP and Python way - O(n) 42ms


  • 1
    G
    class Solution:
        # @param {integer} n
        # @return {integer}
        def climbStairs(self, n):
            cache = {}
            cache[1], cache[2] = 1, 2
    
            for i in range(3, n + 1):
                cache[i] = cache[i - 1] + cache[i - 2]
    
            return cache[n]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.