代码拉取完成,页面将自动刷新
class Solution {
public:
map<bool, map<int, int>> m;
int recursion(const vector<int> nums,
int index = 0, bool head = false) {
// 超出范围
if (index >= nums.size())
return 0;
// 头节点已经被占用,且到达末尾节点
if (index == nums.size() - 1 && head)
return 0;
if (index == nums.size() - 1 && !head)
return nums[index];
// int steal = nums[index] + recursion(nums, index+2, head);
int steal = nums[index];
if (m[head].find(index+2) == m[head].end()) {
int r = recursion(nums, index+2, head);
m[head][index+2] = r;
steal += r;
}
else {
steal += m[head][index + 2];
}
// int keep = recursion(nums, index+1, head);
int keep = 0;
if (m[head].find(index+1) == m[head].end()) {
int k = recursion(nums, index+1, head);
m[head][index+1] = k;
keep = k;
}
keep = m[head][index+1];
return steal > keep ? steal : keep;
}
int rob(vector<int>& nums) {
m[true][0] = nums.front();
int head = nums.front() + recursion(nums, 2, true);
int noHead = recursion(nums, 1, false);
// cout << head << endl;
// cout << noHead << endl;
return head > noHead ? head : noHead;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。