# LintCode(M)打劫房屋（动态规划）

class Solution {
public:
/**
* @param A: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
long long houseRobber(vector<int> A) {
// int n = A.size();
vector<long long> a(A.size(),0);
if (A[1]>A[0] ) { a[0] = 0; a[1] = A[1]; }
else { a[0] = A[0]; a[1] = 0; }
if (A.size() == 1) return A[0];
if (A.size() == 2) return max(A[0],A[1]);
for (int i = 2; i<A.size(); ++i){
a[i] = max(a[i - 2] + A[i], a[i - 1]);
}
return a[A.size() - 1];
}
};

e

 long long houseRobber(vector<int> A) {
// int n = A.size();
vector<long long> a(A.size(),0);
int 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);
}

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) {
// 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);

}

};

• 本文已收录于以下专栏：

举报原因： 您举报文章：LintCode(M)打劫房屋（动态规划） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)