Process Restricted Friend Requests
Last updated
Last updated
class DisjointSet {
List<Integer> parent = new ArrayList<>();
List<Integer> rank = new ArrayList<>();
public DisjointSet(int n) {
for(int i=0;i<n;i++) {
parent.add(i);
rank.add(1);
}
}
public int findParent(int node) {
if(node == parent.get(node)) {
return node;
}
int parentNode = findParent(parent.get(node));
parent.set(node, parentNode);
return parent.get(node);
}
public void unionByRank(int x, int y) {
int parentX = findParent(x);
int parentY = findParent(y);
if(parentX == parentY) {
return;
}
int rankX = rank.get(parentX), rankY = rank.get(parentY);
if(rankX < rankY) {
parent.set(parentX, parentY);
} else if(rankY < rankX) {
parent.set(parentY, parentX);
} else {
parent.set(parentY, parentX);
rank.set(parentX, rankX + 1);
}
}
}
class Solution {
public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {
// Time Complexity: O(m Ξ±(n)), inverse ackerman function
// Space Complexity: O(n), n -> number of users in graph
// `result` is a boolean array that will store the results of the friend requests.
boolean[] result = new boolean[requests.length];
// `ds` is a disjoint set data structure that will be used to keep track of the connected components of the graph.
DisjointSet ds = new DisjointSet(n);
for(int i=0;i<requests.length;i++) {
// Get the two users representatives involved in the request.
int p1 = ds.findParent(requests[i][0]);
int p2 = ds.findParent(requests[i][1]);
boolean flag = true;
for(int[] restriction: restrictions) {
// Get the restricted user representatives
int r1 = ds.findParent(restriction[0]);
int r2 = ds.findParent(restriction[1]);
// If two users are connected by restriction, we cannot approve the request
if((p1 == r1 && p2 == r2) || (p1 == r2 && p2 == r1)) {
flag = false;
break;
}
}
// If the two users are not connected by a restriction, then we can approve the friend request.
if(flag) {
ds.unionByRank(p1, p2);
result[i] = true;
}
}
return result;
}
}