You are given an array of variable pairs equations and an array of real numbers values,
where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i].
Each Ai or Bi is a string that represents a single variable.
You are also given some queries,
where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?.
Return the answers to all queries.
If a single answer cannot be determined, return -1.
두 변수의 쌍의 배열인 equations 와 배열 한 쌍 (a, b) 에 대해 a / b 의 값을 가지는 equations 배열이 있다.
두 배열을 이용해 queries 배열 내 변수 쌍의 나눗셈 결과를 배열로 리턴하시오.
만약 나눗셈이 불가능하다면 -1을 리턴하시오.
Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000]
Explanation:
a/b 는 2.0이고, b/c 는 3.0 이다.
[6.0, 0.5, -1.0, 1.0, -1.0] 가 쿼리의 결과가 된다.
x는 등장한 적이 없기에 그냥 -1.0 이 된다.
from typing import Tuple, Dict, Set
class Node:
def __init__(self, name: str):
self.name = name
self.edges: List[Tuple[float, Node]] = []
def add_edges(self, to: 'Node', value: float):
self.edges.append((value, to))
class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
self.graph: Dict[str, Node] = {}
for f, s in equations:
if f not in self.graph:
self.graph[f] = Node(f)
if s not in self.graph:
self.graph[s] = Node(s)
for i, (f, s) in enumerate(equations):
self.graph[f].add_edges(self.graph[s], values[i])
self.graph[s].add_edges(self.graph[f], 1.0 / values[i])
self.check: Set[str]
def dfs(start: str, end: str, value: float = 1.0):
if start not in self.check:
return -2
else:
self.check.remove(start)
if start == end:
return value
ans = []
for next_node in self.graph[start].edges:
ans.append(dfs(next_node[1].name, end, value * next_node[0]))
return max(ans)
ans: List[float] = []
for f, s in queries:
self.check = set(self.graph.keys())
ans.append(max(dfs(f, s), -1))
return ans