You are given equations in the form of a / b = k
, where a
and b
are variables represented as strings, and k
is a floating-point number. Some equations are given to you as input, along with some queries, where each query is in the form of c / d
. Return the answers to all queries. If such a division cannot be determined, return -1.0.
equations = [["a", "b"], ["b", "c"]]
values = [2.0, 3.0]
queries = [["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"]]
[6.0, 0.5, -1.0, 1.0, -1.0]
a / b = k
, create an edge from a
to b
with weight k
, and an edge from b
to a
with weight 1/k
.-1.0
.["a", "a"]
should return 1.0
).-1.0
.from collections import defaultdict, deque
def calcEquation(equations, values, queries):
# Graph initialization
graph = defaultdict(dict)
# Building the graph
for (dividend, divisor), value in zip(equations, values):
graph[dividend][divisor] = value
graph[divisor][dividend] = 1 / value
# Function to perform BFS to find the result of a single query
def bfs(start, end):
if start not in graph or end not in graph:
return -1.0
queue = deque([(start, 1.0)])
visited = set()
while queue:
current, product = queue.popleft()
if current == end:
return product
visited.add(current)
for neighbor in graph[current]:
if neighbor not in visited:
queue.append((neighbor, product * graph[current][neighbor]))
return -1.0
# Process each query using the BFS function
results = []
for dividend, divisor in queries:
result = bfs(dividend, divisor)
results.append(result)
return results
# Example usage
equations = [["a", "b"], ["b", "c"]]
values = [2.0, 3.0]
queries = [["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"]]
print(calcEquation(equations, values, queries)) # Output: [6.0, 0.5, -1.0, 1.0, -1.0]
The overall time complexity for Q queries would thus be O(Q * (V + E))
. This should be efficient for typical constraints given by coding interview problems.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?