-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHouseRobberIII.java
More file actions
73 lines (60 loc) · 1.76 KB
/
HouseRobberIII.java
File metadata and controls
73 lines (60 loc) · 1.76 KB
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
package dynamicprogramming;
import java.util.HashMap;
import java.util.Map;
// Source : https://leetcode.com/problems/house-robber-iii/
// Id : 337
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-09-17
// Topic : Dynamic Programming
// Level : Medium
// Other :
// Tips :
// Result : 100.00% 100.00%
public class HouseRobberIII {
Map<TreeNode, Integer> memo = new HashMap<>();
//! 5.40% 1136 ms 33.33% without memo
// 59.03% 3ms 33.33%
public int rob(TreeNode root) {
if (root == null)
return 0;
if (memo.containsKey(root))
return memo.get(root);
int ans = Math.max(rob(root, true), rob(root, false));
memo.put(root, ans);
return ans;
}
private int rob(TreeNode root, boolean rootIncluded) {
if (root == null)
return 0;
if (rootIncluded)
return root.val + rob(root.left, false) + rob(root.right, false);
else
return rob(root.left) + rob(root.right);
}
// 100.00% 0ms 33.33%
public int rob1(TreeNode root) {
// public int rob(TreeNode root) {
if (root == null)
return 0;
int[] res = solve(root);
return Math.max(res[0], res[1]);
}
private int[] solve(TreeNode root) {
int[] res = new int[2];
if (root == null)
return res;
int[] left = solve(root.left);
int[] right = solve(root.right);
res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
res[1] = root.val + left[0] + right[0];
return res;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}