Problem description:

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.

Example 1:

1
2
3
4
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.

Example 2:

1
2
3
4
Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.

Solution:

DP solution:
For dp solution, we need to know what is the recursion function in it. As you can see in the example, once you pick ith house to rob, you must not came from i-1th house. Therefore, let’s try to think it in this way:
Let us look at the case n = 1, clearly f(1) = A1.
Now, let us look at n = 2, which f(2) = max(A1, A2).
For n = 3, you have basically the following two options:

  1. Rob the third house, and add its amount to the first house’s amount.
  2. Do not rob the third house, and stick with the maximum amount of the first two houses.
    Clearly, you would want to choose the larger of the two options at each step.
    Therefore, we could summarize the formula as following:
    f(k) = max(f(k – 2) + Ak, f(k – 1))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
def rob(self, nums: List[int]) -> int:
dp = [0]*len(nums)
if not nums:
return 0
if len(nums) <= 2:
return max(nums)
dp = [0]*len(nums)
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])

for i in range(2, len(nums)):
dp[i] = max(dp[i-2]+ nums[i], dp[i-1])
return dp[-1]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//DP
class Solution {
public:
int rob(vector<int>& nums) {

int n=nums.size();

//Edge Cases
if(n==0) return 0;
if(n<=2) return *max_element(nums.begin(),nums.end());


int dp[n];
dp[0]=nums[0]; //Represent starting from the odd house
dp[1]=max(dp[0], nums[1]);//Represent max loot at the even house

for(int i=2;i<nums.size();i++)
{
dp[i] = max(dp[i-1],dp[i-2]+nums[i]);
}

return dp[n-1];
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//odd, even

class Solution {
public:
int rob(vector<int>& nums) {

//Edge Cases
if(nums.size()==0) return 0;
if(nums.size()<=2) return *max_element(nums.begin(),nums.end());

int odd,even;
odd=nums[0]; //Represent starting from the odd house
even=nums[1];//Represent starting from the even house

for(int i=2;i<nums.size();i++){

if((i+1)%2==0) //Even
{
if(odd<even) odd=even; //If robbing even path gives more value then put odd as even
even+=nums[i];
}
else //Odd
{
if(even<odd) even=odd;
odd+=nums[i];
}

}

return max(even,odd);
}
};