-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhouseRobberII.cpp
More file actions
38 lines (35 loc) · 1.07 KB
/
houseRobberII.cpp
File metadata and controls
38 lines (35 loc) · 1.07 KB
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
/*
Dynamic Programming
Hint:Since House[1] and House[n] are adjacent, they cannot be robbed together.
Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n],
depending on which choice offers more money.
Now the problem has degenerated to the House Robber, which is already been solved.
*/
class Solution {
public:
int orginal_rob(vector<int> &money, int start, int end) {
int n2=0;
int n1=0;
for (int i=start; i<end; i++){
int current = max(n1, n2 + money[i]);
n2 = n1;
n1 = current;
}
return n1;
}
int rob(vector<int>& nums) {
int n = nums.size();
switch (n) {
case 0:
return 0;
case 1:
return nums[0];
case 2:
return max(nums[0], nums[1]);
default:
int m1 = orginal_rob(nums, 0, n-1);
int m2 = orginal_rob(nums, 1, n);
return max(m1, m2);
}
}
};