|
9 | 9 | public class EvaluateDivision {
|
10 | 10 |
|
11 | 11 | public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
|
12 |
| - HashMap<String, HashMap<String, Double>> valueMap = new HashMap<>(); |
13 |
| - |
| 12 | + HashMap<String, HashMap<String, Double>> table = new HashMap<>(); |
14 | 13 | for (int i = 0; i < equations.length; i++) {
|
15 |
| - String[] equation = equations[i]; |
16 |
| - HashMap<String, Double> map = valueMap.computeIfAbsent(equation[0], k -> new HashMap<>()); |
17 |
| - map.put(equation[1], values[i]); |
18 |
| - map = valueMap.computeIfAbsent(equation[1], k -> new HashMap<>()); |
19 |
| - map.put(equation[0], 1 / values[i]); |
| 14 | + add(table, equations[i], values[i]); |
20 | 15 | }
|
21 |
| - |
22 | 16 | double[] result = new double[queries.length];
|
| 17 | + HashSet<String> visited = new HashSet<>(); |
23 | 18 | for (int i = 0; i < queries.length; i++) {
|
24 |
| - double[] value = new double[] {1.0}; |
25 |
| - result[i] = dfs(valueMap, queries[i][0], queries[i][1], new HashSet<>(), value) ? value[0] : -1.0; |
| 19 | + visited.clear(); |
| 20 | + result[i] = calc(table, visited, queries[i][0], queries[i][1]); |
26 | 21 | }
|
27 | 22 | return result;
|
28 | 23 | }
|
29 | 24 |
|
30 |
| - private boolean dfs(HashMap<String, HashMap<String, Double>> map, String start, String end, HashSet<String> set, double[] value) { |
31 |
| - if (!map.containsKey(start) || !map.containsKey(end) || set.contains(start)) { |
32 |
| - return false; |
| 25 | + private double calc(HashMap<String, HashMap<String, Double>> table, HashSet<String> visited, String a, String b) { |
| 26 | + HashMap<String, Double> map = table.get(a); |
| 27 | + if (map == null) { |
| 28 | + return -1.0; |
33 | 29 | }
|
34 |
| - if (start.equals(end)) { |
35 |
| - return true; |
| 30 | + if (a.equals(b)) { |
| 31 | + return 1.0; |
36 | 32 | }
|
37 |
| - set.add(start); |
38 |
| - HashMap<String, Double> valueMap = map.get(start); |
39 |
| - boolean flag = false; |
40 |
| - for (Map.Entry<String, Double> entry : valueMap.entrySet()) { |
41 |
| - value[0] *= entry.getValue(); |
42 |
| - if (dfs(map, entry.getKey(), end, set, value)) { |
43 |
| - flag = true; |
44 |
| - break; |
| 33 | + for (Map.Entry<String, Double> entry : map.entrySet()) { |
| 34 | + String key = entry.getKey(); |
| 35 | + Double value = entry.getValue(); |
| 36 | + |
| 37 | + if (visited.contains(key)) { |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + visited.add(key); |
| 42 | + |
| 43 | + double t = calc(table, visited, key, b); |
| 44 | + if (t != -1.0) { |
| 45 | + return value * t; |
45 | 46 | }
|
46 |
| - value[0] /= entry.getValue(); |
| 47 | + |
| 48 | + visited.remove(key); |
| 49 | + } |
| 50 | + return -1.0; |
| 51 | + } |
| 52 | + |
| 53 | + private void add(HashMap<String, HashMap<String, Double>> table, String[] equation, double value) { |
| 54 | + HashMap<String, Double> map0 = table.get(equation[0]); |
| 55 | + HashMap<String, Double> map1 = table.get(equation[1]); |
| 56 | + if (map0 == null) { |
| 57 | + map0 = new HashMap<>(); |
| 58 | + table.put(equation[0], map0); |
| 59 | + } |
| 60 | + if (map1 == null) { |
| 61 | + map1 = new HashMap<>(); |
| 62 | + table.put(equation[1], map1); |
47 | 63 | }
|
48 |
| - set.remove(start); |
49 |
| - return flag; |
| 64 | + map0.put(equation[1], value); |
| 65 | + map1.put(equation[0], 1 / value); |
50 | 66 | }
|
51 | 67 | }
|
0 commit comments