Show my depth first search solution, python


  • 0
    C
    class Solution:
    # @param num, a list of integer
    # @return an integer
    def rob(self, num):
        self.num = [0, 0] + num 
        self.total = [None] * (len(self.num) + 3)
        return self.dfs(0)
    
    def dfs(self, index):
        if self.total[index] != None:
            return self.total[index]
        if index >= len(self.num):
            return 0
        left = self.dfs(index+2)
        right = self.dfs(index+3)                                                       
        self.total[index] = self.num[index]
        self.total[index] += left if left >= right else right
        return self.total[index]

Log in to reply
 

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