-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate-division.py
More file actions
29 lines (23 loc) · 946 Bytes
/
evaluate-division.py
File metadata and controls
29 lines (23 loc) · 946 Bytes
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
class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
graph = defaultdict(list)
for i, edge in enumerate(equations):
graph[edge[0]].append((edge[1],values[i]))
graph[edge[1]].append((edge[0],1/values[i]))
def dfs(node,target,product):
if node not in graph or target not in graph:
return -1.0
if node not in visited:
visited.add(node)
if node==target:
return product
for nbr, val in graph[node]:
result = dfs(nbr,target,product*val)
if result != -1.0:
return result
return -1.0
answer = []
for a,b in queries:
visited = set()
answer.append(dfs(a,b,1))
return answer