思路:
最后一次的状态方程为:
i=nums.size()-1;//i为最后一家
a[i]=max(houseRobber(A)+nums[i], houseRobber(B));
说明:1. houseRobber(A)+nums[i]为抢最后一家的情况,
A为{nums[1]….nums[i-2]}//少了第一家和最后两家。
2. houseRobber(A)+nums[i]为不抢最后一家的情况,
B为{nums[0]…nums[i-1]}//少了开头第一家。
3.houseRobber是打劫房屋1的代码。
代码:
class Solution {
public:
/**
* @param nums: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
long long houseRobber(vector<int> A) {
// write your code here
// int n = A.size();
vector<long long> a(A.size(), 0);
long long temp1, temp2;
if (A.size() == 1) return A[0];
if (A.size() == 2) return max(A[0], A[1]);
a[0] = 0; a[1] = A[1];
for (int i = 2; i<A.size(); ++i){
a[i] = max(a[i - 2] + A[i], a[i - 1]);
}
temp1 = a[A.size() - 1];
a[0] = A[0]; a[1] = 0;
for (int i = 2; i<A.size(); ++i){
a[i] = max(a[i - 2] + A[i], a[i - 1]);
}
temp2 = a[A.size() - 1];
return max(temp1, temp2);
}
int houseRobber2(vector<int>& nums) {
//for (auto c : nums)cout << c << endl;
if (nums.size() == 1)return nums[0];
if (nums.size() == 2)return 0;
if (nums.size() == 3)return nums[1];
int temp1, temp2;
vector<int>::iterator it = nums.end(), it2,it3;
int b = *(--it);
it2=nums.erase(it);
temp1 = houseRobber(nums);
//for (auto c : nums)cout << c << endl;
//不打劫
//cout << temp1 << endl;
nums.erase(--it2);
it = nums.begin();
nums.erase(it);
//for (auto c : nums)cout << c << endl;
temp2 = houseRobber(nums);
//打劫
//cout << temp2 << endl;
temp2 += b;
int ret = max(temp1, temp2);
// for (auto c : nums)cout << c<<endl;
return ret;
}
};
这个错误结果让我发现我通过lintcode的打劫房屋1的代码有误,就打劫房屋1的代码而言,当我的数据为[4,1,2,7,5,3]时,我的输出答案是11,但是正确答案应该是14,打劫房屋1的思路是正确的,但是我在打劫房屋1里有一个误解:两栋相隔房屋之间必有一家被打劫,这是错误的。
修改打劫房屋1的代码后,全部代码成功:
class Solution {
public:
/**
* @param nums: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
long long houseRobber(vector<int> A) {
// write your code here
// int n = A.size();
vector<long long> a(A.size(), 0);
long long temp1, temp2;
if (A.size() == 1) return A[0];
if (A.size() == 2) return max(A[0], A[1]);
/*a[0] = 0; a[1] = A[1];
for (int i = 2; i<A.size(); ++i){
a[i] = max(a[i - 2] + A[i], a[i - 1]);
}
temp1 = a[A.size() - 1];
a[0] = A[0]; a[1] = 0;*/
a[0] = A[0]; a[1] = max(A[0], A[1]);
for (int i = 2; i<A.size(); ++i){
a[i] = max(a[i - 2] + A[i], a[i - 1]);
}
temp2 = a[A.size() - 1];
return temp2;
//return max(temp1, temp2);
}
int houseRobber2(vector<int>& nums) {
//for (auto c : nums)cout << c << endl;
if (nums.size() == 1)return nums[0];
if (nums.size() == 2)return 0;
if (nums.size() == 3)return nums[1];
int temp1, temp2;
vector<int>::iterator it = nums.end(), it2,it3;
int b = *(--it);
it2=nums.erase(it);
temp1 = houseRobber(nums);
//for (auto c : nums)cout << c << endl;
//不打劫
//cout << temp1 << endl;
nums.erase(--it2);
it = nums.begin();
nums.erase(it);
//for (auto c : nums)cout << c << endl;
temp2 = houseRobber(nums);
//打劫
//cout << temp2 << endl;
temp2 += b;
int ret = max(temp1, temp2);
// for (auto c : nums)cout << c<<endl;
return ret;
}
};