Skip to content

Commit 9e3beec

Browse files
committedApr 23, 2025
Add solution #1627
1 parent 7929e9a commit 9e3beec

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,431 LeetCode solutions in JavaScript
1+
# 1,432 LeetCode solutions in JavaScript
22

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

@@ -1255,6 +1255,7 @@
12551255
1624|[Largest Substring Between Two Equal Characters](./solutions/1624-largest-substring-between-two-equal-characters.js)|Easy|
12561256
1625|[Lexicographically Smallest String After Applying Operations](./solutions/1625-lexicographically-smallest-string-after-applying-operations.js)|Medium|
12571257
1626|[Best Team With No Conflicts](./solutions/1626-best-team-with-no-conflicts.js)|Medium|
1258+
1627|[Graph Connectivity With Threshold](./solutions/1627-graph-connectivity-with-threshold.js)|Hard|
12581259
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12591260
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12601261
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 1627. Graph Connectivity With Threshold
3+
* https://leetcode.com/problems/graph-connectivity-with-threshold/
4+
* Difficulty: Hard
5+
*
6+
* We have n cities labeled from 1 to n. Two different cities with labels x and y are directly
7+
* connected by a bidirectional road if and only if x and y share a common divisor strictly
8+
* greater than some threshold. More formally, cities with labels x and y have a road between
9+
* them if there exists an integer z such that all of the following are true:
10+
* - x % z == 0,
11+
* - y % z == 0, and
12+
* - z > threshold.
13+
*
14+
* Given the two integers, n and threshold, and an array of queries, you must determine for each
15+
* queries[i] = [ai, bi] if cities ai and bi are connected directly or indirectly. (i.e. there
16+
* is some path between them).
17+
*
18+
* Return an array answer, where answer.length == queries.length and answer[i] is true if for
19+
* the ith query, there is a path between ai and bi, or answer[i] is false if there is no path.
20+
*/
21+
22+
/**
23+
* @param {number} n
24+
* @param {number} threshold
25+
* @param {number[][]} queries
26+
* @return {boolean[]}
27+
*/
28+
var areConnected = function(n, threshold, queries) {
29+
const parent = new Array(n + 1).fill().map((_, i) => i);
30+
31+
for (let z = threshold + 1; z <= n; z++) {
32+
for (let x = z; x <= n; x += z) {
33+
union(x, z);
34+
}
35+
}
36+
37+
return queries.map(([a, b]) => find(a) === find(b));
38+
39+
function find(x) {
40+
if (parent[x] !== x) {
41+
parent[x] = find(parent[x]);
42+
}
43+
return parent[x];
44+
}
45+
46+
function union(x, y) {
47+
parent[find(x)] = find(y);
48+
}
49+
};

0 commit comments

Comments
 (0)