@@ -21,30 +21,46 @@ public static void main (String[] args) throws java.lang.Exception
21
21
22
22
23
23
public double [] calcEquation (String [][] equations , double [] values , String [][] queries ) {
24
- HashMap <String , HashMap <String , Double >> hm = new HashMap <String , HashMap <String , Double >>();
24
+ HashMap <String , ArrayList <String >> pairs = new HashMap <String , ArrayList <String >>();
25
+ HashMap <String , ArrayList <Double >> weights = new HashMap <String , ArrayList <Double >>();
26
+
25
27
for (int i = 0 ; i < equations .length ; i ++) {
26
- String [] strs = equations [i ];
27
- if (!hm .containsKey (strs [0 ])) {
28
- HashMap <String , Double > sub = new HashMap <String , Double >();
29
- hm .put (strs [0 ], sub );
28
+ if (!pairs .containsKey (equations [i ][0 ])) {
29
+ pairs .put (equations [i ][0 ], new ArrayList <String >());
30
+ weights .put (equations [i ][0 ], new ArrayList <Double >());
30
31
}
31
- hm .get (strs [0 ]).put (strs [1 ], values [i ]);
32
- if (!hm .containsKey (strs [1 ])) {
33
- HashMap <String , Double > sub = new HashMap <String , Double >();
34
- hm .put (strs [1 ], sub );
32
+ if (!pairs .containsKey (equations [i ][1 ])) {
33
+ pairs .put (equations [i ][1 ], new ArrayList <String >());
34
+ weights .put (equations [i ][1 ], new ArrayList <Double >());
35
35
}
36
- hm .get (strs [1 ]).put (strs [1 ], 1 /values [i ]);
36
+ pairs .get (equations [i ][0 ]).add (equations [i ][1 ]);
37
+ weights .get (equations [i ][0 ]).add (values [i ]);
38
+ pairs .get (equations [i ][1 ]).add (equations [i ][0 ]);
39
+ weights .get (equations [i ][1 ]).add (1 /values [i ]);
37
40
}
38
41
double [] results = new double [queries .length ];
39
42
for (int i = 0 ; i < queries .length ; i ++) {
40
- if (hm .containsKey (queries [i ][0 ])) {
41
- HashMap <String , Double > sub = hm .get (queries [i ][0 ]);
42
- if (sub .containsKey (queries [i ][1 ])) results [i ] = sub .get (queries [i ][1 ]);
43
- else results [i ] = -1.0 ;
44
- } else {
45
- results [i ] = -1.0 ;
46
- }
43
+ double retVal = dfs (queries [i ][0 ], queries [i ][1 ], 1.0 , pairs , weights , new HashSet <String >());
44
+ if (retVal == 0.0 ) results [i ] = -1.0 ; else results [i ] = retVal ;
47
45
}
48
46
return results ;
49
- }
47
+ }
48
+ public double dfs (String startNode , String endNode , double value , HashMap <String , ArrayList <String >> pairs ,
49
+ HashMap <String , ArrayList <Double >> weights , HashSet <String > visited ) {
50
+ if (visited .contains (startNode )) return 0.0 ;
51
+ if (!pairs .containsKey (startNode )) return 0.0 ;
52
+ if (startNode .equals (endNode )) return value ;
53
+ List <String > toNodes = pairs .get (startNode );
54
+ visited .add (startNode );
55
+ double retVal = 0.0 ;
56
+ for (int i = 0 ; i < toNodes .size (); i ++) {
57
+ String next = toNodes .get (i );
58
+ retVal = dfs (next , endNode , value * weights .get (startNode ).get (i ), pairs , weights , visited );
59
+ if (retVal != 0.0 ) {
60
+ break ;
61
+ }
62
+ }
63
+ visited .remove (startNode ); // remove because there might be a case of a/b = 3.0 and c/b = 2.0
64
+ return retVal ;
65
+ }
50
66
}
0 commit comments