leetcod 213 House Robber II

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
无输入输出样例

解题思路:
动态规划方程:money[i] = max(money[i-2]+nums[i],money[i-1])
加上几个临界条件
然后把一个圈分解为两条线,从1到n 和从0到n-1

class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        length = len(nums)
        if length == 0 :
            return 0
        elif length == 1 :
            return nums[0]
        elif length == 2 :
            return max(nums[1],nums[0])
        else :
            ll = [0 for i in range(0,length)]
            ll[0] = nums[0]
            ll[1] = max(nums[0],nums[1])
            a = 0
            for i in range(2,length-1):
                ll[i] = max(ll[i-2] + nums[i],ll[i-1])
            a = max(ll)
            l = [0 for i in range(0,length)]
            l[1] = nums[1]
            l[2] = max(nums[1],nums[2])
            b = 0
            for i in range(2,length):
                l[i] = max(l[i-2]+nums[i],l[i-1])
            b = max(l)
            return max(a,b)

a = Solution()
b = [0,0,0]
c = a.rob(b)
print(c)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值