399 Evaluate Division
399. Evaluate Division
1. Question
Equations are given in the formatA / B = k, whereAandBare variables represented as strings, andkis a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return-1.0.
Example:
Givena / 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 ].
The input is:vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries, whereequations.size() == values.size(), and the values are positive. This represents the equations. Returnvector<double>.
According to the example above:
equations = [ ["a", "b"], ["b", "c"] ],
values = [2.0, 3.0],
queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ].2. Implementation
(1) Union Find
思路: 用union find的思路主要是将 a/b公式中的分母(在这里是b)作为分子的parent, 如果我们知道a/b, 和b/c的值,则要查询a/c时,我们只需要知道a和c在union find里的root, 如果他们有共同的root, 则有解,否则无解。我觉得union find的解法,关键是如何设置node的parent,通过代码可以发现,我们将node b设为node a的parent当:
a和b都不在图中,而b是equation中的分母,a是对应的分子
b已经出现在图中,而a不在
如果a和b都在图中,找出a和b对应的root,将a的root的parent设为b的root的parent
(2) DFS
3. Time & Space Complexity
(1) Union Find: 时间复杂度O(e + q), 创建union find需要O(e)时间, e是equations的长度, 每次query的需要O(1)时间,所以总的query时间是O(q), 两部分加起来是O(e + q), 空间复杂度O(e), 空间主要是创建Union Find时,用到的map需要node的个数
(2) DFS: 时间复杂度(e + q*e), O(e)
Last updated
Was this helpful?