[Leetcode] 198. House Robber
2016-04-12 本文已影响0人
gammaliu
- House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Credits:Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
Subscribe to see which companies asked this question
public class Solution {
public int rob(int[] nums) {
int len = nums.length;
if(len == 0) return 0;
int[] doRob = new int[len];
int[] noRob = new int[len];
doRob[0] = nums[0];
noRob[0] = 0;
for(int i = 1; i < len; i++){
doRob[i] = noRob[i-1] + nums[i];
noRob[i] = Math.max(doRob[i-1],noRob[i-1]);
}
return Math.max(doRob[len-1],noRob[len-1]);
}
}