Python DP time O(n) with memorization


  • 0
    A
    class Solution(object):
        def climbStairs(self, n):
            """
            :type n: int
            :rtype: int
            """
            mem = [0]*n
            mem[:2] = [0,1,2]
            
            return self.steps(n,mem)
                
        def steps(self,n,mem):
        
            if n < 3:
                return mem[n]
            else:
                if mem[n] == 0:
                    mem[n] = self.steps(n-1,mem) + self.steps(n-2,mem)
                return mem[n]
    
    
    

Log in to reply
 

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