题目描述
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
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.
Example 1:
1 | Input: [2,3,2] |
Example 2:
1 | Input: [1,2,3,1] |
本题是之前House Robber的升级版。在这里,所有的房子形成一个环形,言下之意就是第一个房子和最后一个房子不能都偷。
假设有n个房子,他们的财产为house[0], house[1], …, house[n-1],要保证house[0]和house[n-1]都不被偷,并且求出能偷的最大值。如果不偷house[n-1],那么就转化成了House Robber中的house[0], …, house[n-2]的情形;同理,如果不偷house[0],那么就转化成了求house[1], …, house[n-1]的情形。因此,本题就是求House Robber中的house[0], …, house[n-2]和house[1], …, house[n-1]的最大值。
代码实现
1 | class Solution: |