-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate-division.js
103 lines (86 loc) · 1.97 KB
/
evaluate-division.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* 手工封装Union-Find算法
*/
class UF {
constructor(n) {
this.parent = new Array(n)
this.weights = new Array(n).fill(1);
this.count = 0;
this.size = new Array(n).fill(1)
for (let i = 0; i < n; i++) {
this.parent[i] = i
}
}
/**
* 归并
*/
union(p, q, weight) {
let rootP = this.find(p)
let rootQ = this.find(q);
if (rootP === rootQ) {
return
}
this.parent[rootP] = this.parent[rootQ];
this.size[rootQ]++
this.weights[rootP] = this.weights[q] * weight / this.weights[p]
this.count--
}
find(x) {
if (x != this.parent[x]) {
const origin = this.parent[x];
this.parent[x] = this.find(this.parent[x]);
this.weights[x] *= this.weights[origin]
}
return this.parent[x]
}
isConnected(p, q) {
let rootP = this.find(p)
let rootQ = this.find(q)
if (rootP == rootQ) {
return this.weights[p] / this.weights[q];
} else {
return -1;
}
}
getCount() {
return this.count
}
}
/**
* @param {string[][]} equations
* @param {number[]} values
* @param {string[][]} queries
* @return {number[]}
*/
var calcEquation = function (equations, values, queries) {
var map = new Map();
var id = 0;
var uf = new UF(2 * equations.length);
equations.forEach((o, i) => {
const [a, b] = o;
if (!map.has(a)) {
map.set(a, id)
id++
}
if (!map.has(b)) {
map.set(b, id)
id++
}
uf.union(map.get(a), map.get(b), values[i])
})
let queriesSize = queries.length;
const res = new Array(queriesSize)
for (let i = 0; i < queriesSize; i++) {
let [var1, var2] = queries[i];
let id1 = map.get(var1);
let id2 = map.get(var2);
if (id1 == null || id2 == null) {
res[i] = -1.0;
} else {
res[i] = uf.isConnected(id1, id2);
}
}
console.log(res)
return res;
};
calcEquation([["a", "b"], ["b", "c"]], [2.0, 3.0], [["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"]])