Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leetcode House robber

I was trying House Robber problem(dp problem) on leet code. This solution from one of the user GYX looks simple and elegant.

   int rob(vector<int>& num) {
   int n = num.size();
        if (n==0) return 0;
        vector<int> result(n+1,0);
        result[1] = num[0];
        for (int i=2;i<=n;i++){
            result[i] = max(result[i-1],result[i-2]+num[i-1]);
        }
        return result[n];
   }

But I just could not get my head around the logic. Please help me with the logic and also how to approach problems like this?

like image 881
srip Avatar asked Sep 17 '16 00:09

srip


2 Answers

Basically the answer is f(n) = max( f(n-1), f(n-2) + arr[n] ) and you are asking why.

Let's suppose this array arr = [9,1,7,9] and f(n) is the function.

When the array is only [9], your max f(0) will be arr[0].

When the array is [9,1], your max f(1) is max(arr[0], arr[1]).

When the array is [9,1,7], if you select 7, you can't select 1 therefore f(n-2) + arr[n]. However, if you don't select 7, you max f(2) will be same as f(1) which is f(n-1).

When the array is [9,1,7,9], you need to drop both 1 & 7 and choose 9, 9. f(n) = max( f(n-1), f(n-2)+arr[n] ) equation satisfies this case.

like image 161
aerin Avatar answered Sep 22 '22 14:09

aerin


Suppose I store the amount in kth house in house[k].

Suppose now I store the maximum amount of money possible to loot from the first k houses(and first k only) in max[k].

Now consider no houses, so max[0]=0

Now considering only first house, max[1]=amount in house 1

Now considering first 2 houses,

max[2]={either max[1](implies we chose to loot house 1) or (amount in house 2 + maximum amount that I had looted until the house located 2 places before my current house)}={max(max[1],house[2]+max[0])}

Similarly for first 3 houses, max[3]=max(max[2],house[3]+max[1])

Observing this trend, it can be formulated that max[k]=max(max[k-1],house[k]+max[k-2]). This value is calculated till in the end when there are no more houses, we get the maximum amount that can be looted from these first n houses.

DP problems strike the head only when you have had some practice and familiarity before, and this always helps.

like image 44
brijs Avatar answered Sep 20 '22 14:09

brijs