Equations are given in the format A / B = k
, where A
and B
are variables represented as strings, and k
is 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
, where equations.size() == values.size()
, and the values are positive. This represents the equations. Return vector<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"] ].
The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.
分析
这道题看起来很唬人,但是细细分析应该是一个典型的递归+回溯的题目。
我们先来分析一下例子,如果 a/b = 2, b/c = 3, 那怎么计算a/c呢?
首先我们需要将所有的关系式都记录下来,需要记录a/b = 2, b/a = 0.5, b/c=3, c/b = 0.33,为了记录这些关系我们需要使用map(string, vector<pair<string, value>>),key是被除数,value是除数和被除数/除数的结果。这样我们就把所有给定的关系都给记录下来了。
那么接下来如何找到a/c的结果?这个时候就需要体用递归+回溯了。我们用下表表示关系
a | b | c | |
a | 2 | ||
b | 0.5 | 3 | |
c | 0.33 |
我们从a开始寻找,遍历所有与a有关系的string,找到b,让后遍历所有与b有关系的string,直到找到c,为了防止遍历的过程中出现环,a->b->a,我们使用一个set记录已经便利过的点,如果在此次寻找中已经找过了就直接跳过。
看到其他人的方法,发现还有一种更为简单和直观的解法: Floyd算法。如上表所示,我们已经列出了已知条件中关系,我们可以通过遍历中间点,算出任意两两之间的关系。
Code
递归+回溯
class Solution {
public:
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
map<string, vector<pair<string, double>>> grid;
vector<double> res;
for (int i = 0; i < equations.size(); i ++)
{
string a = equations[i].first;
string b = equations[i].second;
double value = values[i];
if (grid.find(a) == grid.end())
{
grid.insert(make_pair(a, vector<pair<string, double>>()));
}
if (grid.find(b) == grid.end())
{
grid.insert(make_pair(b, vector<pair<string, double>>()));
}
grid[a].push_back(make_pair(b, value));
grid[b].push_back(make_pair(a, 1/value));
}
for (int i = 0; i < queries.size(); i ++)
{
string a = queries[i].first;
string b = queries[i].second;
set<string> touch;
touch.insert(a);
res.push_back(calc(grid, 1.0, a, b, touch));
}
return res;
}
double calc(map<string, vector<pair<string, double>>>& grid, double prefix,
string a, string b, set<string> touch)
{
if (grid.find(a) == grid.end() || grid.find(b) == grid.end())
return -1.0;
if (a == b)
return prefix;
vector<pair<string, double>> g = grid[a];
for (int i = 0; i < g.size(); i ++)
{
string tmpS = g[i].first;
double tmpValue = g[i].second;
if (touch.find(tmpS) != touch.end())
continue;
touch.insert(tmpS);
double ret = calc(grid, prefix * tmpValue, tmpS, b, touch);
if (ret != -1.0)
{
return ret;
}
touch.erase(tmpS);
}
return -1.0;
}
};
Floyd算法
class Solution {
public:
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
map<string, map<string, double>> grid;
vector<double> res;
for (int i = 0; i < equations.size(); i ++)
{
string a = equations[i].first;
string b = equations[i].second;
double value = values[i];
grid[a][b] = value;
grid[b][a] = 1/value;
}
map<string, map<string, double>>::iterator k,m,n;
for (k = grid.begin(); k!= grid.end(); k++) // middle
for (m = grid.begin(); m!=grid.end(); m++)
for (n = grid.begin(); n !=grid.end(); n++)
{
if (grid[m->first].count(n->first) == 0 &&
grid[m->first].count(k->first) == 1 &&
grid[k->first].count(n->first) == 1)
{
grid[m->first][n->first] = m->second.at(k->first) *
k->second.at(n->first);
}
}
for (int i = 0; i < queries.size(); i ++)
{
string a = queries[i].first;
string b = queries[i].second;
if (grid.find(a) == grid.end() || grid.find(b) == grid.end()
|| grid[a].count(b) == 0)
{
res.push_back(-1.0);
continue;
}
res.push_back(grid[a][b]);
}
return res;
}
};
运行效率
递归+回溯
Runtime: 4 ms, faster than 100.00% of C++ online submissions for Evaluate Division.
Memory Usage: 9.9 MB, less than 8.17% of C++ online submissions for Evaluate Division.
Floyd
Runtime: 8 ms, faster than 24.88% of C++ online submissions for Evaluate Division.
Memory Usage: 9.4 MB, less than 86.54% of C++ online submissions for Evaluate Division.