399. 除法求值
399. 除法求值
🟠 🔖 深度优先搜索
广度优先搜索
并查集
图
数组
最短路
🔗 LeetCode
题目
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.
Example 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:
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
Example 2:
Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
Output: [3.75000,0.40000,5.00000,0.20000]
Example 3:
Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
Output: [0.50000,2.00000,-1.00000,-1.00000]
Constraints:
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
作为已知条件,其中 equations[i] = [Ai, Bi]
和 values[i]
共同表示等式 Ai / Bi = values[i]
。每个 Ai
或 Bi
是一个表示单个变量的字符串。
另有一些以数组 queries
表示的问题,其中 queries[j] = [Cj, Dj]
表示第 j
个问题,请你根据已知条件找出 Cj / Dj = ?
的结果作为答案。
返回 所有问题的答案 。如果存在某个无法确定的答案,则用 -1.0
替代这个答案。如果问题中出现了给定的已知条件中没有出现的字符串,也需要用 -1.0
替代这个答案。
注意:输入总是有效的。你可以假设除法运算中不会出现除数为 0
的情况,且不存在任何矛盾的结果。
注意:未在等式列表中出现的变量是未定义的,因此无法确定它们的答案。
解题思路
这道题在考察 有向加权图 的遍历。
a/b=2
就相当于往图中添加了一条 a->b
权值为 2
的边,同时添加一条 b->a
权值为 1/2
的边。
queries
问 x/y
的值,相当于就是图中是否存在一条从 x
到 y
的路径,如果有,那么路径上所有边的权值相乘就是 x/y
的值。
所以思路就是,用邻接表建图,然后用 DFS 或者 BFS 遍历即可。
代码
/**
* @param {string[][]} equations
* @param {number[]} values
* @param {string[][]} queries
* @return {number[]}
*/
var calcEquation = function (equations, values, queries) {
// 把 equations 抽象成一幅图,邻接表存储
let graph = new Map();
for (let i = 0; i < equations.length; i++) {
let [start, end] = equations[i],
weight = values[i];
// 构建双向图
if (!graph.has(start)) {
graph.set(start, []);
}
graph.get(start).push({ node: end, weight });
if (!graph.has(end)) {
graph.set(end, []);
}
graph.get(end).push({ node: start, weight: 1 / weight });
}
// 依次求解 queries
let res = new Array(queries.lenghth);
for (let i = 0; i < queries.length; i++) {
const [start, end] = queries[i];
// BFS 遍历图,计算 start 到 end 的路径乘积
res[i] = dfs(graph, start, end);
}
return res;
};
var dfs = function (graph, start, end) {
// 不存在的节点,肯定无法到达
if (!graph.has(start) || !graph.has(end)) return -1.0;
if (start == end) return 1.0;
// BFS 标准框架
let queue = [start],
visited = new Set([start]),
// key 为节点变量名,value 记录从 start 到该节点的路径乘积
weight = new Map([[start, 1.0]]);
while (queue.length) {
let cur = queue.shift();
for (let item of graph.get(cur)) {
if (visited.has(item.node)) {
continue;
}
// 更新路径乘积
weight.set(item.node, weight.get(cur) * item.weight);
if (item.node == end) {
return weight.get(end);
}
// 记录 visited
visited.add(item.node);
// 新节点加入队列继续遍历
queue.push(item.node);
}
}
return -1.0;
};
相关题目
题号 | 标题 | 题解 | 标签 | 难度 |
---|---|---|---|---|
2307 | 检查方程中的矛盾之处 | 深度优先搜索 并查集 图 1+ |