Leetcode 399除数求值
题目定义:
给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件,
其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] 。
每个 Ai 或 Bi 是一个表示单个变量的字符串。
另有一些以数组 queries 表示的问题,其中 queries[j] = [Cj, Dj] 表示第 j 个问题,
请你根据已知条件找出 Cj / Dj = ? 的结果作为答案。
返回 所有问题的答案 。如果存在某个无法确定的答案,则用 -1.0 替代这个答案。
注意:输入总是有效的。你可以假设除法运算中不会出现除数为 0 的情况,
且不存在任何矛盾的结果。
示例 1:
输入:equations = [["a","b"],["b","c"]],
values = [2.0,3.0],
queries = [["a","c"], ["b","a"],["a","e"],["a","a"],["x","x"]]
输出:[6.00000,0.50000,-1.00000,1.00000,-1.00000]
解释:
条件:a / b = 2.0, b / c = 3.0
问题:a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
结果:[6.0, 0.5, -1.0, 1.0, -1.0 ]
示例 2:
输入:equations = [["a","b"],["b","c"],["bc","cd"]],
values = [1.5,2.5,5.0],
queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
输出:[3.75000,0.40000,5.00000,0.20000]
示例 3:
输入:equations = [["a","b"]], values = [0.5],
queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
输出:[0.50000,2.00000,-1.00000,-1.00000]
提示:
1 <= equations.length <= 20
equations[i].length == 2
1 <= Ai.length, Bi.length <= 5
values.length == equations.length
0.0 < values[i] <= 20.0
1 <= queries.length <= 20
queries[i].length == 2
1 <= Cj.length, Dj.length <= 5
Ai, Bi, Cj, Dj 由小写英文字母与数字组成
方式一(图):
class Solution {
private Map<String,Integer> variables = new HashMap<>();
private Integer nvars = 0;
public double[] calcEquation(List<List<String>> equations,
double[] values, List<List<String>> queries) {
List<Pair<Integer,Double>>[] edges = getEdges(equations,values);
int queriesCount = queries.size();
double[] ret = new double[queriesCount];
for(int i = 0; i < queriesCount; i++){
List<String> query = queries.get(i);
double result = -1.0;
if(variables.containsKey(query.get(0)) && variables.containsKey(query.get(1))){
int ia = variables.get(query.get(0));
int ib = variables.get(query.get(1));
if(ia == ib)
result = 1.0;
else{
Queue<Integer> points = new LinkedList<>();
points.offer(ia);
double[] ratios = new double[nvars];
Arrays.fill(ratios,-1.0);
ratios[ia] = 1.0;
while(!points.isEmpty() && ratios[ib] < 0){
int x = points.poll();
for(Pair pair : edges[x]){
int y = (int) pair.getKey();
double val = (double) pair.getValue();
if(ratios[y] < 0){
ratios[y] = ratios[x] * val;
points.offer(y);
}
}
}
result = ratios[ib];
}
}
ret[i] = result;
}
return ret;
}
//存储图的节点和边权值
private List<Pair<Integer,Double>>[] getEdges(List<List<String>> equations,
double[] values){
int n = equations.size();
for(int i = 0; i < n; i++){
variables.putIfAbsent(equations.get(i).get(0),nvars++);
variables.putIfAbsent(equations.get(i).get(1),nvars++);
}
List<Pair<Integer,Double>>[] edges = new List[nvars];
for(int i = 0 ;i < nvars; i++){
edges[i] = new ArrayList<>();
}
for(int i = 0; i < n; i++){
int va = variables.get(equations.get(i).get(0));
int vb = variables.get(equations.get(i).get(1));
edges[va].add(new Pair(vb,values[i]));
edges[vb].add(new Pair(va,1.0 / values[i]));
}
return edges;
}
}
方式二(Floyd算法):
class Solution {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
int nvars = 0;
Map<String,Integer> variables = new HashMap<>();
for(int i = 0; i < equations.size(); i++){
if(!variables.containsKey(equations.get(i).get(0)))
variables.put(equations.get(i).get(0),nvars++);
if(!variables.containsKey(equations.get(i).get(1)))
variables.put(equations.get(i).get(1),nvars++);
}
double[][] graph = new double[nvars][nvars];
for(int i = 0 ; i < nvars; i++)
Arrays.fill(graph[i],-1);
for(int i = 0; i < equations.size(); i++){
int va = variables.get(equations.get(i).get(0));
int vb = variables.get(equations.get(i).get(1));
graph[va][vb] = values[i];
graph[vb][va] = 1.0 / values[i];
}
//floyd算法
for(int k = 0; k < nvars; k++)
for(int i = 0; i < nvars; i++)
for(int j = 0; j < nvars; j++)
if(graph[i][k] > 0 && graph[k][j] > 0)
graph[i][j] = graph[i][k] * graph[k][j];
double[] ret = new double[queries.size()];
for(int i = 0; i < queries.size(); i++){
List<String> query = queries.get(i);
double result = -1.0;
if(variables.containsKey(query.get(0)) && variables.containsKey(query.get(1))){
int va = variables.get(query.get(0));
int vb = variables.get(query.get(1));
if(graph[va][vb] > 0)
result = graph[va][vb];
}
ret[i] = result;
}
return ret;
}
}
方式三(并查集):
class Solution {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
int equationsSize = equations.size();
UnionFind unionFind =new UnionFind(2 * equationsSize);
Map<String,Integer> map = new HashMap<>(2 * equationsSize);
int nvars = 0;
for(int i = 0; i < equationsSize; i++){
if(!map.containsKey(equations.get(i).get(0)))
map.put(equations.get(i).get(0),nvars++);
if(!map.containsKey(equations.get(i).get(1)))
map.put(equations.get(i).get(1),nvars++);
unionFind.union(map.get(equations.get(i).get(0)),map.get(equations.get(i).get(1)),values[i]);
}
int queriesSize = queries.size();
double[] ret = new double[queriesSize];
for(int i = 0; i < queriesSize; i++){
if(map.get(queries.get(i).get(0)) == null || map.get(queries.get(i).get(1)) == null)
ret[i] = -1.0;
else
ret[i] = unionFind.isConnected(map.get(queries.get(i).get(0)),map.get(queries.get(i).get(1)));
}
return ret;
}
private class UnionFind{
private int[] parent;
private double[] weight;
public UnionFind(int n){
this.parent = new int[n];
this.weight = new double[n];
for(int i = 0; i < n; i++){
parent[i] = i;
weight[i] = 1.0d;
}
}
public void union(int x,int y,double value){
int rootX = find(x);
int rootY = find(y);
if(rootX == rootY)
return;
parent[rootX] = rootY;
weight[rootX] = weight[y] * value /weight[x];
}
public int find(int x){
if(x != parent[x]){
int origin = parent[x];
parent[x] = find(parent[x]);
weight[x] *= weight[origin];
}
return parent[x];
}
public double isConnected(int x,int y){
int rootX = find(x);
int rootY = find(y);
if(rootX == rootY)
return weight[x] / weight[y];
else
return -1.0;
}
}
}
参考:
https://leetcode-cn.com/problems/evaluate-division/solution/399-chu-fa-qiu-zhi-nan-du-zhong-deng-286-w45d/
https://leetcode-cn.com/problems/evaluate-division/solution/chu-fa-qiu-zhi-by-leetcode-solution-8nxb/