-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path399. Evaluate Division
63 lines (42 loc) · 1.69 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
from collections import deque
class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
self.graph = {}
self.weight = {}
for i in range(len(equations)):
[u,v] = equations[i]
if u in self.graph:
self.graph[u].add(v)
else:
self.graph[u] = {v}
if v in self.graph:
self.graph[v].add(u)
else:
self.graph[v] = {u}
self.weight[u+"-"+v] = values[i]
self.weight[v+"-"+u] = 1/values[i]
ans = []
for query in queries:
[x,y] = query
if x not in self.graph:
ans.append(-1)
continue
if x+"-"+y in self.weight:
ans.append(self.weight[x+"-"+y])
continue
self.visited = set()
ans.append(self.bfs(x,y,1) if x != y else 1.0)
return ans
def bfs(self, x: str,y: str,val: float) -> float:
self.visited.add(x)
q = deque([(val,x)])
while q:
(val,x) = q.popleft()
if x+"-"+y in self.weight:
return val*self.weight[x+"-"+y]
for node in self.graph[x]:
if node not in self.visited:
newval = val * self.weight[x+"-"+node]
q.append((newval,node))
self.visited.add(node)
return -1