Skip to content

Commit 1052461

Browse files
committed
Add solution #1697
1 parent df95f97 commit 1052461

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,483 LeetCode solutions in JavaScript
1+
# 1,484 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1308,6 +1308,7 @@
13081308
1694|[Reformat Phone Number](./solutions/1694-reformat-phone-number.js)|Easy|
13091309
1695|[Maximum Erasure Value](./solutions/1695-maximum-erasure-value.js)|Medium|
13101310
1696|[Jump Game VI](./solutions/1696-jump-game-vi.js)|Medium|
1311+
1697|[Checking Existence of Edge Length Limited Paths](./solutions/1697-checking-existence-of-edge-length-limited-paths.js)|Hard|
13111312
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13121313
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13131314
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* 1697. Checking Existence of Edge Length Limited Paths
3+
* https://leetcode.com/problems/checking-existence-of-edge-length-limited-paths/
4+
* Difficulty: Hard
5+
*
6+
* An undirected graph of n nodes is defined by edgeList, where edgeList[i] = [ui, vi, disi] denotes
7+
* an edge between nodes ui and vi with distance disi. Note that there may be multiple edges between
8+
* two nodes.
9+
*
10+
* Given an array queries, where queries[j] = [pj, qj, limitj], your task is to determine for each
11+
* queries[j] whether there is a path between pj and qj such that each edge on the path has a
12+
* distance strictly less than limitj.
13+
*
14+
* Return a boolean array answer, where answer.length == queries.length and the jth value of answer
15+
* is true if there is a path for queries[j] is true, and false otherwise.
16+
*/
17+
18+
/**
19+
* @param {number} n
20+
* @param {number[][]} edgeList
21+
* @param {number[][]} queries
22+
* @return {boolean[]}
23+
*/
24+
var distanceLimitedPathsExist = function(n, edgeList, queries) {
25+
const parent = new Array(n).fill().map((_, i) => i);
26+
27+
edgeList.sort((a, b) => a[2] - b[2]);
28+
const sortedQueries = queries.map((q, i) => [...q, i]).sort((a, b) => a[2] - b[2]);
29+
const result = new Array(queries.length).fill(false);
30+
31+
let edgeIndex = 0;
32+
for (const [p, q, limit, index] of sortedQueries) {
33+
while (edgeIndex < edgeList.length && edgeList[edgeIndex][2] < limit) {
34+
union(edgeList[edgeIndex][0], edgeList[edgeIndex][1]);
35+
edgeIndex++;
36+
}
37+
result[index] = find(p) === find(q);
38+
}
39+
40+
return result;
41+
42+
function find(x) {
43+
if (parent[x] !== x) {
44+
parent[x] = find(parent[x]);
45+
}
46+
return parent[x];
47+
}
48+
49+
function union(x, y) {
50+
parent[find(x)] = find(y);
51+
}
52+
};

0 commit comments

Comments
 (0)