class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
adj = collections.defaultdict(list) # Map a -> list of [b, a/b]
for i, eq in enumerate(equations):
a, b = eq
adj[a].append((b, values[i]))
adj[b].append((a, 1 / values[i]))
def bfs(src, target):
if src not in adj or target not in adj:
return -1
q, visit = deque([(src, 1)]), set()
visit.add(src)
while q:
node, w = q.popleft()
if node == target:
return w
for nei, weight in adj[node]:
if nei not in visit:
q.append((nei, w * weight))
visit.add(nei)
return -1
return [bfs(q[0], q[1]) for q in queries]Where is the number of unique strings and is the number of queries.
class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
adj = collections.defaultdict(list) # Map a -> list of [b, a/b]
for i, eq in enumerate(equations):
a, b = eq
adj[a].append((b, values[i]))
adj[b].append((a, 1 / values[i]))
def dfs(src, target, visited):
if src not in adj or target not in adj:
return -1
if src == target:
return 1
visited.add(src)
for nei, weight in adj[src]:
if nei not in visited:
result = dfs(nei, target, visited)
if result != -1:
return weight * result
return -1
return [dfs(q[0], q[1], set()) for q in queries]Where is the number of unique strings and is the number of queries.
class UnionFind:
def __init__(self):
self.parent = {}
self.weight = {}
def add(self, x):
if x not in self.parent:
self.parent[x] = x
self.weight[x] = 1.0
def find(self, x):
if x != self.parent[x]:
orig_parent = self.parent[x]
self.parent[x] = self.find(self.parent[x])
self.weight[x] *= self.weight[orig_parent]
return self.parent[x]
def union(self, x, y, value):
self.add(x)
self.add(y)
root_x = self.find(x)
root_y = self.find(y)
if root_x != root_y:
self.parent[root_x] = root_y
self.weight[root_x] = value * self.weight[y] / self.weight[x]
def get_ratio(self, x, y):
if x not in self.parent or y not in self.parent or self.find(x) != self.find(y):
return -1.0
return self.weight[x] / self.weight[y]
class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
uf = UnionFind()
for (a, b), value in zip(equations, values):
uf.union(a, b, value)
return [uf.get_ratio(a, b) for a, b in queries]Where is the number of unique strings and is the number of queries.
class Solution:
def calcEquation(self, equations, values, queries):
graph = collections.defaultdict(dict)
for (a, b), value in zip(equations, values):
graph[a][b] = value
graph[b][a] = 1.0 / value
for k in graph:
for i in graph[k]:
for j in graph[k]:
if j not in graph[i]:
graph[i][j] = graph[i][k] * graph[k][j]
result = []
for a, b in queries:
if a in graph and b in graph[a]:
result.append(graph[a][b])
else:
result.append(-1.0)
return resultWhere is the number of unique strings and is the number of queries.