-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevalDivision.java
More file actions
55 lines (42 loc) · 1.71 KB
/
evalDivision.java
File metadata and controls
55 lines (42 loc) · 1.71 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
class Solution {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
Map<String, Map<String, Double>> graph = new HashMap<>();
for (int i = 0; i < equations.size(); i++) {
String u = equations.get(i).get(0);
String v = equations.get(i).get(1);
double val = values[i];
graph.putIfAbsent(u, new HashMap<>());
graph.putIfAbsent(v, new HashMap<>());
graph.get(u).put(v, val);
graph.get(v).put(u, 1.0 / val);
}
double[] res = new double[queries.size()];
for (int i = 0; i < queries.size(); i++) {
String src = queries.get(i).get(0);
String dest = queries.get(i).get(1);
if (!graph.containsKey(src) || !graph.containsKey(dest)) {
res[i] = -1.0;
} else if (src.equals(dest)) {
res[i] = 1.0;
} else {
res[i] = dfs(graph, src, dest, 1.0, new HashSet<>());
}
}
return res;
}
private static double dfs(Map<String, Map<String, Double>> graph,
String curr, String target,
double product, Set<String> visited) {
visited.add(curr);
if (curr.equals(target)) return product;
for (String nei : graph.get(curr).keySet()) {
if (!visited.contains(nei)) {
double result = dfs(graph, nei, target,
product * graph.get(curr).get(nei),
visited);
if (result != -1.0) return result;
}
}
return -1.0;
}
}