|
19 | 19 | 3
|
20 | 20 | / \
|
21 | 21 | 2 3
|
22 |
| - \ \ |
| 22 | + \ \ |
23 | 23 | 3 1
|
24 | 24 | Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
|
25 | 25 |
|
26 | 26 | Example 2:
|
27 | 27 | 3
|
28 | 28 | / \
|
29 | 29 | 4 5
|
30 |
| - / \ \ |
| 30 | + / \ \ |
31 | 31 | 1 3 1
|
32 | 32 | Maximum amount of money the thief can rob = 4 + 5 = 9.
|
33 | 33 | */
|
34 | 34 |
|
35 | 35 | public class _337 {
|
36 | 36 |
|
37 |
| - //same idea, but with cacheing via a hashmap: 8 ms |
38 |
| - public int rob_dp(TreeNode root) { |
39 |
| - Map<TreeNode, Integer> map = new HashMap<>(); |
40 |
| - return getMaxValue(root, map); |
41 |
| - } |
42 |
| - |
43 |
| - private int getMaxValue(TreeNode root, Map<TreeNode, Integer> map) { |
44 |
| - if (root == null) { |
45 |
| - return 0; |
46 |
| - } |
47 |
| - if (map.containsKey(root)) { |
48 |
| - return map.get(root); |
49 |
| - } |
| 37 | + public static class Solution1 { |
| 38 | + //simple recursion without cacheing: 1189 ms |
| 39 | + public int rob(TreeNode root) { |
| 40 | + if (root == null) { |
| 41 | + return 0; |
| 42 | + } |
50 | 43 |
|
51 |
| - int val = 0; |
52 |
| - if (root.left != null) { |
53 |
| - val += getMaxValue(root.left.left, map) + getMaxValue(root.left.right, map); |
| 44 | + int val = 0; |
| 45 | + if (root.left != null) { |
| 46 | + val += rob(root.left.left) + rob(root.left.right); |
| 47 | + } |
| 48 | + if (root.right != null) { |
| 49 | + val += rob(root.right.left) + rob(root.right.right); |
| 50 | + } |
| 51 | + val = Math.max(val + root.val, rob(root.left) + rob(root.right)); |
| 52 | + return val; |
54 | 53 | }
|
55 |
| - if (root.right != null) { |
56 |
| - val += getMaxValue(root.right.left, map) + getMaxValue(root.right.right, map); |
57 |
| - } |
58 |
| - int max = Math.max(root.val + val, getMaxValue(root.left, map) + getMaxValue(root.right, map)); |
59 |
| - map.put(root, max); |
60 |
| - return max; |
61 | 54 | }
|
62 | 55 |
|
63 |
| - //simple recursion without cacheing: 1189 ms |
64 |
| - public int rob(TreeNode root) { |
65 |
| - if (root == null) { |
66 |
| - return 0; |
| 56 | + public static class Solution2 { |
| 57 | + //same idea, but with cacheing via a hashmap: 8 ms |
| 58 | + public int rob_dp(TreeNode root) { |
| 59 | + Map<TreeNode, Integer> map = new HashMap<>(); |
| 60 | + return getMaxValue(root, map); |
67 | 61 | }
|
68 | 62 |
|
69 |
| - int val = 0; |
70 |
| - if (root.left != null) { |
71 |
| - val += rob(root.left.left) + rob(root.left.right); |
72 |
| - } |
73 |
| - if (root.right != null) { |
74 |
| - val += rob(root.right.left) + rob(root.right.right); |
| 63 | + private int getMaxValue(TreeNode root, Map<TreeNode, Integer> map) { |
| 64 | + if (root == null) { |
| 65 | + return 0; |
| 66 | + } |
| 67 | + if (map.containsKey(root)) { |
| 68 | + return map.get(root); |
| 69 | + } |
| 70 | + |
| 71 | + int val = 0; |
| 72 | + if (root.left != null) { |
| 73 | + val += getMaxValue(root.left.left, map) + getMaxValue(root.left.right, map); |
| 74 | + } |
| 75 | + if (root.right != null) { |
| 76 | + val += getMaxValue(root.right.left, map) + getMaxValue(root.right.right, map); |
| 77 | + } |
| 78 | + int max = Math.max(root.val + val, |
| 79 | + getMaxValue(root.left, map) + getMaxValue(root.right, map)); |
| 80 | + map.put(root, max); |
| 81 | + return max; |
75 | 82 | }
|
76 |
| - val = Math.max(val + root.val, rob(root.left) + rob(root.right)); |
77 |
| - return val; |
78 | 83 | }
|
79 | 84 |
|
80 | 85 | }
|
0 commit comments