forked from Nandinig24/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path213
49 lines (35 loc) · 1008 Bytes
/
213
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
class Solution {
public:
int f(int i, int end, vector<int>& nums, vector<int>& dp) {
if ( i == end) {
return nums[i];
}
if (i < end) {
return 0;
}
if (dp[i] != -1) {
return dp[i];
}
int take = 0;
int notake = 0;
// Include the current house and exclude the adjacent one
take = f(i - 2, end, nums, dp) + nums[i];
// Exclude the current house
notake = f(i - 1, end, nums, dp);
return dp[i] = max(take, notake);
}
int rob(vector<int>& nums) {
int n = nums.size();
if (n == 1) {
return nums[0];
}
// Scenario 1: Include the first house and exclude the last house
vector<int> dp1(n, -1);
int ans1 = f(n - 2, 0, nums, dp1);
// Scenario 2: Include the last house and exclude the first house
vector<int> dp2(n, -1);
int ans2 = f(n - 1, 1, nums, dp2);
// Take the maximum from the two scenarios
return max(ans1, ans2);
}
};