-
Notifications
You must be signed in to change notification settings - Fork 0
/
Evaluate Division
45 lines (28 loc) · 1.15 KB
/
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
// LC: Evaluate Division
class Solution:
def DFS(self, src, des, visited, graph, curr):
if (src not in graph) or (des not in graph) or (src in visited):
return -1
if src == des:
return curr
visited.add(src)
for nbr in graph[src]:
prod = self.DFS(nbr[0], des, visited, graph, curr * nbr[1])
if prod != -1:
return prod
return -1
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
graph = defaultdict(list)
for i in range(len(values)):
u, v = equations[i] # u / v
val = values[i]
graph[u].append((v, val))
graph[v].append((u, 1 / val))
ans = []
# Each query is independent
for q in queries:
src, des = q[0], q[1]
visited = set()
ok = self.DFS(src, des, visited, graph, 1.0)
ans.append(ok)
return ans