Advertisement
uopspop

Untitled

Jul 14th, 2021
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. class Solution {
  2.     public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
  3.        
  4.        
  5.         Map<String, List<String>> graph1 = new HashMap<>(); // bi-direcction
  6.         Map<String, Double> graph = new HashMap<>(); // bi-direcction
  7.         for (int i = 0; i < equations.size() ;i++) {
  8.             List<String> equation = equations.get(i);
  9.             String key1 = equation.get(0);
  10.             String key2 = equation.get(1);
  11.             double value = values[i];
  12.            
  13.             if (graph1.containsKey(key1) == false) {
  14.                 graph1.put(key1, new ArrayList<>());
  15.             }
  16.             graph1.get(key1).add(key2);
  17.             if (graph1.containsKey(key2) == false) {
  18.                 graph1.put(key2, new ArrayList<>());
  19.             }
  20.             graph1.get(key2).add(key1);
  21.            
  22.             graph.put(get_key(key1, key2), value);
  23.             graph.put(get_key(key2, key1), 1 / value);
  24.         }
  25.        
  26.         double[] results = new double[queries.size()];
  27.         for (int i = 0; i < queries.size() ;i++) {
  28.             List<String> query = queries.get(i);
  29.             String start = query.get(0);
  30.             String end = query.get(1);
  31.            
  32.             String now = start;
  33.            
  34.             // System.out.printf("[%s ---> %s]%n", start, end);
  35.            
  36.             visited = new HashSet<>();
  37.             Double result = find(graph1, graph, now, end);
  38.             if (result != 0) {
  39.                 results[i] = result;
  40.             }else {
  41.                 results[i] = -1;
  42.             }
  43.         }
  44.        
  45.         return results;
  46.     }
  47.    
  48.     Set<String> visited = new HashSet<>();
  49.     public Double find(Map<String, List<String>> graph1, Map<String, Double> graph, String now, String end) {
  50.             if (graph1.containsKey(now) == false) {
  51.                 return 0d;
  52.             }
  53.             if (now.equals(end)) {
  54.                 return 1d;
  55.             }
  56.            
  57.             String from = now;
  58.             visited.add(now);
  59.             boolean is_end = true;
  60.             for (String to :graph1.get(now)) {
  61.                
  62.                 if (visited.contains(to)) continue;
  63.                 is_end = false;
  64.                
  65.                 Double value = graph.get(get_key(from, to));
  66.                 Double result_now = value * find(graph1, graph, to, end);
  67.                 if (result_now != 0) {
  68.                     return result_now;
  69.                 }
  70.                 // System.out.printf("(%s->%s:%f)%n", from, to, value);
  71.             }
  72.        
  73.             if (is_end) {
  74.                 if (now.equals(end) == false) {
  75.                     return 0d;
  76.                 }
  77.             }
  78.             visited.remove(now);
  79.  
  80.             return 0d;
  81.     }
  82.    
  83.     public String get_key(String from, String to) {
  84.         return from + "->" + to;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement