-
Notifications
You must be signed in to change notification settings - Fork 0
/
213.打家劫舍-ii.cpp
45 lines (36 loc) · 1.13 KB
/
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
44
/*
* @lc app=leetcode.cn id=213 lang=cpp
*
* [213] 打家劫舍 II
*/
// @lc code=start
#include<vector>
using namespace std;
class Solution {
public:
int rob_easy(vector<int>& nums, int start, int end) {
int len = end - start;
if(len == 0) return 0;
if(len == 1) return nums[start];
vector<int> dp(len, 0);
dp[0] = nums[start];
dp[1] = max(nums[start], nums[start + 1]);
for(int j = 2; j < len; j++) // 遍历容量
{
dp[j] = max(dp[j - 2] + nums[j + start], dp[j - 1]);
}
return dp[len - 1];
}
int rob(vector<int>& nums) {
if(nums.size() == 0) return 0;
if(nums.size() == 1) return nums[0];
if(nums.size() == 2) return max(nums[0], nums[1]);
// rob_zero : 偷第0个,不能偷第1个和最后一个
// rob_no_zero : 不偷第一个,后边的都可以偷
// [start, end) : 有 end - start 个元素
int rob_zero = nums[0] + rob_easy(nums, 2, nums.size() - 1);
int rob_no_zero = rob_easy(nums, 1, nums.size());
return max(rob_zero, rob_no_zero);
}
};
// @lc code=end