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:
Given
queries are:
return
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 ].
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.
[Analysis]Consider each equation as an edge in a directed graph, each string is a vertex, then the problem becomes to find a path for each pair of strings in the queries array.
Inspired by Floyd-Warshall algorithm, using a 2-D matrix to represent vertex A to vertex path (if exists), A[i][j] = A[i][k]*A[k][j], for k=0,...|v|-1.
[Solution]
class Solution {
public:
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
set<string> nodes;
unordered_map<string, int> inv;
for (auto& e:equations) {
nodes.insert(e.first);
nodes.insert(e.second);
}
int i=0;
for (auto it= nodes.begin(); it!=nodes.end(); it++, i++)
inv[*it] = i;
vector<vector<double>> equ(nodes.size(), vector<double>(nodes.size(),-1.0));
for (int i=0; i< nodes.size(); i++)
equ[i][i]= 1.0;
for (int i=0; i< equations.size(); i++) {
int x = inv[equations[i].first];
int y = inv[equations[i].second];
equ[x][y] = values[i];
equ[y][x] = 1.0 / values[i];
}
for (int k=0; k<nodes.size(); k++) {
for (int i=0; i<nodes.size(); i++) {
for (int j=i+1; j<nodes.size(); j++) {
if (equ[i][k]!=-1.0 && equ[k][j]!=-1.0) {
equ[i][j] = equ[i][k] * equ[k][j];
equ[j][i] = 1.0/ equ[i][j];
}
}
}
}
vector<double> res;
for (auto& q: queries) {
if (nodes.count(q.first) && nodes.count(q.second)) {
int x= inv[q.first], y= inv[q.second];
res.push_back( equ[x][y] );
}
else res.push_back(-1.0);
}
return res;
}
};
No comments:
Post a Comment