http://www.lintcode.com/zh-cn/problem/house-robber/
假设你是一个专业的窃贼,准备沿着一条街打劫房屋。每个房子都存放着特定金额的钱。你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动报警。
给定一个非负整数列表,表示每个房子中存放的钱, 算一算,如果今晚去打劫,你最多可以得到多少钱 在不触动报警装置的情况下。
样例
给定 [3, 8, 4]
, 返回 8
.
public class Solution {
/*
* @param A: An array of non-negative integers
* @return: The maximum amount of money you can rob tonight
*/
public long houseRobber(int[] A) {
// write your code here
if(A==null) return 0l;
int n=A.length;
if(n==0) return 0l;
else if(n==1) return A[0];
long[] dp = new long[n];//走过第i个房子得到的最大收益
dp[0] = A[0];
dp[1] = Math.max(A[0],A[1]);
for(int i=2; i<n; i++){
dp[i] = Math.max(dp[i-2]+A[i], dp[i-1]);
}
return dp[n-1];
}
};