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.0.
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.
Note: The variables that do not occur in the list of equations are undefined, so the answer cannot be determined for them.
1 <= equations.length <= 20
equations[i].length == 2
1 <= Ai.length, Bi.length <= 5
values.length == equations.length
0.0 < values[i] <= 20.0
1 <= queries.length <= 20
queries[i].length == 2
1 <= Cj.length, Dj.length <= 5
Ai, Bi, Cj, Dj
consist of lower case English letters and digits.์ ๋ ฅ
equations | values | queries |
---|---|---|
[["a","b"],["b","c"]] | [2.0,3.0] | [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]] |
์ถ๋ ฅ
output |
---|
[6.00000,0.50000,-1.00000,1.00000,-1.00000] |
์ค๋ช
Given: a / b = 2.0, b / c = 3.0
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
note: x is undefined => -1.0
์ ๋ ฅ
equations | values | queries |
---|---|---|
[["a","b"],["b","c"],["bc","cd"]] | [1.5,2.5,5.0] | [["a","c"],["c","b"],["bc","cd"],["cd","bc"]] |
์ถ๋ ฅ
output |
---|
[3.75000,0.40000,5.00000,0.20000] |
์ ๋ ฅ
equations | values | queries |
---|---|---|
[["a","b"]] | [0.5] | [["a","b"],["b","a"],["a","c"],["x","y"]] |
์ถ๋ ฅ
output |
---|
[0.50000,2.00000,-1.00000,-1.00000] |
๋ฌธ์ ์ค๋ช ๋ง์ผ๋ก๋ ์ดํด๊ฐ ์ ์๋ผ์ ์ ์ถ๋ ฅ ์์๋ฅผ ์์ธํ ์ดํด๋ณด์๋ค.
์
์ถ๋ ฅ ์ #1์ ์ดํด๋ณด๋ฉด,
["a","b"]
์ value๋ 2.0
["b","c"]
์ value๋ 3.0์ผ๋ก ์ฃผ์ด์ก๋ค.
์ด๋ ์๋์ ๋ ๊ด๊ณ๊ฐ ์ฃผ์ด์ก๋ค๋ ์๋ฏธ์ด๋ค.
a / b = 2.0
b / c = 3.0
๊ทธ๋ฆฌ๊ณ ์
๋ ฅ์ queries
์ค ["a","c"]
์ ํด๋นํ๋ ์ถ๋ ฅ์ผ๋ก, a / c
๊ฐ ๋์์ผ ํ๋ค.
์ด ๋, a / c
๋ a / b
์ b / c
๋ฅผ ๊ณฑํ ๊ฐ์ผ๋ก, 6.0์ด ์ถ๋ ฅ๋๋ค.
๊ทธ๋ ๋ค๋ฉด queries
์ค ["b", "a"]
์ ์ถ๋ ฅ๊ฐ์ ์ด๋ป๊ฒ ๋ ๊น?
๋ฐ๋ก a / b
์ ์ญ์์ธ 0.5๊ฐ ์ถ๋ ฅ๋๋ค.
์ด๋ ๊ฒ ํ์ฑ๋ ๊ด๊ณ๋ฅผ ์๋์ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ๊ฐ์ค์น ๋ฐฉํฅ ๊ทธ๋ํ๋ก ๋ํ๋ผ ์ ์์๋ค.
์ด์ , ์ด ๊ทธ๋ํ์์ ์ํ๋ ์ถ๋ฐ์ง์ ๋์ฐฉ์ง ์ฌ์ด์ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ตฌํด ๊ฒฝ๋ก ์์ ์๋ ๊ฐ์ค์น๋ฅผ ๊ณฑํด์ฃผ๋ฉด ์ํ๋ ์ถ๋ ฅ๊ฐ์ ์ป์ ์ ์๋ค!
์ต๋จ ๊ฒฝ๋ก ์๊ณ ๋ฆฌ์ฆ์ผ๋ก๋ DFS๋ฅผ ์ฌ์ฉํด๋ณด์๋ค.
class Solution {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
// ๊ทธ๋ํ ์์ฑ
Map<String, Map<String, Double>> graph = new HashMap<>();
// ๊ทธ๋ํ ์
๋ ฅ
for (int i = 0; i < equations.size(); i++) {
String start = equations.get(i).get(0); // ์ถ๋ฐ์ง
String dest = equations.get(i).get(1); // ๋์ฐฉ์ง
double value = values[i]; // ๊ฐ์ค์น
// ์ถ๋ฐ์ง๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด ๋์ฐฉ์ง์ ๊ฐ์ค์น ์
๋ ฅ
graph.computeIfAbsent(start, k -> new HashMap<>()).put(dest, value);
// ๋์ฐฉ์ง๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด ์ถ๋ฐ์ง์ ๊ฐ์ค์น์ ์ญ์๋ฅผ ์
๋ ฅ
graph.computeIfAbsent(dest, k -> new HashMap<>()).put(start, 1 / value);
}
double[] answer = new double[queries.size()]; // ์ต์ข
์ถ๋ ฅ ๋ฐฐ์ด
for (int i = 0; i < queries.size(); i++) {
String start = queries.get(i).get(0); // queries์ ์ถ๋ฐ์ง ์ถ์ถ
String dest = queries.get(i).get(1); // queries์ ๋์ฐฉ์ง ์ถ์ถ
Set<String> visited = new HashSet<>(); // ๋ฐฉ๋ฌธ ์ฌ๋ถ Set ์์ฑ
answer[i] = dfs(graph, start, dest, visited); // dfs ํธ์ถ ํ ๋ฐํ๊ฐ ์ ์ฅ
}
return answer; // ์ต์ข
๊ฒฐ๊ณผ ๋ฐํ
}
// ๊ทธ๋ํ ํ์
public double dfs(Map<String, Map<String, Double>> graph, String start, String dest, Set<String> visited) {
if (!graph.containsKey(start)) {
return -1.0; // ๋ง์ฝ ์ถ๋ฐ์ง๊ฐ ๊ทธ๋ํ์ ์์ผ๋ฉด -1 ๋ฐํ
}
if (start.equals(dest)) {
return 1.0; // ์ถ๋ฐ์ง์ ๋์ฐฉ์ง๊ฐ ๊ฐ์ผ๋ฉด 1 ๋ฐํ
}
visited.add(start); // ๋ฐฉ๋ฌธ ์ฌ๋ถ ํ์
// ์ ์ ์ ์ฐ๊ฒฐ๋ ๋ชจ๋ ์ด์ ์ ์ ๊ณผ ๊ฐ์ค์น๋ฅผ ๊ฐ์ ธ์ด
for (Map.Entry<String, Double> neighbor : graph.get(start).entrySet()) {
// ๋ฐฉ๋ฌธํ์ง ์์์ ๊ฒฝ์ฐ
if (!visited.contains(neighbor.getKey())) {
// ํ์ฌ ์ ์ ์์ ๋์ฐฉ์ง๋ก ๊ฐ๋ ๊ฒฝ๋ก๋ฅผ ์ฐพ๊ธฐ ์ํด dfs ์ฌ๊ท ํธ์ถ
double result = dfs(graph, neighbor.getKey(), dest, visited);
// ๋์ฐฉ์ง์ ๋๋ฌํ ์ ์๋ ๊ฒฝ๋ก๊ฐ ๋ฐ๊ฒฌ๋ ๊ฒฝ์ฐ
if (result != -1.0) {
// ํด๋น ๊ฒฝ๋ก์ ๊ฐ์ค์น์ ํ์ฌ ์ด์ ์ ์ ์ผ๋ก ๊ฐ๋ ๊ฐ์ค์น๋ฅผ ๊ณฑํ์ฌ ๋ฐํ
return result * neighbor.getValue();
}
}
}
return -1.0; // ๊ฒฝ๋ก๋ฅผ ์ฐพ์ง ๋ชปํ์ ๊ฒฝ์ฐ -1.0 ๋ฐํ
}
}
์ ์ ์ ์ฐ๊ฒฐ๋ ์ด์ ์ ์ ๋ค์ ํ์ํ ๋, Map.Entry
๋ฅผ ์ฌ์ฉํ๋ Map์ ์ ์ฅ๋ ๋ชจ๋ key-value ์์ ๊ฐ์ key-value๋ฅผ ๊ฐ์ง๊ณ ์๋ ํ๋์ ๊ฐ์ฒด๋ก ์ป์ ์ ์๋ค๋ ์ฅ์ ์ด ์์๋ค.
์ด๋ ค์ด ๋ฌธ์ ๊ตฐ์...! ์ค๋๋ ๊ณ ์์ด ๋ง์์โบ๏ธ ๋ง์ง๋ง์ ํ๊ทน๊ธฐ ์ผ์ค๐