-
Notifications
You must be signed in to change notification settings - Fork 0
/
399. Evaluate Division
67 lines (55 loc) · 2.2 KB
/
399. Evaluate Division
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
public class Solution {
public double[] CalcEquation(IList<IList<string>> equations, double[] values, IList<IList<string>> queries) {
// initialize a dictionary for storing equations variables
// for numerator -> store value and denominator
// for denominator -> store 1/value and numerator
var result = new double[queries.Count];
var graph = new Dictionary<string, Dictionary<string, double>>();
for(int i =0; i< equations.Count; i++)
{
var numer = equations[i][0];
var denom = equations[i][1];
if(!graph.ContainsKey(numer))
graph.Add(numer, new Dictionary<string, double>());
if(!graph.ContainsKey(denom))
graph.Add(denom, new Dictionary<string, double>());
graph[numer][denom] = values[i];
graph[denom][numer] = 1/values[i];
}
// for queries
// if a query variable doesn't exist => -1
// else dfs on numerator to denominator relations
for(int i =0; i< queries.Count; i++)
{
string num = queries[i][0];
string den = queries[i][1];
if(graph.ContainsKey(num) && graph.ContainsKey(den))
{
var visited = new HashSet<string>();
result[i]=dfs(num, den, 1.0, visited);
}
else
result[i] = -1.0;
}
return result;
double dfs(string start, string end, double product, HashSet<string> visited )
{
if(start == end )
return product;
visited.Add(start);
foreach(var nei in graph[start]) //a-b // b-c
{
if (!visited.Contains(nei.Key))
{
var result = dfs( nei.Key, end, product* nei.Value, visited);
if (result != -1.0)
return result;
// the thing here is that I want to continue looping/searching for
// a result if current neighbor didn't evaluate (gave -1.0)
// and I don't want to multiply the -1.0(not evaluable ) to the result
}
}
return -1;
}
}
}