1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution { public int rob(int[] nums) { int n = nums.length; int[][] dp = new int[n+1][2];
for(int i = 1; i <= n; i++){ dp[i][0] = Math.max(dp[i-1][1], dp[i-1][0]); dp[i][1] = dp[i-1][0] + nums[i-1]; }
return Math.max(dp[n][0],dp[n][1]); } }
|