399.Evaluate Division
Description
Equations are given in the formatA / B = k
, whereA
andB
are variables represented as strings, andk
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
, 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"] ].
The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.
Discussion
method 1 & 2
bfs & dfs + hash map
code
// BFS
class Solution {
public:
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
vector<double> rst;
if(0 == equations.size() || 0 == queries.size() || equations.size() != values.size())
return rst;
unordered_multimap<string, tuple<string, string, double>> tracker;
//unordered_map<pair<string, string>, double> tracker_2;
for(int loop = 0; loop < equations.size(); ++loop) {
tracker.insert(make_pair(equations[loop].first, make_tuple(equations[loop].first, equations[loop].second, values[loop])));
//tracker_2.insert(equations[loop], values[loop]);
if(values[loop] != 0) {
tracker.insert(make_pair(equations[loop].second, make_tuple(equations[loop].second, equations[loop].first, 1 / values[loop])));
//tracker_2.insert(make_pair(equation[loop].second, equation[loop].first), 1/values[loop]);
}
}
for(auto query : queries) {
double tmp = 0;
if(true == findQuery(tracker, query, tmp)) {
rst.push_back(tmp);
}
else {
rst.push_back(-1);
}
}
return rst;
}
private:
bool findQuery(unordered_multimap<string, tuple<string, string, double>> &tracker, pair<string, string>& query, double &tmp) {
bool found = false;
queue<tuple<string, string, double>> q;
auto range = tracker.equal_range(query.first);
//cout << "find: " << query.first << ", " << query.second << endl;
for(auto iter = range.first; iter != range.second; ++iter) {
if(iter == range.first && query.first == query.second) {
tmp = 1;
found = true;
break;
}
if(get<1>(iter->second) == query.second) {
tmp = get<2>(iter->second);
found = true;
break;
}
else {
q.push(iter->second);
}
}
//cout << "test" << endl;
while(!q.empty() && false == found) {
auto next = q.front();
q.pop();
//cout << "map char:" << get<1>(next) << endl;
range = tracker.equal_range(get<1>(next));
for(auto iter = range.first; iter != range.second; ++iter) {
//cout << "pair:" << get<0>(next) << ", " << get<1>(next) << ">" << get<0>(iter->second) << "," << get<1>(iter->second) << "," << get<2>(iter->second) << endl;
if(get<0>(next) == get<1>(iter->second))
continue;
double value = get<2>(next) * get<2>(iter->second);
if(get<1>(iter->second) == query.second) {
//cout << "break?" << endl;
tmp = value;
found = true;
break;
}
else {
q.push(make_tuple(get<0>(iter->second), get<1>(iter->second), value));
// cannot update tracker, since iterator is under used.
/*
tracker.insert(make_pair(get<0>(next), make_tuple(get<0>(next), get<1>(iter->second), value)));
if(value != 0) {
tracker.insert(make_pair(get<1>(next), make_tuple(get<1>(next), get<0>(iter->second), 1 / value)));
}
*/
}
}
//cout << "test2" << endl;
}
return found;
}
};
method 3
union find (copy from leetcode discussion)
class Solution {
public:
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
unordered_map<string, Node*> map;
vector<double> res;
for (int i = 0; i < equations.size(); i ++) {
string s1 = equations[i].first, s2 = equations[i].second;
if (map.count(s1) == 0 && map.count(s2) == 0) {
map[s1] = new Node();
map[s2] = new Node();
map[s1] -> value = values[i];
map[s2] -> value = 1;
map[s1] -> parent = map[s2];
} else if (map.count(s1) == 0) {
map[s1] = new Node();
map[s1] -> value = map[s2] -> value * values[i];
map[s1] -> parent = map[s2];
} else if (map.count(s2) == 0) {
map[s2] = new Node();
map[s2] -> value = map[s1] -> value / values[i];
map[s2] -> parent = map[s1];
} else {
unionNodes(map[s1], map[s2], values[i], map);
}
}
for (auto query : queries) {
if (map.count(query.first) == 0 || map.count(query.second) == 0 || findParent(map[query.first]) != findParent(map[query.second]))
res.push_back(-1);
else
res.push_back(map[query.first] -> value / map[query.second] -> value);
}
return res;
}
private:
struct Node {
Node* parent;
double value = 0.0;
Node() {parent = this;}
};
void unionNodes(Node* node1, Node* node2, double num, unordered_map<string, Node*>& map) {
Node* parent1 = findParent(node1), *parent2 = findParent(node2);
double ratio = node2 -> value * num / node1 -> value;
for (auto it = map.begin(); it != map.end(); it ++)
if (findParent(it -> second) == parent1)
it -> second -> value *= ratio;
parent1 -> parent = parent2;
}
Node* findParent(Node* node) {
if (node -> parent == node)
return node;
node -> parent = findParent(node -> parent);
return node -> parent;
}
};