-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path399.py
27 lines (25 loc) · 1.04 KB
/
399.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
class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
graphRep = defaultdict(dict)
for (var1, var2), value in zip(equations, values):
graphRep[var1][var2] = value
graphRep[var2][var1] = 1/value
def BFS(s,e):
if s not in graphRep or e not in graphRep:
return -1
queue = deque([(s,1.0)])
Vset = set()
while queue:
var, value = queue.popleft()
Vset.add(var)
if (var == e):
return value
for nebVar in graphRep[var]:
if nebVar not in Vset:
queue.append((nebVar, value*graphRep[var][nebVar]))
return -1
result = []
for q in queries:
temp = BFS(q[0], q[1])
result.append(temp)
return result