当前位置:网站首页>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 hoisting machinery command registration examination and hoisting machinery command examination registration
- LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
- How to choose the right server for website data collection?
- 2022年聚合工艺考试题及模拟考试
- [leetcode skimming] February summary (updating)
- 什么是权限?什么是角色?什么是用户?
- OSPF notes [dr and bdr]
- Browser top loading (from Zhihu)
- What is uid? What is auth? What is a verifier?
- CUDA development and debugging tool
猜你喜欢

Recommend the best product development process in the Internet industry!

LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机

Tip of edge browser: enter+ctrl can automatically convert the address bar into a web address

Spock单元测试框架介绍及在美团优选的实践___第一章

Task04 mathematical statistics

Use winmtr software to simply analyze, track and detect network routing

类和对象收尾

This may be your last chance to join Tencent

2022年上海市安全员C证考试题模拟考试题库及答案

2022 hoisting machinery command registration examination and hoisting machinery command examination registration
随机推荐
TCP server communication flow
Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
DO280管理应用部署--RC
Tip of edge browser: enter+ctrl can automatically convert the address bar into a web address
Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security
The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears
总结全了,低代码还需要解决这4点问题
NFT:使用 EIP-2981 開啟 NFT 版稅之旅
Seven crimes of counting software R & D Efficiency
LetCode 1829. Maximum XOR value per query
细数软件研发效能的七宗罪
如何看待智慧城市建设中的改变和机遇?
【人话版】WEB3黑暗森林中的隐私博弈
2022 polymerization process test questions and simulation test
Obtain detailed ideas for ABCDEF questions of 2022 American Games
What is uid? What is auth? What is a verifier?
2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
2022年化工自动化控制仪表操作证考试题库及答案
Leetcode learning - day 36
C language games (I) -- guessing games