Skip to content

Commit 95c836c

Browse files
authored
Adding the solution of Graph Connectivity With Threshold (#120)
* Added a new Leetcode Problem 1627 * Resolving bug in readme
1 parent 9b9ebe0 commit 95c836c

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Problem Name : Graph Connectivity with Threshold
3+
* Concept Involved : Disjoint Union Set, Seive
4+
*
5+
* Execution Time : 16 ms
6+
* Memory Consumed : 92.7 mb
7+
*
8+
*/
9+
class Solution {
10+
public static class Dus {
11+
int[] parent;
12+
int n;
13+
14+
Dus(int n) {
15+
this.n = n;
16+
parent = new int[n];
17+
for (int i = 1; i < n; i++) {
18+
parent[i] = i;
19+
}
20+
}
21+
22+
public int find(int u) {
23+
if (u != parent[u]) {
24+
parent[u] = find(parent[u]);
25+
}
26+
return parent[u];
27+
}
28+
29+
public void union(int u, int v) {
30+
int pu = find(u);
31+
int pv = find(v);
32+
33+
if (pu != pv) {
34+
parent[pv] = pu;
35+
}
36+
}
37+
}
38+
public List<Boolean> areConnected(int n, int threshold, int[][] queries) {
39+
ArrayList<Boolean> res = new ArrayList<>();
40+
Dus dus = new Dus(n+1);
41+
42+
for(int i=1; i<=n; i++){
43+
for(int j=2*i; j<=n; j+=i){
44+
if(i > threshold){
45+
dus.union(i,j);
46+
}
47+
}
48+
}
49+
50+
for(int i=0; i<queries.length; i++){
51+
int u = queries[i][0];
52+
int v = queries[i][1];
53+
int pu = dus.find(u);
54+
int pv = dus.find(v);
55+
56+
if(pu == pv){
57+
res.add(true);
58+
}
59+
else{
60+
res.add(false);
61+
}
62+
}
63+
64+
return res;
65+
}
66+
}

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
371371
| 785 | [Is Graph Bipartite?](https://leetcode.com/problems/is-graph-bipartite/) | [C++](./C++/Is-Graph-Bipartite.cpp) | _O(V+E)_ | _O(V)_ | Medium | Graph | BFS |
372372
| 947 | [Most Stones Removed with Same Row or Column](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/) | [C++](./C++/Most-Stones-Removed-with-Same-Row-or-Column.cpp) | _O(V)_ | _O(2V)_ | Medium | Graph | Union Find | | [C++](./C++/Most-Stones-Removed-with-Same-Row-or-Column.cpp) | _O(V)_ | _O(2V)_ | Medium | Graph | Union Find |
373373
| 210 | [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/) | [C++](./C++/Course-Schedule-II.cpp) | _O(V+E)_ | _O(V)_ | Medium | Graph | BFS |
374+
| 1627 | [Graph Connectivity with Threshold](https://leetcode.com/problems/graph-connectivity-with-threshold/) | [Java](./Java/graph_connectivity_with_threshold.java) | _O(V.logV + Q)_ | _O(V)_ | Hard | Graph | Union Find + Sieve |
374375
<br/>
375376
<div align="right">
376377
<b><a href="#algorithms">⬆️ Back to Top</a></b>
@@ -434,7 +435,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
434435
| [James Y](https://github.com/jameszu) <br> <img src="https://avatars0.githubusercontent.com/u/41566813?s=400&u=af77d15517566ea590a316030b4a6d402b0041b6&v=4" width="100" height="100"> | New Zealand | python | [Github](https://github.com/jameszu) |
435436
| [Hamza B](https://github.com/9Hamza) <br> <img src="https://avatars2.githubusercontent.com/u/56516922?s=400&u=2c1adeef0194a2859361d464f28783bfba698638&v=4" width="100" height="100"> | Saudi Arabia | Java | [Github](https://github.com/9Hamza) |
436437
| [Meli Haktas](https://github.com/MercerFrey) <br> <img src="https://avatars1.githubusercontent.com/u/29127873?s=460&u=149319db4468ec2316e49a75eb5e05b35eb05eef&v=4" width="100" height="100"> | Turkey | python | [Github](https://github.com/MercerFrey) |
437-
438+
| [Saurav Prateek](https://github.com/SauravP97) <br> <img src="https://avatars3.githubusercontent.com/u/26816418?s=460&u=4b5222d5b6db79389efba062714c9dfba263732f&v=4" width="100" height="100"> | India | Java | [Github](https://github.com/SauravP97) |
438439
<br/>
439440
<div align="right">
440441
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)