Skip to content

Commit 745d941

Browse files
Add files via upload
1 parent e294f7f commit 745d941

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

House Robber III/House_Robber_III.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Runtime: 28 ms, faster than 72.39% of C++ online submissions for House Robber III.
2+
// Memory Usage: 23.8 MB, less than 23.60% of C++ online submissions for House Robber III.
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* struct TreeNode {
7+
* int val;
8+
* TreeNode *left;
9+
* TreeNode *right;
10+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
11+
* };
12+
*/
13+
typedef unordered_map<TreeNode*, int> HashMap;
14+
class Solution
15+
{
16+
public:
17+
int rob(TreeNode* root)
18+
{
19+
HashMap hashmap;
20+
return helper(root, hashmap);
21+
}
22+
private:
23+
int helper(TreeNode* root, HashMap& hashmap)
24+
{
25+
if (!root)
26+
return 0;
27+
if (hashmap.find(root) == hashmap.end())
28+
{
29+
int notrobfirst = helper(root->left, hashmap) + helper(root->right, hashmap);
30+
31+
int robfirst = root->val;
32+
if (root->left && root->right)
33+
robfirst += helper(root->left->left, hashmap) +
34+
helper(root->left->right, hashmap) +
35+
helper(root->right->left, hashmap) +
36+
helper(root->right->right, hashmap);
37+
38+
else if (root->left)
39+
robfirst += helper(root->left->left, hashmap) + helper(root->left->right, hashmap);
40+
41+
else if (root->right)
42+
robfirst += helper(root->right->left, hashmap) + helper(root->right->right, hashmap);
43+
44+
hashmap.insert(make_pair(root, max(notrobfirst, robfirst)));
45+
}
46+
return hashmap.at(root);
47+
}
48+
};

House Robber III/House_Robber_III.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Runtime: 72 ms, faster than 39.39% of Python3 online submissions for House Robber III.
2+
# Memory Usage: 15.8 MB, less than 5.09% of Python3 online submissions for House Robber III.
3+
4+
# Definition for a binary tree node.
5+
# class TreeNode:
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.left = None
9+
# self.right = None
10+
11+
class Solution:
12+
def rob(self, root: TreeNode) -> int:
13+
hashmap = dict()
14+
return self.helper(root, hashmap)
15+
def helper(self, root, hashmap):
16+
if root is None:
17+
return 0
18+
if root not in hashmap:
19+
notrobfirst = self.helper(root.left, hashmap) + self.helper(root.right, hashmap)
20+
robfirst = root.val
21+
if root.left and root.right:
22+
robfirst += (self.helper(root.left.left, hashmap) +
23+
self.helper(root.left.right, hashmap) +
24+
self.helper(root.right.left, hashmap) +
25+
self.helper(root.right.right, hashmap))
26+
elif root.left:
27+
robfirst += self.helper(root.left.left, hashmap) + self.helper(root.left.right, hashmap)
28+
elif root.right:
29+
robfirst += self.helper(root.right.left, hashmap) + self.helper(root.right.right, hashmap)
30+
31+
hashmap[root] = max(robfirst, notrobfirst)
32+
return hashmap[root]

0 commit comments

Comments
 (0)