/**
* Problem: LeetCode 2076 - Process Restricted Friend Requests
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
class Solution {
private int[] parent;
private int find(int x) {
while (parent[x] != x) {
parent[x] = parent[parent[x]]; // path compression
x = parent[x];
}
return x;
}
public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {
parent = new int[n];
for (int i = 0; i < n; i++) parent[i] = i;
boolean[] result = new boolean[requests.length];
for (int i = 0; i < requests.length; i++) {
int pu = find(requests[i][0]);
int pv = find(requests[i][1]);
// Pehle se same group mein hain toh request successful
if (pu == pv) {
result[i] = true;
continue;
}
// Check karo ki pu aur pv ko jodne se koi restriction violate toh nahi hoti
boolean ok = true;
for (int[] r : restrictions) {
int px = find(r[0]);
int py = find(r[1]);
if ((px == pu && py == pv) || (px == pv && py == pu)) {
ok = false;
break;
}
}
if (ok) {
parent[pu] = pv; // dono groups merge
result[i] = true;
}
}
return result;
}
}
"""
Problem: LeetCode 2076 - Process Restricted Friend Requests
Difficulty: Hard
Language: Python 3
Author: CodeByTushu
"""
class Solution:
def friendRequests(self, n: int, restrictions: list[list[int]], requests: list[list[int]]) -> list[bool]:
parent = list(range(n))
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]] # path compression
x = parent[x]
return x
result = []
for u, v in requests:
pu = find(u)
pv = find(v)
# Already in the same group -> request is already satisfied
if pu == pv:
result.append(True)
continue
# Verify if merging components pu and pv violates any restriction
ok = True
for x, y in restrictions:
px = find(x)
py = find(y)
if (px == pu and py == pv) or (px == pv and py == pu):
ok = False
break
if ok:
parent[pu] = pv # merge components
result.append(True)
else:
result.append(False)
return result
/**
* Problem: LeetCode 2076 - Process Restricted Friend Requests
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
using namespace std;
class Solution {
private:
vector<int> parent;
int find(int x) {
while (parent[x] != x) {
parent[x] = parent[parent[x]]; // path compression
x = parent[x];
}
return x;
}
public:
vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {
parent.resize(n);
for (int i = 0; i < n; i++) parent[i] = i;
int m = requests.size();
vector<bool> result(m, false);
for (int i = 0; i < m; i++) {
int pu = find(requests[i][0]);
int pv = find(requests[i][1]);
// If already in the same component, request succeeds automatically
if (pu == pv) {
result[i] = true;
continue;
}
// Check if merging pu and pv violates any existing restriction
bool ok = true;
for (const auto& r : restrictions) {
int px = find(r[0]);
int py = find(r[1]);
if ((px == pu && py == pv) || (px == pv && py == pu)) {
ok = false;
break;
}
}
if (ok) {
parent[pu] = pv; // merge components
result[i] = true;
}
}
return result;
}
};
/**
* Problem: LeetCode 2076 - Process Restricted Friend Requests
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number} n
* @param {number[][]} restrictions
* @param {number[][]} requests
* @return {boolean[]}
*/
var friendRequests = function(n, restrictions, requests) {
const parent = new Int32Array(n);
for (let i = 0; i < n; i++) parent[i] = i;
function find(x) {
while (parent[x] !== x) {
parent[x] = parent[parent[x]]; // path compression
x = parent[x];
}
return x;
}
const m = requests.length;
const result = new Array(m).fill(false);
for (let i = 0; i < m; i++) {
const pu = find(requests[i][0]);
const pv = find(requests[i][1]);
// If already in the same friend circle, request is valid
if (pu === pv) {
result[i] = true;
continue;
}
// Verify that merging pu and pv does not violate any restriction
let ok = true;
for (let j = 0; j < restrictions.length; j++) {
const px = find(restrictions[j][0]);
const py = find(restrictions[j][1]);
if ((px === pu && py === pv) || (px === pv && py === pu)) {
ok = false;
break;
}
}
if (ok) {
parent[pu] = pv; // merge groups
result[i] = true;
}
}
return result;
};