Skip to content

Commit 53d4895

Browse files
5ooolabuladong
authored andcommitted
Update:抢房子 C++版本
1 parent 268ce74 commit 53d4895

File tree

1 file changed

+114
-1
lines changed

1 file changed

+114
-1
lines changed

动态规划系列/抢房子.md

+114-1
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,122 @@ int[] dp(TreeNode root) {
230230

231231
![labuladong](../pictures/labuladong.jpg)
232232

233+
[5ooo](https://github.com/5ooo) 提供 House Robber I C++ 解法代码:
234+
235+
```c++
236+
class Solution {
237+
public:
238+
int rob(vector<int>& nums) {
239+
int dp_i = 0; //第i间房子最多能抢的钱
240+
int dp_i_1 = 0; //第i+1间房子最多能抢的钱
241+
int dp_i_2 = 0; //第i+2间房子最多能抢的钱
242+
243+
//从最后一间房子开始,往前移动
244+
for (int i = nums.size() - 1; i >= 0; i--) {
245+
dp_i = max(dp_i_1, nums[i] + dp_i_2);
246+
dp_i_2 = dp_i_1;
247+
dp_i_1 = dp_i;
248+
}
249+
250+
return dp_i;
251+
}
252+
};
253+
```
254+
255+
[5ooo](https://github.com/5ooo) 提供 House Robber II C++ 解法代码:
256+
257+
```c++
258+
class Solution {
259+
public:
260+
int rob(vector<int>& nums) {
261+
if (nums.size() == 1)
262+
return nums[0];
263+
264+
return max(robRange(nums, 0, nums.size() - 2),
265+
robRange(nums, 1, nums.size() - 1));
266+
}
267+
268+
int robRange(vector<int>& nums, int start, int end) {
269+
int dp_i = 0; //第i间房子最多能抢的钱
270+
int dp_i_1 = 0; //第i+1间房子最多能抢的钱
271+
int dp_i_2 = 0; //第i+2间房子最多能抢的钱
272+
273+
for (int i = end; i >= start; i--) {
274+
dp_i = max(dp_i_1, nums[i] + dp_i_2);
275+
dp_i_2 = dp_i_1;
276+
dp_i_1 = dp_i;
277+
}
278+
279+
return dp_i;
280+
}
281+
};
282+
```
283+
284+
[5ooo](https://github.com/5ooo) 提供 House Robber III C++ 解法代码:
285+
286+
```c++
287+
class Solution {
288+
public:
289+
int rob(TreeNode* root) {
290+
if (root == nullptr)
291+
return 0;
292+
293+
// 利用备忘录消除重叠子问题
294+
if (memo.find(root) != memo.end())
295+
return memo[root];
296+
297+
// 抢,然后去下下家
298+
int do_it = root->val +
299+
(root->left == nullptr ?
300+
0 : rob(root->left->left) + rob(root->left->right)) +
301+
(root->right == nullptr ?
302+
0 : rob(root->right->left) + rob(root->right->right));
303+
304+
// 不抢,然后去下家
305+
int not_do = rob(root->left) + rob(root->right);
306+
307+
int ret = max(do_it, not_do);
308+
memo[root] = ret;
309+
310+
return ret;
311+
}
312+
private:
313+
unordered_map<TreeNode *, int > memo;
314+
};
315+
```
316+
317+
```c++
318+
class Solution {
319+
public:
320+
int rob(TreeNode* root) {
321+
//ret[0]表示不抢root,获取的最大钱数
322+
//ret[1]表示抢root,获取的最大钱数
323+
vector<int> ret = dp(root);
324+
return max(ret[0], ret[1]);
325+
}
326+
327+
328+
vector<int> dp(TreeNode* root) {
329+
if (root == nullptr)
330+
return {0, 0};
331+
332+
vector<int> left = dp(root->left);
333+
vector<int> right = dp(root->right);
334+
335+
//抢当前的,则接下来不能抢
336+
int rob = root->val + left[0] + right[0];
337+
338+
//不抢当前的,接下来可抢可不抢,取收益大的
339+
int not_rob = max(left[0], left[1]) + max(right[0], right[1]);
340+
341+
return {not_rob, rob};
342+
}
343+
};
344+
```
233345

234346
[上一篇:团灭 LeetCode 股票买卖问题](../动态规划系列/团灭股票问题.md)
235347

236348
[下一篇:动态规划之四键键盘](../动态规划系列/动态规划之四键键盘.md)
237349

238-
[目录](../README.md#目录)
350+
[目录](../README.md#目录)
351+

0 commit comments

Comments
 (0)