题目:
分析:不能偷相邻的两个房屋因为会报警,那么当前房屋的偷窃最高金额=子问题1(两个房屋前的最高金额+当前房屋金额)和子问题2(前一个房屋的最高金额)的最优解,谁金额较大取谁
代码:
class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int before1 = 0;
int before2 = 0;
for(int i = 0; i < nums.length; i++){
int current_max = Math.max(before2 + nums[i], before1);
before2 = before1;
before1 = current_max;
}
return before1;
}
}