Python Dynamic Programming using hash table easy to follow


  • 0
    Y
    memory = {}
    memory[0] = 0
    memory[1] = 1
    memory[2] = 2
    class Solution(object):
        
        def climbStairs(self, n):
            if n in memory:
                return memory[n]
                
            first = self.climbStairs(n-1)
            memory[n-1] = first
            second = self.climbStairs(n-2)
            memory[n-2] = second
            
            return first + second 
    

Log in to reply
 

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