当前位置:网站首页>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
边栏推荐
- Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch / 3.4.2 multiple registration protocol (MRP)
- MySQL winter vacation self-study 2022 12 (5)
- Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
- Use winmtr software to simply analyze, track and detect network routing
- Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
- 【人话版】WEB3黑暗森林中的隐私博弈
- Odeint et GPU
- LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
- CF1638E colorful operations
- Some small knowledge points
猜你喜欢
Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security
分账技术赋能农贸市场,重塑交易管理服务效能
Maixll dock quick start
Class and object finalization
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism
[leetcode skimming] February summary (updating)
Internet winter, how to spend three months to make a comeback
Custom components in applets
Ten wastes of software research and development: the other side of research and development efficiency
How to do the performance pressure test of "Health Code"
随机推荐
Tip of edge browser: enter+ctrl can automatically convert the address bar into a web address
Maixll dock quick start
Spock单元测试框架介绍及在美团优选的实践___第一章
2022 hoisting machinery command registration examination and hoisting machinery command examination registration
js 图片路径转换base64格式
Annual inventory review of Alibaba cloud's observable practices in 2021
TCP server communication flow
Recommend the best product development process in the Internet industry!
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
Some small knowledge points
Task04 | statistiques mathématiques
selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
TASK04|数理统计
Class and object finalization
如何看待智慧城市建设中的改变和机遇?
尺取法:有效三角形的个数
【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码
Common thread methods and daemon threads
类和对象收尾
Concurrent mode of different performance testing tools