|
1 | 1 | class Solution {
|
2 |
| - public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) { |
3 |
| - double[] ans = new double[queries.size()]; |
4 |
| - |
5 |
| - Map<String, Set<String>> map = new HashMap<>(); |
6 |
| - Map<String, Double> resMap = new HashMap<>(); |
7 |
| - |
8 |
| - for (int i = 0; i < equations.size(); i++) { |
9 |
| - String operand1 = equations.get(i).get(0); |
10 |
| - String operand2 = equations.get(i).get(1); |
11 |
| - |
12 |
| - map.computeIfAbsent(operand1, k -> new HashSet<>()).add(operand2); |
13 |
| - map.computeIfAbsent(operand2, k -> new HashSet<>()).add(operand1); |
14 |
| - |
15 |
| - resMap.put(operand1 + "|" + operand2, values[i]); |
16 |
| - } |
17 |
| - |
18 |
| - for (int i = 0; i < queries.size(); i++) { |
19 |
| - String operand1 = queries.get(i).get(0); |
20 |
| - String operand2 = queries.get(i).get(1); |
21 |
| - |
22 |
| - if (!map.containsKey(operand1) || !map.containsKey(operand2)) { |
23 |
| - ans[i] = -1.0; |
24 |
| - } |
25 |
| - else if (operand1.equals(operand2)) { |
26 |
| - ans[i] = 1.0; |
27 |
| - } |
28 |
| - else if (map.get(operand1).contains(operand2)) { |
29 |
| - ans[i] = getSimpleDivisionVal(resMap, operand1, operand2); |
30 |
| - } |
31 |
| - else if (operand2.contains(operand1)) { |
32 |
| - ans[i] = getSimpleDivisionVal(resMap, operand2, operand1); |
33 |
| - } |
34 |
| - else { |
35 |
| - ans[i] = dfs(map, resMap, operand1, operand2, 1.0, new HashSet<>()); |
36 |
| - } |
37 |
| - } |
38 |
| - |
39 |
| - return ans; |
| 2 | + public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) { |
| 3 | + Map<String, List<String>> map = new HashMap<>(); |
| 4 | + Map<String, List<Double>> valueMap = new HashMap<>(); |
| 5 | + for (int i = 0; i < equations.size(); i++) { |
| 6 | + List<String> equation = equations.get(i); |
| 7 | + map.computeIfAbsent(equation.get(0), k -> new ArrayList<>()).add(equation.get(1)); |
| 8 | + map.computeIfAbsent(equation.get(1), k -> new ArrayList<>()).add(equation.get(0)); |
| 9 | + valueMap.computeIfAbsent(equation.get(0), k -> new ArrayList<>()).add(values[i]); |
| 10 | + valueMap.computeIfAbsent(equation.get(1), k -> new ArrayList<>()).add(1 / values[i]); |
40 | 11 | }
|
41 |
| - |
42 |
| - private double dfs (Map<String, Set<String>> map, Map<String, Double> resMap, String operand1, String operand2, |
43 |
| - double base, Set<String> visited) { |
44 |
| - if (visited.contains(operand1)) { |
45 |
| - return -1.0; |
46 |
| - } |
47 |
| - if (map.get(operand1).contains(operand2)) { |
48 |
| - return base * getSimpleDivisionVal(resMap, operand1, operand2); |
49 |
| - } |
50 |
| - |
51 |
| - visited.add(operand1); |
52 |
| - |
53 |
| - double ans = -1.0; |
54 |
| - Iterator<String> children = map.get(operand1).iterator(); |
55 |
| - |
56 |
| - while (children.hasNext()) { |
57 |
| - String child = children.next(); |
58 |
| - if (!visited.contains(child)) { |
59 |
| - ans = Math.max(ans, dfs(map, resMap, child, operand2, base * getSimpleDivisionVal(resMap, operand1, child), visited)); |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - return ans; |
| 12 | + double[] ans = new double[queries.size()]; |
| 13 | + for (int i = 0; i < queries.size(); i++) { |
| 14 | + List<String> query = queries.get(i); |
| 15 | + ans[i] = dfs(map, valueMap, query.get(0), query.get(1), new HashSet<>(), 1.0); |
| 16 | + ans[i] = ans[i] == 0.0 ? -1.0 : ans[i]; |
64 | 17 | }
|
65 |
| - |
66 |
| - private double getSimpleDivisionVal (Map<String, Double> resMap, String operand1, String operand2) { |
67 |
| - if (resMap.containsKey(operand1 + "|" + operand2)) { |
68 |
| - return resMap.get(operand1 + "|" + operand2); |
69 |
| - } |
70 |
| - |
71 |
| - return 1 / resMap.get(operand2 + "|" + operand1); |
| 18 | + return ans; |
| 19 | + } |
| 20 | + |
| 21 | + private double dfs(Map<String, List<String>> map, Map<String, List<Double>> valueMap, String a, String b, Set<String> set, double curr) { |
| 22 | + if (set.contains(a)) { |
| 23 | + return 0.0; |
72 | 24 | }
|
| 25 | + if (!map.containsKey(a)) { |
| 26 | + return 0.0; |
| 27 | + } |
| 28 | + if (a.equals(b)) { |
| 29 | + return curr; |
| 30 | + } |
| 31 | + set.add(a); |
| 32 | + List<String> children = map.get(a); |
| 33 | + List<Double> valueList = valueMap.get(a); |
| 34 | + double temp = 0.0; |
| 35 | + for (int i = 0; i < children.size(); i++) { |
| 36 | + temp = dfs(map, valueMap, children.get(i), b, set, curr * valueList.get(i)); |
| 37 | + if (temp != 0.0) { |
| 38 | + break; |
| 39 | + } |
| 40 | + } |
| 41 | + set.remove(a); |
| 42 | + return temp; |
| 43 | + } |
73 | 44 | }
|
0 commit comments