Skip to content

Commit c6eec46

Browse files
committed
leetcode.com 399. Evaluate Division
문제 링크: https://leetcode.com/problems/evaluate-division
1 parent 6743bbb commit c6eec46

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
6+
dicts = {}
7+
8+
for i in range(len(equations)):
9+
d1, d2 = equations[i]
10+
v = values[i]
11+
12+
if d1 not in dicts.keys():
13+
dicts[d1] = {}
14+
15+
dicts[d1][d2] = v
16+
dicts[d1][d1] = 1
17+
18+
if d2 not in dicts.keys():
19+
dicts[d2] = {}
20+
21+
dicts[d2][d1] = 1 / v
22+
dicts[d2][d2] = 1
23+
24+
answer = []
25+
26+
for q1, q2 in queries:
27+
rtn = self.direct(dicts, q1, q2)
28+
29+
if None != rtn:
30+
answer.append(rtn)
31+
continue
32+
33+
rtn = self.dfs(dicts, q1, q2, 1, [])
34+
answer.append(rtn)
35+
36+
return answer
37+
38+
def direct(self, dicts, q1, q2):
39+
if q1 in dicts.keys():
40+
if q2 in dicts[q1].keys():
41+
return dicts[q1][q2]
42+
43+
return None
44+
45+
def dfs(self, dicts, bgn, end, v, visited: List[str]):
46+
if bgn not in dicts.keys():
47+
return -1
48+
49+
for mid in dicts[bgn].keys():
50+
if mid not in visited:
51+
if mid == end:
52+
v *= dicts[bgn][mid]
53+
return v
54+
else:
55+
rtn = self.dfs(dicts, mid, end, v * dicts[bgn][mid], visited + [bgn])
56+
if rtn != -1:
57+
return rtn
58+
59+
return -1
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from unittest import TestCase
2+
from main import Solution
3+
4+
5+
class TestSolution(TestCase):
6+
def test1_calc_equation(self):
7+
sln = Solution()
8+
self.assertEqual(
9+
[6.00000, 0.50000, -1.00000, 1.00000, -1.00000],
10+
sln.calcEquation(
11+
[["a", "b"], ["b", "c"]], [2.0, 3.0], [["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"]]
12+
)
13+
)
14+
15+
def test2_calc_equation(self):
16+
sln = Solution()
17+
self.assertEqual(
18+
[3.75000, 0.40000, 5.00000, 0.20000],
19+
sln.calcEquation(
20+
equations=[["a", "b"], ["b", "c"], ["bc", "cd"]], values=[1.5, 2.5, 5.0], queries=[["a", "c"], ["c", "b"], ["bc", "cd"], ["cd", "bc"]]
21+
)
22+
)
23+
24+
def test3_calc_equation(self):
25+
sln = Solution()
26+
self.assertEqual(
27+
[0.50000, 2.00000, -1.00000, -1.00000],
28+
sln.calcEquation(
29+
equations=[["a", "b"]], values=[0.5], queries=[["a", "b"], ["b", "a"], ["a", "c"], ["x", "y"]]
30+
)
31+
)
32+
33+
def test18_calc_equation(self):
34+
sln = Solution()
35+
self.assertEqual(
36+
[360.00000, 0.00833, 20.00000, 1.00000, -1.00000, -1.00000],
37+
sln.calcEquation(
38+
equations=[["x1", "x2"], ["x2", "x3"], ["x3", "x4"], ["x4", "x5"]],
39+
values=[3.0, 4.0, 5.0, 6.0],
40+
queries=[["x1", "x5"], ["x5", "x2"], ["x2", "x4"], ["x2", "x2"], ["x2", "x9"], ["x9", "x9"]]
41+
)
42+
)

0 commit comments

Comments
 (0)