Repository navigation
Expand file tree
/
Copy path399_Evaluate_Division.java
More file actions
52 lines (40 loc) · 1.68 KB
/
Copy path399_Evaluate_Division.java
File metadata and controls
52 lines (40 loc) · 1.68 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
class Solution {
private Map<String, Map<String, Double>> buildGraph(List<List<String>> equations, double[] values) {
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);
graph.putIfAbsent(u, new HashMap<>());
graph.get(u).put(v, values[i]);
graph.putIfAbsent(v, new HashMap<>());
graph.get(v).put(u, 1.0 / values[i]);
}
return graph;
}
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
Map<String, Map<String, Double>> graph = buildGraph(equations, values);
double[] result = new double[queries.size()];
for (int i = 0; i < queries.size(); i++) {
result[i] = dfs(graph, new HashSet<>(), queries.get(i).get(0), queries.get(i).get(1));
}
return result;
}
private double dfs(Map<String, Map<String, Double>> graph, Set<String> visited, String start, String end) {
if (!graph.containsKey(start)) {
return -1.0;
}
if (graph.get(start).containsKey(end)) {
return graph.get(start).get(end);
}
visited.add(start);
for (Map.Entry<String, Double> neighbour : graph.get(start).entrySet()) {
if (!visited.contains(neighbour.getKey())) {
double product = dfs(graph, visited, neighbour.getKey(), end);
if (product != -1.0) {
return neighbour.getValue() * product;
}
}
}
return -1.0;
}
}