/
MinimumTimeToCollectAllApples.java
161 lines (127 loc) · 3.82 KB
/
MinimumTimeToCollectAllApples.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/
class Solution {
public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
int totalApples = 0;
for (boolean x: hasApple) {
if (x) totalApples++;
}
if (totalApples == 0) {
return 0;
}
Map<Integer, List<Integer>> adj = adjMapOf(edges);
List<Deque<Integer>> paths = new ArrayList<>();
dfs(adj, 0, hasApple, new ArrayDeque<>(), new HashSet<>(), paths);
Distance dist = new Distance(totalApples);
dfs(extractEdgesFrom(paths), 0, hasApple, new HashSet<>(), false, dist);
return dist.value();
}
void dfs(
Map<Integer, List<Integer>> adj,
int curr,
List<Boolean> hasApple,
Deque<Integer> path,
Set<Integer> visited,
List<Deque<Integer>> paths) {
if (visited.contains(curr)) {
return;
}
visited.add(curr);
path.add(curr);
if (hasApple.get(curr)) {
paths.add(new ArrayDeque<>(path));
}
List<Integer> neighbors = adj.get(curr);
if (neighbors == null) {
return;
}
for (int x: neighbors) {
dfs(adj, x, hasApple, path, visited, paths);
}
visited.remove(curr);
path.removeLast();
}
Map<Integer, List<Integer>> adjMapOf(int[][] edges) {
Map<Integer, List<Integer>> adj = new HashMap<>();
for (int[] edge: edges) {
adj.computeIfAbsent(edge[0], x -> new ArrayList<>()).add(edge[1]);
adj.computeIfAbsent(edge[1], x -> new ArrayList<>()).add(edge[0]);
}
return adj;
}
Map<Integer, Set<Integer>> extractEdgesFrom(List<Deque<Integer>> paths) {
Map<Integer, Set<Integer>> adj = new HashMap<>();
for (var path: paths) {
int last = -1;
for (int x: path) {
if (last == -1) {
last = x;
} else {
adj.computeIfAbsent(x, y -> new TreeSet<>()).add(last);
adj.computeIfAbsent(last, y -> new TreeSet<>()).add(x);
last = x;
}
}
}
return adj;
}
boolean dfs(
Map<Integer, Set<Integer>> adj,
int curr,
List<Boolean> hasApple,
Set<Integer> visited,
boolean force,
Distance dist
) {
if (hasApple.get(curr)) {
dist.incTask();
hasApple.set(curr, false);
}
if (curr == 0 && dist.areTasksCompleted()) {
return true;
}
if (!force) {
if (visited.contains(curr))
return false;
}
visited.add(curr);
dist.inc();
Set<Integer> neighbors = adj.get(curr);
if (neighbors == null) {
return false;
}
boolean didVisitAny = false;
for (int x: neighbors) {
if (!visited.contains(x)) {
didVisitAny = true;
boolean res = dfs(adj, x, hasApple, visited, false, dist);
if (res) return true;
}
}
if (!didVisitAny) {
for (int x: neighbors) {
boolean res = dfs(adj, x, hasApple, visited, true, dist);
if (res) return true;
}
}
visited.remove(curr);
return false;
}
class Distance {
private int dist, task;
Distance(int totalTask) {
this.task = totalTask;
}
void inc() {
dist++;
}
void incTask() {
task--;
}
boolean areTasksCompleted() {
return task == 0;
}
int value() {
return dist;
}
}
}