-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathEvaluate Division.py
35 lines (26 loc) · 1.09 KB
/
Evaluate Division.py
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
class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
graph = dict()
for (n, d), v in zip(equations, values):
if n not in graph:
graph[n] = []
if d not in graph:
graph[d] = []
graph[n].append((d, v))
graph[d].append((n, 1/v))
def dfs(node, target, product, visited):
if n not in graph or d not in graph:
return -1
if node == target:
return product
visited.add(node)
for neighbor, quotient in graph[node]:
if neighbor not in visited:
soln = dfs(neighbor, target, product * quotient, visited)
if soln != -1:
return soln
return -1
solns = []
for n, d in queries:
solns.append(dfs(n, d, 1, set()))
return solns