Skip to content

Commit 38f46e5

Browse files
committed
added houseRobberIII
1 parent da6bc7c commit 38f46e5

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LeetCode
88

99
| # | Title | Solution | Difficulty |
1010
|---| ----- | -------- | ---------- |
11+
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/) | [C++](./algorithms/cpp/houseRobber/houseRobberIII.cpp)|Medium|
1112
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [C++](./algorithms/cpp/increasingTripletSubsequence/increasingTripletSubsequence.cpp)|Medium|
1213
|330|[Patching Array](https://leetcode.com/problems/patching-array/) | [C++](./algorithms/cpp/patchingArray/PatchingArray.cpp)|Medium|
1314
|329|[Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/) | [C++](./algorithms/cpp/longestIncreasingPathInAMatrix/LongestIncreasingPathInAMatrix.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Source : https://leetcode.com/problems/house-robber-iii/
2+
// Author : Calinescu Valentin
3+
// Date : 2016-04-29
4+
5+
/***************************************************************************************
6+
*
7+
* The thief has found himself a new place for his thievery again. There is only one
8+
* entrance to this area, called the "root." Besides the root, each house has one and
9+
* only one parent house. After a tour, the smart thief realized that "all houses in
10+
* this place forms a binary tree". It will automatically contact the police if two
11+
* directly-linked houses were broken into on the same night.
12+
*
13+
* Determine the maximum amount of money the thief can rob tonight without alerting the
14+
* police.
15+
*
16+
* Example 1:
17+
* 3
18+
* / \
19+
* 2 3
20+
* \ \
21+
* 3 1
22+
* Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
23+
* Example 2:
24+
* 3
25+
* / \
26+
* 4 5
27+
* / \ \
28+
* 1 3 1
29+
* Maximum amount of money the thief can rob = 4 + 5 = 9.
30+
* Credits:
31+
* Special thanks to @dietpepsi for adding this problem and creating all test cases.
32+
*
33+
***************************************************************************************/
34+
/**
35+
* Definition for a binary tree node.
36+
* struct TreeNode {
37+
* int val;
38+
* TreeNode *left;
39+
* TreeNode *right;
40+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
41+
* };
42+
*/
43+
/*
44+
* Solution 1 - O(N log N)
45+
* =========
46+
*
47+
* We can use a recursive function that computes the solution for every node of the tree
48+
* using the previous solutions calculated for the left and right subtrees. At every step
49+
* we have 2 options:
50+
*
51+
* 1) Take the value of the current node + the solution of the left and right subtrees of
52+
* each of the left and right children of the current node.
53+
* 2) Take the solution of the left and right subtrees of the current node, skipping over
54+
* its value.
55+
*
56+
* This way we can make sure that we do not pick 2 adjacent nodes.
57+
*
58+
* If we implemented this right away we would get TLE. Thus, we need to optimize the
59+
* algorithm. One key observation would be that we only need to compute the solution for
60+
* a certain node once. We can use memoization to calculate every value once and then
61+
* retrieve it when we get subsequent calls. As the header of the recursive function
62+
* doesn't allow additional parameters we can use a map to link every node(a pointer) to
63+
* its solution(an int). For every call the map lookup of an element and its insertion
64+
* take logarithmic time and there are a constant number of calls for each node. Thus, the
65+
* algorithm takes O(N log N) time to finish.
66+
*
67+
*/
68+
class Solution {
69+
public:
70+
map<TreeNode*, int> dict;
71+
int rob(TreeNode* root) {
72+
if(root == NULL)
73+
return 0;
74+
else if(dict.find(root) == dict.end())
75+
{
76+
int lwith = rob(root->left);
77+
int rwith = rob(root->right);
78+
int lwithout = 0, rwithout = 0;
79+
if(root->left != NULL)
80+
lwithout = rob(root->left->left) + rob(root->left->right);
81+
if(root->right != NULL)
82+
rwithout = rob(root->right->left) + rob(root->right->right);
83+
//cout << lwith << " " << rwith << " " << lwithout << " " << rwithout << '\n';
84+
dict[root] = max(root->val + lwithout + rwithout, lwith + rwith);
85+
}
86+
return dict[root];
87+
}
88+
};

0 commit comments

Comments
 (0)