-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
58 lines (46 loc) · 1.68 KB
/
Solution.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
class Solution {
HashMap<String, HashMap<String, Double>> adj;
HashSet<String> vis;
private double dfs(String a, String t) {
if (a == t) return 0.0;
if (!adj.containsKey(a)) return -1.0;
if (adj.get(a).containsKey(t)) return adj.get(a).get(t);
if (vis.contains(a)) return -1.0;
vis.add(a);
for (var bv : adj.get(a).entrySet()) {
var b = bv.getKey();
var v = bv.getValue();
if (v < 0) continue;
var got = dfs(b, t);
if (got < 0) continue;
var res = v * got;
adj.get(a).put(t, res);
return res;
}
return -1.0;
}
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
adj = new HashMap<>();
for (int i = 0; i < equations.size(); ++i) {
var a = equations.get(i).get(0);
var b = equations.get(i).get(1);
var v = values[i];
if (!adj.containsKey(a)) adj.put(a, new HashMap<String, Double>());
if (!adj.containsKey(b)) adj.put(b, new HashMap<String, Double>());
adj.get(a).put(b, v);
if (v != 0) adj.get(b).put(a, 1.0 / v);
}
var result = new double[queries.size()];
for (int i = 0; i < queries.size(); ++i) {
vis = new HashSet<String>();
var a = queries.get(i).get(0);
var b = queries.get(i).get(1);
if (!adj.containsKey(a) || !adj.containsKey(b)) {
result[i] = -1.0;
} else {
result[i] = dfs(a, b);
}
}
return result;
}
}