-
Notifications
You must be signed in to change notification settings - Fork 110
/
198. House Robber.cpp
61 lines (52 loc) · 1.63 KB
/
198. 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
//recursion
//TLE
/**
class Solution {
public:
int robStart(vector<int>& nums, int s){
if(s >= nums.size()) return 0;
return nums[s] + max(robStart(nums, s+2), robStart(nums, s+3));
}
int rob(vector<int>& nums) {
return max(robStart(nums, 0), robStart(nums, 1));
}
};
**/
//DP, consider 2nd and 3rd next houses
//Runtime: 4 ms, faster than 100.00% of C++ online submissions for House Robber.
//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for House Robber.
class Solution {
public:
vector<int> money;
int rob(vector<int>& nums) {
if(nums.size() == 0) return 0;
else if(nums.size() == 1) return nums[0];
int N = nums.size();
money = nums;
//so that we can access money[N]
money.push_back(0);
for(int i = N-3; i >= 0; i--){
money[i] += max(money[i+2], money[i+3]);
}
return max(money[0], money[1]);
}
};
//DP, consider next 2 houses
//Runtime: 0 ms, faster than 100.00% of C++ online submissions for House Robber.
//Memory Usage: 6.4 MB, less than 100.00% of C++ online submissions for House Robber.
class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
if(n == 0) return 0;
if(n == 1) return nums[0];
vector<int> dp(n);
dp[0] = nums[0];
dp[1] = max(nums[0], nums[1]);
for(int i = 2; i < n; i++){
//if we rob the ith house, we can add dp[i-2] because there is no alear
dp[i] = max(dp[i-1], dp[i-2]+nums[i]);
}
return dp[n-1];
}
};