声明:原题目转载自LeetCode,解答部分为原创
Problem :
Note: This is an extension of House Robber. [LeetCode] 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.
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size() == 0)
return 0;
if(nums.size() == 1)
return nums[0];
if(nums.size() == 2)
return max(nums[0], nums[1]);
if(nums.size() == 3)
return max( max(nums[0], nums[1]), nums[2]);
vector<int> temp_1(nums.size() - 1);
vector<int> temp_2(nums.size() - 1);
temp_1.assign(nums.begin(), nums.end() - 1);
temp_2.assign(nums.begin() + 1, nums.end());
int result_1 = find_max_money(temp_1);
int result_2 = find_max_money(temp_2);
return max(result_1, result_2);
}
private:
int find_max_money(vector<int> &array)
{
vector<int> max_money(array.size());
max_money[0] = array[0];
max_money[1] = array[1];
max_money[2] = max(array[0] + array[2], array[1]);
int result = max( max(max_money[0], max_money[1]), max_money[2] );
for(int i = 3; i < array.size(); i ++)
{
max_money[i] = array[i] + max(max_money[i - 2], max_money[i - 3]);
result = max(result, max_money[i]);
}
return result;
}
};