# LeetCode – House Robber (Java)

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.

**Java Solution 1 - Dynamic Programming**

The key is to find the relation `dp[i] = Math.max(dp[i-1], dp[i-2]+nums[i])`

.

public int rob(int[] nums) { if(nums==null||nums.length==0) return 0; if(nums.length==1) return nums[0]; int[] dp = new int[nums.length]; dp[0]=nums[0]; dp[1]=Math.max(nums[0], nums[1]); for(int i=2; i<nums.length; i++){ dp[i] = Math.max(dp[i-2]+nums[i], dp[i-1]); } return dp[nums.length-1]; } |

**Java Solution 2**

We can use two variables, even and odd, to track the maximum value so far as iterating the array. You can use the following example to walk through the code.

50 1 1 50

public int rob(int[] num) { if(num==null || num.length == 0) return 0; int even = 0; int odd = 0; for (int i = 0; i < num.length; i++) { if (i % 2 == 0) { even += num[i]; even = even > odd ? even : odd; } else { odd += num[i]; odd = even > odd ? even : odd; } } return even > odd ? even : odd; } |

<pre><code> String foo = "bar"; </code></pre>

## Leave a comment

counter example showing that your odd-even solution may not work: [50, 1, 1, 100]

You are right!

When the robber is in the ith house he has to decide if he is going to steal ith or i+1th house:

So, if N is the number of houses and v_i the value in the ith house we can define recursively the maximum value as:

maxvalor(i) = { 0 if i >= N;

max(v_i + max(i+2) , max(i+1))

}

For each i there is two calls of max(i) in this definition, so a plain recursive implementation will give you an exponential solution. So you need to make a memoization of results.

It will work

In the last there will be a comparison between 51 and 150 and 150 will be picked. Looks like it works.

how to print indexes of robbed houses??

store the index of which ever is maximum out of dp[i-2]+num[i] , dp[i-1] in an array houses. Now backtrack from houses[n].