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; } |
Java Solution 3 - Dynamic Programming with Memorization
public int rob(int[] nums) { if(nums.length==0){ return 0; } int[] mem = new int[nums.length+1]; Arrays.fill(mem, -1); mem[0] = 0; return helper(nums.length, mem, nums); } private int helper(int size, int[] mem, int[] nums){ if(size <1){ return 0; } if(mem[size]!=-1){ return mem[size]; } //two cases int firstSelected = helper(size-2, mem, nums) + nums[nums.length -size]; int firstUnselected = helper(size-1, mem, nums); return mem[size] = Math.max(firstSelected, firstUnselected); } |
<pre><code> String foo = "bar"; </code></pre>
-
Alyaa Mansor
-
Ajay V
-
Neha Rao
-
Internet Hero
-
OJ
-
rafaelolg
-
Mario