当前位置:网站首页>2022-02-15 (399. Division evaluation)
2022-02-15 (399. Division evaluation)
2022-07-01 04:30:00 【TickTick123】
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);
// The first 1 Step : Preprocessing , Compare the value of the variable with id mapping , Make the bottom layer of the parallel search set use array to realize , Easy to code
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]);
}
// The first 2 Step : Make a query
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;
/** * The weight of the parent node pointed to */
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;
// For the derivation of the relation, see 「 Reference code 」 Diagram below
weight[rootX] = weight[y] * value / weight[x];
}
/** * Path compression * * @param x * @return Of the root node 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;
}
}
}
}
Alas, my life sighs , The priority of joint search set is low
边栏推荐
- 2022 gas examination question bank and online simulation examination
- One job hopping up 8K, three times in five years
- Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
- 嵌入式系统开发笔记79:为什么要获取本机网卡IP地址
- Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
- Hololens2 development environment building and deploying apps
- 【无标题】
- Common thread methods and daemon threads
- Embedded System Development Notes 80: using QT designer to design the main interface
- Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
猜你喜欢

软件研发的十大浪费:研发效能的另一面

测量三相永磁同步电机的交轴直轴电感

类和对象收尾

2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试

Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022

使用WinMTR软件简单分析跟踪检测网络路由情况

TASK04|数理统计

Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions

Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling

Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and
随机推荐
Daily question - line 10
2022年T电梯修理题库及模拟考试
Mallbook: how can hotel enterprises break the situation in the post epidemic era?
This may be your last chance to join Tencent
[human version] Web3 privacy game in the dark forest
做网站数据采集,怎么选择合适的服务器呢?
Annual inventory review of Alibaba cloud's observable practices in 2021
Haskell lightweight threads overhead and use on multicores
Unity's 3D multi-point arrow navigation
MySQL function variable stored procedure
LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
Software testing needs more and more talents. Why do you still not want to take this path?
Coinbase in a bear market: losses, layoffs, stock price plunges
2. Use of classlist (element class name)
【LeetCode】100. Same tree
2022 hoisting machinery command registration examination and hoisting machinery command examination registration
NFT:使用 EIP-2981 開啟 NFT 版稅之旅
Account sharing technology enables the farmers' market and reshapes the efficiency of transaction management services
OSPF notes [multiple access, two multicast addresses with OSPF]
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机