The trick here is to name the state correctly, then let the implementation follow. For 337. House Robber III, the solution in this repository is mainly a DFS + memoization solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: DFS + memoization, dynamic programming, two pointers, sliding window.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are rob, dfs.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 28 ms, faster than 34.86% of C++ online submissions for House Robber III.
02//Memory Usage: 25.1 MB, less than 16.67% of C++ online submissions for House Robber III.
03/**
04 * Definition for a binary tree node.
05 * struct TreeNode {
06 * int val;
07 * TreeNode *left;
08 * TreeNode *right;
09 * TreeNode() : val(0), left(nullptr), right(nullptr) {}
10 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
11 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
12 * };
13 */
14class Solution {
15public:
16 unordered_map<TreeNode*, int> memo;
17
18 int rob(TreeNode* root) {
19 if(root == nullptr) return 0;
20 if(memo.find(root) != memo.end()) return memo[root];
21
22 int robHere = root->val;
23 if(root->left) robHere += rob(root->left->left) + rob(root->left->right);
24 if(root->right) robHere += rob(root->right->left) + rob(root->right->right);
25
26 int robNext = rob(root->left) + rob(root->right);
27
28 memo[root] = max(robHere, robNext);
29 return memo[root];
30 }
31};
32
33//dfs without memo
34//https://github.com/keineahnung2345/fucking-algorithm/blob/note/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E7%B3%BB%E5%88%97/%E6%8A%A2%E6%88%BF%E5%AD%90.md
35//Runtime: 32 ms, faster than 26.22% of C++ online submissions for House Robber III.
36//Memory Usage: 32.5 MB, less than 5.55% of C++ online submissions for House Robber III.
37/**
38 * Definition for a binary tree node.
39 * struct TreeNode {
40 * int val;
41 * TreeNode *left;
42 * TreeNode *right;
43 * TreeNode() : val(0), left(nullptr), right(nullptr) {}
44 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
45 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
46 * };
47 */
48class Solution {
49public:
50 vector<int> dfs(TreeNode* node){
51 if(node == nullptr) return {0,0};
52
53 /*
54 a vector of two elements,
55 first is the money if robe here,
56 second is the money if robe its next
57 */
58 vector<int> left = dfs(node->left);
59 vector<int> right = dfs(node->right);
60
61 /*
62 left[1]:
63 dfs(node->left)[1] :
64 max(preLeft[0], preLeft[1]) + max(preRight[0], preRight[1]) :
65 maxValueOf(dfs(node->left->left)) + maxValueOf(dfs(node->left->right)) :
66 robInPrevMethod(node->left->left) + robInPrevMethod(node->left->right)
67 */
68 int robHere = node->val + left[1] + right[1];
69
70 /*
71 why not:
72 int robNext = left[0] + right[0];
73 ?
74
75 in the last method:
76 int robNext = rob(root->left) + rob(root->right);
77 return max(robHere, robNext);
78 the return value of rob() function is the max of robHere and robNext,
79 but in current method, we don't take max() before dfs() return.
80 So here we need to take max of left and right to
81 mimic what the last method does
82 */
83 int robNext = max(left[0], left[1]) + max(right[0], right[1]);
84
85 return {robHere, robNext};
86 };
87
88 int rob(TreeNode* root) {
89 bool dummy = false;
90 vector<int> v = dfs(root);
91 return max(v[0], v[1]);
92 }
93};
Cost