|
1 | 1 | classSolution {
|
2 |
| -publicdouble[]calcEquation(List<List<String>>equations,double[]values,List<List<String>>queries) { |
3 |
| -double[]ans =newdouble[queries.size()]; |
4 |
| - |
5 |
| -Map<String,Set<String>>map =newHashMap<>(); |
6 |
| -Map<String,Double>resMap =newHashMap<>(); |
7 |
| - |
8 |
| -for (inti =0;i <equations.size();i++) { |
9 |
| -Stringoperand1 =equations.get(i).get(0); |
10 |
| -Stringoperand2 =equations.get(i).get(1); |
11 |
| - |
12 |
| -map.computeIfAbsent(operand1,k ->newHashSet<>()).add(operand2); |
13 |
| -map.computeIfAbsent(operand2,k ->newHashSet<>()).add(operand1); |
14 |
| - |
15 |
| -resMap.put(operand1 +"|" +operand2,values[i]); |
16 |
| - } |
17 |
| - |
18 |
| -for (inti =0;i <queries.size();i++) { |
19 |
| -Stringoperand1 =queries.get(i).get(0); |
20 |
| -Stringoperand2 =queries.get(i).get(1); |
21 |
| - |
22 |
| -if (!map.containsKey(operand1) || !map.containsKey(operand2)) { |
23 |
| -ans[i] = -1.0; |
24 |
| - } |
25 |
| -elseif (operand1.equals(operand2)) { |
26 |
| -ans[i] =1.0; |
27 |
| - } |
28 |
| -elseif (map.get(operand1).contains(operand2)) { |
29 |
| -ans[i] =getSimpleDivisionVal(resMap,operand1,operand2); |
30 |
| - } |
31 |
| -elseif (operand2.contains(operand1)) { |
32 |
| -ans[i] =getSimpleDivisionVal(resMap,operand2,operand1); |
33 |
| - } |
34 |
| -else { |
35 |
| -ans[i] =dfs(map,resMap,operand1,operand2,1.0,newHashSet<>()); |
36 |
| - } |
37 |
| - } |
38 |
| - |
39 |
| -returnans; |
| 2 | +publicdouble[]calcEquation(List<List<String>>equations,double[]values,List<List<String>>queries) { |
| 3 | +Map<String,List<String>>map =newHashMap<>(); |
| 4 | +Map<String,List<Double>>valueMap =newHashMap<>(); |
| 5 | +for (inti =0;i <equations.size();i++) { |
| 6 | +List<String>equation =equations.get(i); |
| 7 | +map.computeIfAbsent(equation.get(0),k ->newArrayList<>()).add(equation.get(1)); |
| 8 | +map.computeIfAbsent(equation.get(1),k ->newArrayList<>()).add(equation.get(0)); |
| 9 | +valueMap.computeIfAbsent(equation.get(0),k ->newArrayList<>()).add(values[i]); |
| 10 | +valueMap.computeIfAbsent(equation.get(1),k ->newArrayList<>()).add(1 /values[i]); |
40 | 11 | }
|
41 |
| - |
42 |
| -privatedoubledfs (Map<String,Set<String>>map,Map<String,Double>resMap,Stringoperand1,Stringoperand2, |
43 |
| -doublebase,Set<String>visited) { |
44 |
| -if (visited.contains(operand1)) { |
45 |
| -return -1.0; |
46 |
| - } |
47 |
| -if (map.get(operand1).contains(operand2)) { |
48 |
| -returnbase *getSimpleDivisionVal(resMap,operand1,operand2); |
49 |
| - } |
50 |
| - |
51 |
| -visited.add(operand1); |
52 |
| - |
53 |
| -doubleans = -1.0; |
54 |
| -Iterator<String>children =map.get(operand1).iterator(); |
55 |
| - |
56 |
| -while (children.hasNext()) { |
57 |
| -Stringchild =children.next(); |
58 |
| -if (!visited.contains(child)) { |
59 |
| -ans =Math.max(ans,dfs(map,resMap,child,operand2,base *getSimpleDivisionVal(resMap,operand1,child),visited)); |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| -returnans; |
| 12 | +double[]ans =newdouble[queries.size()]; |
| 13 | +for (inti =0;i <queries.size();i++) { |
| 14 | +List<String>query =queries.get(i); |
| 15 | +ans[i] =dfs(map,valueMap,query.get(0),query.get(1),newHashSet<>(),1.0); |
| 16 | +ans[i] =ans[i] ==0.0 ? -1.0 :ans[i]; |
64 | 17 | }
|
65 |
| - |
66 |
| -privatedoublegetSimpleDivisionVal (Map<String,Double>resMap,Stringoperand1,Stringoperand2) { |
67 |
| -if (resMap.containsKey(operand1 +"|" +operand2)) { |
68 |
| -returnresMap.get(operand1 +"|" +operand2); |
69 |
| - } |
70 |
| - |
71 |
| -return1 /resMap.get(operand2 +"|" +operand1); |
| 18 | +returnans; |
| 19 | + } |
| 20 | + |
| 21 | +privatedoubledfs(Map<String,List<String>>map,Map<String,List<Double>>valueMap,Stringa,Stringb,Set<String>set,doublecurr) { |
| 22 | +if (set.contains(a)) { |
| 23 | +return0.0; |
72 | 24 | }
|
| 25 | +if (!map.containsKey(a)) { |
| 26 | +return0.0; |
| 27 | + } |
| 28 | +if (a.equals(b)) { |
| 29 | +returncurr; |
| 30 | + } |
| 31 | +set.add(a); |
| 32 | +List<String>children =map.get(a); |
| 33 | +List<Double>valueList =valueMap.get(a); |
| 34 | +doubletemp =0.0; |
| 35 | +for (inti =0;i <children.size();i++) { |
| 36 | +temp =dfs(map,valueMap,children.get(i),b,set,curr *valueList.get(i)); |
| 37 | +if (temp !=0.0) { |
| 38 | +break; |
| 39 | + } |
| 40 | + } |
| 41 | +set.remove(a); |
| 42 | +returntemp; |
| 43 | + } |
73 | 44 | }
|