-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathEvaluateDivision.java
148 lines (124 loc) · 4.73 KB
/
EvaluateDivision.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Author: Shobhit Behl (LC: shobhitbruh)
class Solution {
public double[] calcEquation(List<List<String>> e, double[] v, List<List<String>> q) {
HashMap<String,HashMap<String,Double>> hm=new HashMap<>();
for(int i=0;i<e.size();i++){
if(!hm.containsKey(e.get(i).get(0))){
HashMap<String,Double> al=new HashMap<>();
al.put(e.get(i).get(1),v[i]);
hm.put(e.get(i).get(0),al);
hm.get(e.get(i).get(0)).put(e.get(i).get(0),1.0);
}else{
hm.get(e.get(i).get(0)).put(e.get(i).get(1),v[i]);
}
if(!hm.containsKey(e.get(i).get(1))){
HashMap<String,Double> al=new HashMap<>();
al.put(e.get(i).get(0),(1/v[i]));
hm.put(e.get(i).get(1),al);
hm.get(e.get(i).get(1)).put(e.get(i).get(1),1.0);
}else{
hm.get(e.get(i).get(1)).put(e.get(i).get(0),(1/v[i]));
}
}
double[] ans=new double[q.size()];
for(int i=0;i<q.size();i++){
boolean y=false;
if(!hm.containsKey(q.get(i).get(0))||!hm.containsKey(q.get(i).get(1))){
ans[i]=-1.0;
}else{
HashMap<String,Double> A=hm.get(q.get(i).get(0));
HashMap<String,Double> B=hm.get(q.get(i).get(1));
for(String xy:A.keySet()){
if(B.containsKey(xy)){
ans[i]=A.get(xy)/B.get(xy);
break;
}else if(!q.get(i).get(0).equals(xy)){
HashSet<String> visited=new HashSet<>();
double u=dfs(q.get(i).get(0),q.get(i).get(1),(1/A.get(xy)),xy,hm,visited);
ans[i]=u;
if(u>=0.0){
break;
}
}
}
}
}
return ans;
}
public double dfs(String st,String en,double val,String intr,HashMap<String,HashMap<String,Double>> hm,HashSet<String> visited){
HashMap<String,Double> x=hm.get(intr);
visited.add(intr);
for(String k:x.keySet()){
if(visited.contains(k)){
continue;
}
if(k.equals(en)){
return x.get(k)/val;
}
double u=dfs(st,en,(val/x.get(k)),k,hm,visited);
if(u>=0.0){
return u;
}
}
return -1.0;
}
}
class Solution {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
Map<String, Map<String, Double>> graph = buildGraph(equations, values);
double[] ans = new double[queries.size()];
int i = 0;
for(List<String> query : queries){
Set<String> visited = new HashSet<String>();
if(query.get(0).equals(query.get(1))){
if(graph.containsKey(query.get(0))){
ans[i] = 1.0;
i++;
} else{
ans[i] = -1.0;
i++;
}
continue;
}
double res = dfs(query.get(0), query.get(1), graph, visited);
ans[i] = res;
i++;
}
return ans;
}
private double dfs(String start, String end, Map<String, Map<String, Double>> graph, Set<String> visited) {
if(!graph.containsKey(start)) {
return -1.0;
}
if(graph.get(start).containsKey(end)) {
return graph.get(start).get(end);
}
visited.add(start);
Map<String, Double> startMap = graph.get(start);
for(Map.Entry<String, Double> entry: startMap.entrySet()) {
if(!visited.contains(entry.getKey())){
double currVal = dfs(entry.getKey(), end, graph, visited);
if(currVal!=-1){
return currVal * entry.getValue();
}
}
}
return -1.0;
}
private Map<String, Map<String, Double>> buildGraph(List<List<String>> equations, double[] values) {
// source, <End, Value>
Map<String, Map<String, Double>> graph = new HashMap<>();
int i = 0;
for(List<String> equation : equations) {
String startEl = equation.get(0);
String endEl = equation.get(1);
double val = values[i];
i++;
graph.putIfAbsent(startEl, new HashMap<>());
graph.get(startEl).put(endEl, val);
graph.putIfAbsent(endEl, new HashMap<>());
graph.get(endEl).put(startEl, 1/val);
}
return graph;
}
}