Description
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 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& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector.
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.
Solution
给一组数相除的结果,返回一组query的结果,如果无法计算,返回-1.0.
We use two hash maps to contruct a graph according to equations and values. Then variables in equations are nodes in a graph, values are the weight from node a to node b. Pairs map stores the edges of a node, and valuesPair stores the weight information. First using a for loop to iteratively travel equations and values. Adding edges and weights to pairs and values pair.
Then iteratively access queries, using dfs to find a path from query[0] to query[1]. If does not have this path, return 0.0, and then update -1.0 to res.
For the dfs function, we use a hash set to store nodes we have visited. If a circle is formatted or pairs does not contain start, return 0.0. If found end, return current value; For dfs, we get the edge info and weight info of start node, and iteratively travels the edges using dfs and updated node and values. Remember to remove start in set for upper level use.
Code
class Solution {
private HashMap<String, ArrayList<String>> pairs;
private HashMap<String, ArrayList<Double>> valuesPair;
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
pairs = new HashMap<>();
valuesPair = new HashMap<>();
for (int i = 0; i < equations.length; i++){
String[] equation = equations[i];
if (!pairs.containsKey(equation[0])){
pairs.put(equation[0], new ArrayList<String>());
valuesPair.put(equation[0], new ArrayList<Double>());
}
if (!pairs.containsKey(equation[1])){
pairs.put(equation[1], new ArrayList<String>());
valuesPair.put(equation[1], new ArrayList<Double>());
}
pairs.get(equation[0]).add(equation[1]);
pairs.get(equation[1]).add(equation[0]);
valuesPair.get(equation[0]).add(values[i]);
valuesPair.get(equation[1]).add(1 / values[i]);
}
double[] res = new double[queries.length];
for (int i = 0; i < queries.length; i++){
String[] query = queries[i];
res[i] = dfs(query[0], query[1], new HashSet<String>(), 1.0);
if (res[i] == 0.0){
res[i] = -1.0;
}
}
return res;
}
private double dfs(String start, String end, HashSet<String>set, double value){
if (set.contains(start)){
return 0.0;
}
if (!pairs.containsKey(start)){
return 0.0;
}
if (start.equals(end)){
return value;
}
set.add(start);
ArrayList<String> strList = pairs.get(start);
ArrayList<Double> valueList = valuesPair.get(start);
double temp = 0.0;
for (int i = 0; i < strList.size(); i++){
temp = dfs(strList.get(i), end, set, value * valueList.get(i));
if (temp != 0.0) {
break;
}
}
set.remove(start);
return temp;
}
}
Time Complexity: O()
Space Complexity: O()