leetcode -- House Robber -- 简单重点

https://leetcode.com/problems/house-robber/

简单的DP例题,还有一种改进空间复杂度的办法
http://bookshadow.com/weblog/2015/04/01/leetcode-house-robber/

my code

class Solution(object):
    def rob(self, nums):
        """ :type nums: List[int] :rtype: int """
        if len(nums) == 0:
            return 0
        d = [0] * (len(nums) + 1)
        d[1] = nums[0]
        for i in xrange(2, len(nums) + 1):
            d[i] = max(d[i - 2] + nums[i - 1], d[i - 1])
        return d[len(nums)]

你可能感兴趣的:(LeetCode)