Problem description:

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:

Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.

Solution:

This problem is pretty similar to 198. House Robber. First, let’s look at the example [1,2,3,1]. If you choose to rob nums[0], then you must not rob nums[size-1], and your right boundary would be nums[size-2]. On the other hand, if you choose to rob nums[size-1], then you must not rob nums[0], that means you could only start robbing from nums[1].

  • rob nums[0]: 0...n-2
  • not rob nums[0]: 1...n-1

Therefore, we can either separate the array into two different array or we can do the search twice.

In previous question, 198. House Robber, we use a simple for loop to do the dynamic programming. We can further extract the for loop as a helper function, which takes left and right boundary as input.

If you don’t want to build another function, you can use for loop twice as well, like solution 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def rob(self, nums: List[int]) -> int:
# since it's a circle, if rob nums[0], then can't rob nums[-1]
# if rob nums[1], then can't rob nums[0]
# two variables to keep rob, not_rob
def helper(nums, i, j):
# copy array like nums[:-2] takes O(n), so passing index in here
rob, not_rob = 0, 0
for x in range(i, j+1):
# the rob, not_rob is from previous iteration
# if decide not to rob in x, then max is from max(rob, not_rob)
rob, not_rob = not_rob+nums[x], max(rob, not_rob)
return max(rob, not_rob)
if not nums:
return 0
elif len(nums) == 1:
return nums[0]
else:
rob_0 = helper(nums, 0, len(nums)-2)
rob_1 = helper(nums, 1, len(nums)-1)
return max(rob_0, rob_1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int rob(vector<int>& nums) {
int n= nums.size();
if(n<2) return n? nums[0]: 0;

return max(help(nums, 0, n-2), help(nums, 1, n-1));
}

int help(vector<int>& nums, int left, int right){
int cur= 0, pre= 0;
for(int i= left; i<= right; i++){
int tmp = max(nums[i]+ pre, cur);
pre= cur;
cur = tmp;
}
return cur;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size() == 0)
return 0;
if(nums.size() == 1)
return nums[0];

int pre1 = 0, cur1 = 0;
for(int i = 0; i < nums.size() - 1; ++ i)
{
int temp = pre1;
pre1 = cur1;
cur1 = max(temp + nums[i], pre1);
}

int pre2 = 0, cur2 = 0;
for(int i = 1; i < nums.size(); ++ i)
{
int temp = pre2;
pre2 = cur2;
cur2 = max(temp + nums[i], pre2);
}

return max(cur1, cur2);
}
};

Improvement

Slicing a list is actually $O(n)$ space in Python. In our case, nums[1:], nums[:-1] create copies, to avoid this, we can pass indices into the simple_rob function instead of sliced lists. Below is the less elegant, but true $O(1)$ space solution:

reference:
https://goo.gl/NYAGsU
https://goo.gl/RA1cb9