A DP Way to solve it in python


  • 0
    M

    I tried to do it both in DP and recursion, but the recursion method run out of time, so here is my DP solution

       class Solution(object):
            def climbStairs(self, n):
                """
                :type n: int
                :rtype: int
                """
                result = [0 for x in range(n+1)]
                
                for i in range(n+1):
                    if i is 0:
                        result[i] = 0
                    elif i is 1:
                        result[i] = 1
                    elif i is 2:
                        result[i] = 2
                    else:
                        result[i] = result[i-1]+result[i-2]
                
                return result[n]

Log in to reply
 

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