当前位置:网站首页>LeetCode_并查集_中等_399. 除法求值
LeetCode_并查集_中等_399. 除法求值
2022-06-10 18:25:00 【一瓢江湖我沉浮】
1.题目
给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件,其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。
另有一些以数组 queries 表示的问题,其中 queries[j] = [Cj, Dj] 表示第 j 个问题,请你根据已知条件找出 Cj / Dj = ? 的结果作为答案。
返回所有问题的答案 。如果存在某个无法确定的答案,则用 -1.0 替代这个答案。如果问题中出现了给定的已知条件中没有出现的字符串,也需要用 -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 由小写英文字母与数字组成
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/evaluate-division
2.思路
(1)并查集
思路参考本题官方题解。
相似题目:LeetCode_并查集_中等_990.等式方程的可满足性
3.代码实现(Java)
//思路1————并查集
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public 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);
// 第 1 步:预处理,将变量的值与 id 进行映射,使得并查集的底层使用数组实现,方便编码
Map<String, Integer> hashMap = new HashMap<>(2 * equationsSize);
int id = 0;
for (int i = 0; i < equationsSize; i++) {
List<String> equation = equations.get(i);
String var1 = equation.get(0);
String var2 = equation.get(1);
if (!hashMap.containsKey(var1)) {
hashMap.put(var1, id);
id++;
}
if (!hashMap.containsKey(var2)) {
hashMap.put(var2, id);
id++;
}
unionFind.union(hashMap.get(var1), hashMap.get(var2), values[i]);
}
// 第 2 步:做查询
int queriesSize = queries.size();
double[] res = new double[queriesSize];
for (int i = 0; i < queriesSize; i++) {
String var1 = queries.get(i).get(0);
String var2 = queries.get(i).get(1);
Integer id1 = hashMap.get(var1);
Integer id2 = hashMap.get(var2);
if (id1 == null || id2 == null) {
res[i] = -1.0d;
} else {
res[i] = unionFind.isConnected(id1, id2);
}
}
return res;
}
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];
}
/** * 路径压缩 * * @param x * @return 根结点的 id */
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.0d;
}
}
}
}
边栏推荐
- Win32 child window parent window window owner
- Anchor type and row data type of DB2 SQL pl
- 第四章 数据类型(三)
- 北京地铁票务系统
- Adobe Premiere foundation - tool use (selection tool, razor tool, and other common tools) (III)
- 2022.05.28(LC_516_最长回文子序列)
- SQL语句查看基本表结构和表中约束字段 、主码、外码 (简单有效)
- 抢唱玩法升级,正版音乐高潮片段、实时打分能力等你集成~
- 2022.05.24(LC_674_最长连续递增序列)
- Design and implementation of online ordering system based on SSM Rar (project source code)
猜你喜欢

C (pointer-02)

阵列信号处理仿真之四——Z变换分析阵列多项式

OPENCV 检测人脸 不依赖于任何第三方库

Enterprise data quality management: how to evaluate data quality?

MySQL高级篇第一章(linux下安装MySQL)【上】

Adobe Premiere Basics - introduction, configuration, shortcut keys, creating projects, creating sequences (I)

Openssl1.1.1 VS2013-编译教程

Live broadcast preview | a new era of social interaction, exploring new social experiences in the universe

Data URL

Mysql (17 déclencheurs)
随机推荐
通过举栗子的方式来讲解面试题(可面试,可复习,可学习)
Performance and high availability analysis of database firewall
Adobe Premiere基礎-工具使用(選擇工具,剃刀工具,等常用工具)(三)
直播预告 | 解构OLAP!新型多维分析架构范式全公开!Apache Doris 将带来五个重磅议题!
VIM common shortcut keys
Adobe Premiere Foundation (track related) (V)
In the era of data processing, data analysis has become the basic construction
HelloWorld example of TestNG and how to run it from the command line
Salesmartly | add a new channel slack to help you close the customer relationship
Openssl1.1.1 compilation error can't locate win32/console pm in @INC
2022.05.24(LC_674_最长连续递增序列)
Seata installing the window environment
OPENCV 检测人脸 不依赖于任何第三方库
Upgrade the playing method of snatching singing, integrate the climax clips of genuine music and real-time scoring ability~
Debugging skills
单纯形法代码求解(含超详细代码注释和整个流程图)
SQL语句查看基本表结构和表中约束字段 、主码、外码 (简单有效)
Opencv does not rely on any third-party database for face detection
腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
Adobe Premiere foundation - tool use (selection tool, razor tool, and other common tools) (III)