标签:etc type class 思路 int mic limit bsp limited
mycode
思路:
a:1 2 3 4 5 6 7 8 9
f(9) =max( f(7) + a9 ,f(8)) 前一步、前两步
至于前三步 f(9) = f(6)+ a9,但其实f(7)在求值的时候按照上面的公式一定是比f(7)大于等于的,所以f(6)+a9总是小于等于上面的递推式的
至于前四步更不用考虑了,因为前两步已经考虑了前四步
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]))
leetcode-easy-dynamic-198 House Robber
标签:etc type class 思路 int mic limit bsp limited
原文地址:https://www.cnblogs.com/rosyYY/p/11003499.html