-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0198.cpp
More file actions
31 lines (28 loc) · 823 Bytes
/
0198.cpp
File metadata and controls
31 lines (28 loc) · 823 Bytes
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
class Solution {
public:
int rob(vector<int> &nums) { return func1(nums); }
/*
* basic dp:
* R(n) means MUST rob the n-th house and get most money,
* from rob house 0 to n.
* M(n) means get most money, So the fomule:
* R(n) = nums[n] + M(n-2); (if rob house n then must not rob house n-1)
* M(n) = Max { M(n-1), R(n) }; (1. not rob house n or 2. rob house n)
*/
int func1(vector<int> &nums) {
if (nums.size() <= 0)
return 0;
int n = nums.size();
vector<int> maxVec(n);
vector<int> robVec(n);
robVec[0] = nums[0];
robVec[1] = nums[1];
maxVec[0] = nums[0];
maxVec[1] = max(nums[0], nums[1]);
for (int i = 2; i < n; i++) {
robVec[i] = nums[i] + maxVec[i - 2];
maxVec[i] = max(maxVec[i - 1], robVec[i]);
}
return maxVec[n - 1];
}
};