-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1724-checking-existence-of-edge-length-limited-paths-ii.js
More file actions
83 lines (73 loc) · 2.27 KB
/
1724-checking-existence-of-edge-length-limited-paths-ii.js
File metadata and controls
83 lines (73 loc) · 2.27 KB
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
/**
* 1724. Checking Existence of Edge Length Limited Paths II
* https://leetcode.com/problems/checking-existence-of-edge-length-limited-paths-ii/
* Difficulty: Hard
*
* An undirected graph of n nodes is defined by edgeList, where edgeList[i] = [ui, vi, disi]
* denotes an edge between nodes ui and vi with distance disi. Note that there may be multiple
* edges between two nodes, and the graph may not be connected.
*
* Implement the DistanceLimitedPathsExist class:
* - DistanceLimitedPathsExist(int n, int[][] edgeList) Initializes the class with an
* undirected graph.
* - boolean query(int p, int q, int limit) Returns true if there exists a path from p to q
* such that each edge on the path has a distance strictly less than limit, and otherwise
* false.
*/
/**
* @param {number} n
* @param {number[][]} edgeList
*/
var DistanceLimitedPathsExist = function(n, edgeList) {
this.nodeCount = n;
this.sortedEdges = edgeList.slice().sort((a, b) => a[2] - b[2]);
this.unionFindByLimit = new Map();
this.processedLimits = new Set();
};
/**
* @param {number} p
* @param {number} q
* @param {number} limit
* @return {boolean}
*/
DistanceLimitedPathsExist.prototype.query = function(p, q, limit) {
if (!this.processedLimits.has(limit)) {
const unionFind = new UnionFind(this.nodeCount);
for (const [u, v, weight] of this.sortedEdges) {
if (weight >= limit) break;
unionFind.union(u, v);
}
this.unionFindByLimit.set(limit, unionFind);
this.processedLimits.add(limit);
}
return this.unionFindByLimit.get(limit).connected(p, q);
};
class UnionFind {
constructor(n) {
this.parent = Array.from({ length: n }, (_, i) => i);
this.rank = new Array(n).fill(0);
}
find(x) {
if (this.parent[x] !== x) {
this.parent[x] = this.find(this.parent[x]);
}
return this.parent[x];
}
union(x, y) {
const rootX = this.find(x);
const rootY = this.find(y);
if (rootX !== rootY) {
if (this.rank[rootX] < this.rank[rootY]) {
this.parent[rootX] = rootY;
} else if (this.rank[rootX] > this.rank[rootY]) {
this.parent[rootY] = rootX;
} else {
this.parent[rootY] = rootX;
this.rank[rootX]++;
}
}
}
connected(x, y) {
return this.find(x) === this.find(y);
}
}