-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path1443.Minimum-Time-to-Collect-All-Apples-in-a-Tree.java
69 lines (53 loc) · 1.73 KB
/
1443.Minimum-Time-to-Collect-All-Apples-in-a-Tree.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
// https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/
// algorithms
// Medium (56.12%)
// Total Accepted: 10,821
// Total Submissions: 19,283
class Solution {
private static int res;
public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
res = 0;
Map<Integer, List<Integer>> childMap = new HashMap<>();
for (int[] e : edges) {
int from = e[0], to = e[1];
List<Integer> fromTmp = childMap.get(from);
if (fromTmp == null) {
fromTmp = new ArrayList<>();
childMap.put(from, fromTmp);
}
fromTmp.add(to);
List<Integer> toTmp = childMap.get(to);
if (toTmp == null) {
toTmp = new ArrayList<>();
childMap.put(to, toTmp);
}
toTmp.add(from);
}
recursive(0, n, hasApple, childMap, -1);
return res;
}
private boolean recursive(int curNode, int n, List<Boolean> hasApple, Map<Integer, List<Integer>> childMap,
int parent) {
if (curNode >= n) {
return false;
}
List<Integer> children = childMap.get(curNode);
boolean isChildHasApple = false;
if (children != null) {
for (int child : children) {
if (child == parent) {
continue;
}
boolean tmp = recursive(child, n, hasApple, childMap, curNode);
isChildHasApple |= tmp;
}
}
if (isChildHasApple || hasApple.get(curNode)) {
if (curNode != 0) {
res += 2;
}
return true;
}
return false;
}
}