You are a professional thief to steal street plan of the house. Each room are in possession of some cash, the only constraints affecting steal your neighbor's house is equipped with anti-theft system communicate with each other, if two adjacent houses on the same night, thieves broke into the system will automatically alarm .
Given a representative from each non-negative integer array of Housing storage amount calculated in case you do not touch the alarm device, you can steal the maximum amount to.
Example 1:
Input: [1,2,3,1]
Output: 4
Explanation: Housing theft No. 1 (amount = 1), then the theft Housing 3 (amount = 3).
The maximum amount to theft = 3 + 1 = 4.
Example 2:
Input: [2,7,9,3,1]
Output: 12
Explanation: Housing theft No. 1 (amount = 2), 3 theft Housing (amount = 9), followed by theft No. 5 Housing (amount = 1).
The maximum amount to theft = 2 + 9 + 1 = 12.
class Solution {
public int rob(int[] nums) {
int length=nums.length;
int[] dp=new int[length];
if (length==0) return 0;
else if (length==1) return nums[0];
else{
dp[0]=nums[0];
dp[1]=Math.max(nums[0],nums[1]);
for(int i=2;i<length;i++){
dp[i]=Math.max(dp[i-1],dp[i-2]+nums[i]);
}
}
return dp[length-1];
}
}