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

287人阅读 评论(0)

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

}

};
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：19089次
• 积分：1524
• 等级：
• 排名：千里之外
• 原创：140篇
• 转载：4篇
• 译文：0篇
• 评论：2条
文章分类
阅读排行
评论排行
最新评论