# Untitled

unknown
plain_text
a month ago
2.3 kB
5
Indexable
Never
```class Solution {
class Triple {
String destination;
double weight;

public Triple(String d, double w) {
this.destination = d;
this.weight = w;
}

}

public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
HashMap<String, List<Triple>> adjList = new HashMap<>();

for (int i = 0; i < equations.size(); i++) {
String s1 = equations.get(i).get(0);
String s2 = equations.get(i).get(1);

double val = values[i];

List<Triple> sourceNode = adjList.getOrDefault(s1, new ArrayList<Triple>());

double reverseVal = 1.0/val;
List<Triple> destNode = adjList.getOrDefault(s2, new ArrayList<Triple>());

}

double[] result = new double[queries.size()];
int i = 0;

for (List<String> list : queries) {
String s = list.get(0);
String d = list.get(1);

result[i] = -1.00;
i++;
continue;
}

if (s.equals(d)) {
result[i] = 1.00;
i++;
continue;
}

Set<String> visited = new HashSet<>();
result[i] = dfs(s, d, adjList, visited);
i++;
}

return result;

}

private double dfs(String source, String dest, HashMap<String, List<Triple>> adjList, Set<String> visited) {

double sum = -1;

for (Triple t : adjList.get(source)) {
if (visited.contains(t.destination)) {
continue;
}

if (t.destination.equals(dest)) {
return t.weight;
}
double val = dfs(t.destination, dest, adjList, visited);

if (val >= 0) {
sum = t.weight * val;
}
}

return sum;
}
}```