Skip to content

Commit 29eeea9

Browse files
Update evaluate-division.java
It works this time
1 parent dd5d615 commit 29eeea9

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

evaluate-division.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,46 @@ public static void main (String[] args) throws java.lang.Exception
2121

2222

2323
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
24-
HashMap<String, HashMap<String, Double>> hm = new HashMap<String, HashMap<String, Double>>();
24+
HashMap<String, ArrayList<String>> pairs = new HashMap<String, ArrayList<String>>();
25+
HashMap<String, ArrayList<Double>> weights = new HashMap<String, ArrayList<Double>>();
26+
2527
for (int i = 0; i < equations.length; i++) {
26-
String[] strs = equations[i];
27-
if (!hm.containsKey(strs[0])) {
28-
HashMap<String, Double> sub = new HashMap<String, Double>();
29-
hm.put(strs[0], sub);
28+
if (!pairs.containsKey(equations[i][0])) {
29+
pairs.put(equations[i][0], new ArrayList<String>());
30+
weights.put(equations[i][0], new ArrayList<Double>());
3031
}
31-
hm.get(strs[0]).put(strs[1], values[i]);
32-
if (!hm.containsKey(strs[1])) {
33-
HashMap<String, Double> sub = new HashMap<String, Double>();
34-
hm.put(strs[1], sub);
32+
if (!pairs.containsKey(equations[i][1])) {
33+
pairs.put(equations[i][1], new ArrayList<String>());
34+
weights.put(equations[i][1], new ArrayList<Double>());
3535
}
36-
hm.get(strs[1]).put(strs[1], 1/values[i]);
36+
pairs.get(equations[i][0]).add(equations[i][1]);
37+
weights.get(equations[i][0]).add(values[i]);
38+
pairs.get(equations[i][1]).add(equations[i][0]);
39+
weights.get(equations[i][1]).add(1/values[i]);
3740
}
3841
double[] results = new double[queries.length];
3942
for (int i = 0; i < queries.length; i++) {
40-
if (hm.containsKey(queries[i][0])) {
41-
HashMap<String, Double> sub = hm.get(queries[i][0]);
42-
if (sub.containsKey(queries[i][1])) results[i] = sub.get(queries[i][1]);
43-
else results[i] = -1.0;
44-
} else {
45-
results[i] = -1.0;
46-
}
43+
double retVal = dfs(queries[i][0], queries[i][1], 1.0, pairs, weights, new HashSet<String>());
44+
if (retVal == 0.0) results[i] = -1.0; else results[i] = retVal;
4745
}
4846
return results;
49-
}
47+
}
48+
public double dfs(String startNode, String endNode, double value, HashMap<String, ArrayList<String>> pairs,
49+
HashMap<String, ArrayList<Double>> weights, HashSet<String> visited) {
50+
if (visited.contains(startNode)) return 0.0;
51+
if (!pairs.containsKey(startNode)) return 0.0;
52+
if (startNode.equals(endNode)) return value;
53+
List<String> toNodes = pairs.get(startNode);
54+
visited.add(startNode);
55+
double retVal = 0.0;
56+
for (int i = 0; i < toNodes.size(); i++) {
57+
String next = toNodes.get(i);
58+
retVal = dfs(next, endNode, value * weights.get(startNode).get(i), pairs, weights, visited);
59+
if (retVal != 0.0) {
60+
break;
61+
}
62+
}
63+
visited.remove(startNode); // remove because there might be a case of a/b = 3.0 and c/b = 2.0
64+
return retVal;
65+
}
5066
}

0 commit comments

Comments
 (0)