Skip to content

Commit 535dcc6

Browse files
committed
Add medium problem.
1 parent 063228d commit 535dcc6

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Leetcode/medium/evaluate-division.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
# EVALUATE DIVISION
3+
4+
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.
5+
6+
You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?.
7+
8+
Return the answers to all queries. If a single answer cannot be determined, return -1.0.
9+
10+
Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.
11+
12+
Example 1:
13+
14+
Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
15+
Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000]
16+
Explanation:
17+
Given: a / b = 2.0, b / c = 3.0
18+
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
19+
return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
20+
21+
Example 2:
22+
23+
Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
24+
Output: [3.75000,0.40000,5.00000,0.20000]
25+
Example 3:
26+
27+
Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
28+
Output: [0.50000,2.00000,-1.00000,-1.00000]
29+
30+
Constraints:
31+
32+
1 <= equations.length <= 20
33+
equations[i].length == 2
34+
1 <= Ai.length, Bi.length <= 5
35+
values.length == equations.length
36+
0.0 < values[i] <= 20.0
37+
1 <= queries.length <= 20
38+
queries[i].length == 2
39+
1 <= Cj.length, Dj.length <= 5
40+
Ai, Bi, Cj, Dj consist of lower case English letters and digits.
41+
"""
42+
43+
class Solution:
44+
def calcEquation(self, equations, values, queries):
45+
graph = {}
46+
for i in range(len(equations)):
47+
num, den = equations[i]
48+
ans = values[i]
49+
if num not in graph:
50+
graph[num] = {num: 1}
51+
graph[num][den] = ans
52+
53+
if den not in graph:
54+
graph[den] = {den: 1}
55+
graph[den][num] = (1 / ans)
56+
57+
results = []
58+
for n, d in queries:
59+
visited = set()
60+
visited.add(n)
61+
results.append(self.dfs(n, d, 1, visited, graph))
62+
63+
return results
64+
65+
def dfs(self, num, den, value, visited, graph):
66+
if num not in graph:
67+
return -1.0
68+
69+
possibleValues = graph[num]
70+
if den in possibleValues:
71+
return value * possibleValues[den]
72+
73+
for item in possibleValues:
74+
if item in visited:
75+
continue
76+
visited.add(item)
77+
check = self.dfs(item, den, value * possibleValues[item], visited, graph)
78+
if check != -1.0:
79+
return check
80+
visited.remove(item)
81+
return -1.0

0 commit comments

Comments
 (0)