-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhouse_robber.cpp
72 lines (57 loc) · 1.28 KB
/
house_robber.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
given n and arr
return the maximum sum of the subsequence with the constraint that no two elements are adjacent in the given array/list.
ip: 3
1 2 4
op: 5 (1+4)
ip: 4
2 1 4 9
op: 11 (9+2)
https://leetcode.com/problems/house-robber/
*/
#include <iostream>
#include <vector>
using namespace std;
// recursive sol:
class RecursiveSolution
{
public:
int f(int idx, vector<int> &nums)
{
if (idx == 0)
return nums[idx];
if (idx < 0)
return 0;
int pickup = nums[idx] + f(idx - 2, nums);
int notPickup = 0 + f(idx - 1, nums);
return max(pickup, notPickup);
}
int rob(vector<int> &nums)
{
int n = nums.size();
return f(n - 1, nums);
}
};
// memoization
class MemoSolution
{
public:
int f(int idx, vector<int> &nums, vector<int> &dp)
{
if (idx == 0)
return nums[idx];
if (idx < 0)
return 0;
if (dp[idx] != -1)
return dp[idx];
int pickup = nums[idx] + f(idx - 2, nums, dp);
int notPickup = 0 + f(idx - 1, nums, dp);
return dp[idx] = max(pickup, notPickup);
}
int rob(vector<int> &nums)
{
int n = nums.size();
vector<int> dp(n, -1);
return f(n - 1, nums, dp);
}
};