Leetcode 213 House Robber II
Note: This is an extension of House Robber.
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.
对于删去头和删去尾的两个数组全做一遍之前的dp,因为头尾不能被同时选到,所以两个结果之中必定有最大值。
class Solution(object): def rob(self, nums): if len(nums) == 1: return nums[0] return max(self.rob1(nums[1:]),self.rob1(nums[:-1])) def rob1(self, nums): m = [0,0] + nums for i in range(2,len(m)): m[i] = max(m[i-1],m[i-2]+m[i]) return m[-1]