Advertisement
1988coder

399. Evaluate Division

Feb 2nd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1.  
  2. // LeetCode URL: https://leetcode.com/problems/evaluate-division/
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5.  
  6. /**
  7.  * Build a graph of equations and then perform DFS to find the result of each
  8.  * query
  9.  *
  10.  * Imagine a/b = k as a link between node a and b, the value of link from a to b
  11.  * is k, the reverse link is 1/k. Query is to find a path between two nodes.
  12.  *
  13.  * Time Complexity: BuildGraph + DFS * NoOfQueries. BuildGraph = O(E). DFS =
  14.  * (NoOfNodes + NoOfEdges) = O(2*E + 2*E) = O(4*E).
  15.  *
  16.  * Thus total complexity = O(E + 4*E*Q) = O(E*Q).
  17.  *
  18.  * Space Complexity: Space taken by graph + Recursion Depth = O(E).
  19.  *
  20.  * E = length of equations or values. Q = length of queries
  21.  */
  22. class Solution {
  23.     public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
  24.         if (queries == null || queries.length == 0) {
  25.             return new double[0];
  26.         }
  27.  
  28.         HashMap<String, HashMap<String, Double>> graph = new HashMap<>();
  29.         buildGraph(graph, equations, values);
  30.  
  31.         double[] result = new double[queries.length];
  32.  
  33.         for (int i = 0; i < queries.length; i++) {
  34.             String[] q = queries[i];
  35.             if (!graph.containsKey(q[0]) || !graph.containsKey(q[1])) {
  36.                 result[i] = -1.0;
  37.             } else {
  38.                 result[i] = dfsHelper(q[0], q[1], new HashSet<>(), graph, 1.0);
  39.             }
  40.         }
  41.  
  42.         return result;
  43.     }
  44.  
  45.     private double dfsHelper(String src, String dest, HashSet<String> visited,
  46.             HashMap<String, HashMap<String, Double>> graph, double curVal) {
  47.         if (visited.contains(src)) {
  48.             return -1.0;
  49.         }
  50.         if (src.equals(dest)) {
  51.             return curVal;
  52.         }
  53.  
  54.         visited.add(src);
  55.         HashMap<String, Double> neighbours = graph.get(src);
  56.  
  57.         for (String neigh : neighbours.keySet()) {
  58.             double tmp = dfsHelper(neigh, dest, visited, graph, neighbours.get(neigh) * curVal);
  59.             if (tmp != -1.0) {
  60.                 return tmp;
  61.             }
  62.         }
  63.  
  64.         return -1.0;
  65.     }
  66.  
  67.     private void buildGraph(HashMap<String, HashMap<String, Double>> graph, String[][] equations, double[] values) {
  68.         if (equations == null || equations.length == 0 || values == null || values.length == 0
  69.                 || equations.length != values.length) {
  70.             return;
  71.         }
  72.  
  73.         for (int i = 0; i < equations.length; i++) {
  74.             String[] eq = equations[i];
  75.  
  76.             if (!graph.containsKey(eq[0])) {
  77.                 graph.put(eq[0], new HashMap<>());
  78.             }
  79.             if (!graph.containsKey(eq[1])) {
  80.                 graph.put(eq[1], new HashMap<>());
  81.             }
  82.  
  83.             graph.get(eq[0]).put(eq[1], values[i]);
  84.             graph.get(eq[1]).put(eq[0], 1 / values[i]);
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement