-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path213.打家劫舍-ii.cpp
43 lines (40 loc) · 881 Bytes
/
213.打家劫舍-ii.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
/*
* @lc app=leetcode.cn id=213 lang=cpp
*
* [213] 打家劫舍 II
*/
#include <vector>
using std::max;
using std::vector;
// @lc code=start
class Solution
{
public:
int rob(vector<int> &nums)
{
int sz = nums.size();
if (sz == 1)
return nums[0];
int res1 = rob_helper(nums, 0, nums.size() - 1);
int res2 = rob_helper(nums, 1, nums.size() - 1);
return max(res1, res2);
}
int rob_helper(vector<int> &nums, int beg, int n)
{
int a0 = nums[beg];
if (n == 1)
return a0;
int a1 = max(a0, nums[beg + 1]);
if (n == 2)
return a1;
int temp;
for (int i = 0; i < (int)(n - 2); i++)
{
temp = max(a0 + nums[beg + 2 + i], a1);
a0 = a1;
a1 = temp;
}
return a1;
}
};
// @lc code=end