-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path0399-evaluate-division.py
49 lines (41 loc) · 1.67 KB
/
0399-evaluate-division.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# time complexity: O(m*n)
# space complexity: O(n)
from collections import defaultdict
from typing import List
class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
graph = defaultdict(defaultdict)
def backtrack(currNode: str, targetNode: str, accProduct: int, visited: set):
visited.add(currNode)
ret = -1.0
neighbors = graph[currNode]
if targetNode in neighbors:
ret = accProduct * neighbors[targetNode]
else:
for neighbor, value in neighbors.items():
if neighbor in visited:
continue
ret = backtrack(neighbor, targetNode,
accProduct * value, visited)
if ret != -1.0:
break
visited.remove(currNode)
return ret
for (dividend, divisor), value in zip(equations, values):
graph[dividend][divisor] = value
graph[divisor][dividend] = 1 / value
results = []
for dividend, divisor in queries:
if dividend not in graph or divisor not in graph:
ret = -1.0
elif dividend == divisor:
ret = 1.0
else:
visited = set()
ret = backtrack(dividend, divisor, 1, visited)
results.append(ret)
return results
equations = [["a", "b"], ["b", "c"]]
values = [2.0, 3.0]
queries = [["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"]]
print(Solution().calcEquation(equations, values, queries))