-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0213.cpp
47 lines (41 loc) · 1018 Bytes
/
0213.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
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
int rob(vector<int>& nums)
{
int m = nums.size();
if (m == 0)
return 0;
if (m == 1)
return nums[0];
int result1 = robRange(nums, 0, m - 2);
int result2 = robRange(nums, 1, m - 1);
return max(result1, result2);
}
int robRange(vector<int>& nums, int start, int end)
{
if (start == end)
return nums[start];
int m = nums.size();
vector<int> dp(m + 1, 0);
//注意这儿,初始化错误会导致错误的结果
dp[start] = 0, dp[start + 1] = nums[start];
for (int j = start + 2; j <= end + 1; j++)
{
dp[j] = max(dp[j - 1], dp[j - 2] + nums[j - 1]);
cout << dp[j] << " ";
}
cout << endl;
return dp[end + 1];
}
};
int main()
{
vector<int> nums = { 1,1,3,6,7,10,7,1,8,5,9,1,4,4,3 };
Solution S;
S.rob(nums);
return 0;
}