forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_337.java
85 lines (71 loc) · 2.43 KB
/
_337.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.HashMap;
import java.util.Map;
/**337. House Robber III
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.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Example 1:
3
/ \
2 3
\ \
3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
3
/ \
4 5
/ \ \
1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.
*/
public class _337 {
public static class Solution1 {
//simple recursion without cacheing: 1189 ms
public int rob(TreeNode root) {
if (root == null) {
return 0;
}
int val = 0;
if (root.left != null) {
val += rob(root.left.left) + rob(root.left.right);
}
if (root.right != null) {
val += rob(root.right.left) + rob(root.right.right);
}
val = Math.max(val + root.val, rob(root.left) + rob(root.right));
return val;
}
}
public static class Solution2 {
//same idea, but with cacheing via a hashmap: 8 ms
public int rob_dp(TreeNode root) {
Map<TreeNode, Integer> map = new HashMap<>();
return getMaxValue(root, map);
}
private int getMaxValue(TreeNode root, Map<TreeNode, Integer> map) {
if (root == null) {
return 0;
}
if (map.containsKey(root)) {
return map.get(root);
}
int val = 0;
if (root.left != null) {
val += getMaxValue(root.left.left, map) + getMaxValue(root.left.right, map);
}
if (root.right != null) {
val += getMaxValue(root.right.left, map) + getMaxValue(root.right.right, map);
}
int max = Math.max(root.val + val,
getMaxValue(root.left, map) + getMaxValue(root.right, map));
map.put(root, max);
return max;
}
}
}