leetcode-easy-dynamic-198 House Robber-NO

mycode

Ideas:

a:1 2 3 4 5 6 7 8 9 

f (9) = max (f (7) + a9, f (8)) in the previous step, the first two steps

As to the first three steps f (9) = f (6) + a9, but in fact f (7) in the evaluation of the time according to the above formula must be the ratio f (7) or greater, so that f (6) + a9 Total It is less than or equal to the above recurrence formula

As for the first four steps not to consider, because the first two steps have been considered the first four steps

 

time limited

class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 0
        elif len(nums) == 1:
            return nums[0]
        elif len(nums) == 2:
            return max(nums)
        return max(self.rob(nums[:-2])+nums[-1],self.rob(nums[:-1]))

 

Guess you like

Origin www.cnblogs.com/rosyYY/p/11003499.html