Skip to content

Commit 0507451

Browse files
committed
337 House Robber III
1 parent 3936f11 commit 0507451

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
+ [237 Delete Node in a Linked List(O(1)删除单链表节点)](algorithms/DeleteNodeinaLinkedList)
139139
+ [238 Product of Array Except Self(数组操作)](algorithms/ProductofArrayExceptSelf)
140140
+ [242 Valid Anagram(hash表,判断两个字符串出现的字符和次数均相同)](algorithms/ValidAnagram)
141+
+ [337 House Robber III(DFS,递归)](algorithms/HouseRobberIII)
141142
+ [338 Counting Bits(二进制1的个数,迭代法)](algorithms/CountingBits)
142143

143144
## Database

algorithms/HouseRobberIII/READMD.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## House Robber III
2+
3+
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
4+
5+
Determine the maximum amount of money the thief can rob tonight without alerting the police.
6+
7+
Example 1:
8+
9+
```
10+
3
11+
/ \
12+
2 3
13+
\ \
14+
3 1
15+
```
16+
Maximum amount of money the thief can `rob = 3 + 3 + 1 = 7`.
17+
18+
Example 2:
19+
20+
```
21+
3
22+
/ \
23+
4 5
24+
/ \ \
25+
1 3 1
26+
```
27+
Maximum amount of money the thief can `rob = 4 + 5 = 9`.
28+
29+
## Solution
30+
31+
最开始想到直接层次遍历,获取每一层的值,比如Example 2为`[3,9,5]`,然后转化为[House Robber](../HouseRobber)求解。时间复杂度时O(n),空间复杂度是O(n)。
32+
33+
看提示说使用DFS,一个节点要么选择要么不选择,设当前节点为p,yes为选择p节点的值,no为不选择p节点的值,f(p)为p节点的值,则:
34+
35+
* 若p为null,则yes = 0, no = 0;
36+
* 若选择p,则不能选择p的孩子节点,则`p->yes = f(p->left->no) + f(p->right->no) + p->val`;
37+
* 若不选择p节点,则结果为孩子节点的最大值,即`p->no = max(f(p->left->yes), f(p->left->no)) + max(f(p->right->yes), f(p->right->no))`
38+
39+
```cpp
40+
void dfs(TreeNode *root, int *yes, int *no) {
41+
if (root == nullptr) {
42+
*yes = 0;
43+
*no = 0;
44+
return;
45+
}
46+
int left_yes, left_no, right_yes, right_no;
47+
dfs(root->left, &left_yes, &left_no);
48+
dfs(root->right, &right_yes, &right_no);
49+
*yes = left_no + right_no + root->val;
50+
*no = max(left_yes, left_no) + max(right_yes, right_no);
51+
}
52+
```
53+
54+
最后返回`max(root->yes, root->no)`即可。
55+
56+
```cpp
57+
int rob(TreeNode *root) {
58+
int yes = 0, no = 0;
59+
dfs(root, &yes, &no);
60+
return max(yes, no);
61+
}
62+
```
63+
64+
## 参考
65+
66+
1. [House Robber](../HouseRobber)
67+
2. [House Robber II](../HouseRobberII)
68+
3. [House Robber III](../HouseRobberIII)

algorithms/HouseRobberIII/tree.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include <algorithm>
5+
#include <cstdio>
6+
using namespace std;
7+
struct TreeNode {
8+
int val;
9+
TreeNode *left;
10+
TreeNode *right;
11+
TreeNode(int x) : val(x), left(nullptr), right(nullptr){}
12+
};
13+
class Solution {
14+
public:
15+
int rob(TreeNode *root) {
16+
int yes = 0, no = 0;
17+
dfs(root, &yes, &no);
18+
return max(yes, no);
19+
}
20+
private:
21+
void dfs(TreeNode *root, int *yes, int *no) {
22+
if (root == nullptr) {
23+
*yes = 0;
24+
*no = 0;
25+
return;
26+
}
27+
int left_yes, left_no, right_yes, right_no;
28+
dfs(root->left, &left_yes, &left_no);
29+
dfs(root->right, &right_yes, &right_no);
30+
*yes = left_no + right_no + root->val;
31+
*no = max(left_yes, left_no) + max(right_yes, right_no);
32+
}
33+
};
34+
TreeNode *mk_node(int val)
35+
{
36+
return new TreeNode(val);
37+
}
38+
TreeNode *mk_child(TreeNode *root, TreeNode *left, TreeNode *right)
39+
{
40+
root->left = left;
41+
root->right = right;
42+
return root;
43+
}
44+
TreeNode *mk_child(TreeNode *root, int left, int right)
45+
{
46+
return mk_child(root, new TreeNode(left), new TreeNode(right));
47+
}
48+
TreeNode *mk_child(int root, int left, int right)
49+
{
50+
return mk_child(new TreeNode(root), new TreeNode(left), new TreeNode(right));
51+
}
52+
int main(int argc, char **argv)
53+
{
54+
Solution solution;
55+
TreeNode *root = mk_child(4, 1, 0);
56+
return 0;
57+
}

0 commit comments

Comments
 (0)