Skip to content

Instantly share code, notes, and snippets.

@oyekanmiayo
Created August 3, 2020 21:39
Show Gist options
  • Save oyekanmiayo/4ea671c7733e46a1ef501123e356626b to your computer and use it in GitHub Desktop.
Save oyekanmiayo/4ea671c7733e46a1ef501123e356626b to your computer and use it in GitHub Desktop.
Solution to "Evaluate Division" on Leetcode
public class EvaluateDivision {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
/**
1. Traverse Edge List & Create Ajacency List (Use Map<String, List of Adjs>)
2. Remember going from point A to point B carries a weight, so we need to include that in our adjacent list. So instead of storing just a list of adjacent strings, we can store a list of adjacent Node (class) that will contain the string and weight as fields
3. Remember if A / B = 2.0, then B / A = 1/2.0. So when building the adjacency list, be sure to treat paths as bidirectional, but with weight differences (see first sentence).
4. For each query, try to go from start node to destination node. If possible, get total value, if not possible, answer for that query is -1.0
5. To ensure nodes aren't visited twice for each query, use a set
Questions:
- Is there more than one answer for a query? No, there's no contradiction
*/
Map<String, List<Node>> graph = buildMap(equations, values);
double[] result = new double[queries.size()];
for (int i = 0; i < queries.size(); i++) {
List<String> query = queries.get(i);
String src = query.get(0);
String dest = query.get(1);
if (graph.containsKey(src)) {
result[i] = dfs(src, dest, graph, new HashSet<String>());
}
/**
* If the graph does not contain the key, I want to make sure to set the value
* at that index to -1.0
*/
if (result[i] <= 0.0) {
result[i] = -1.0;
}
}
return result;
}
/**
* Traverse from initial src to dest (if available) using recursion
* Use visited node to ensure we don't visit nodes twice
* <p>
* To keep track of total value multiply current weight of node by result of further depth first searches
*/
private double dfs(String src, String dest, Map<String, List<Node>> graph, HashSet<String> visited) {
if (src.equals(dest)) {
return 1.0;
}
if (visited.contains(src)) {
return -1.0;
}
visited.add(src);
for (Node adj : graph.get(src)) {
double currRes = adj.value * dfs(adj.dest, dest, graph, visited);
if (currRes > 0) {
return currRes;
}
}
return -1.0;
}
private Map<String, List<Node>> buildMap(List<List<String>> equations, double[] values) {
Map<String, List<Node>> graph = new HashMap<>();
for (int i = 0; i < equations.size(); i++) {
List<String> eqn = equations.get(i);
double val = values[i];
graph.putIfAbsent(eqn.get(0), new ArrayList<>());
graph.putIfAbsent(eqn.get(1), new ArrayList<>());
/**
* We use a Node class here to store weights (i.e. division values)
* We inverse the division for the second entry because if A/B = X, then B/A = 1/X
*/
graph.get(eqn.get(0)).add(new Node(eqn.get(1), val));
graph.get(eqn.get(1)).add(new Node(eqn.get(0), 1.0 / val));
}
return graph;
}
}
class Node {
double value;
String dest;
Node(String dest, double value) {
this.dest = dest;
this.value = value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment