Skip to content

Commit 3b83f3d

Browse files
authored
Added task 2076.
1 parent 0888d94 commit 3b83f3d

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package g2001_2100.s2076_process_restricted_friend_requests;
2+
3+
// #Hard #Graph #Union_Find #2022_05_29_Time_102_ms_(55.25%)_Space_54.4_MB_(55.25%)
4+
5+
public class Solution {
6+
public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {
7+
// Check for each request whether it can cause conflict or not
8+
UnionFind uf = new UnionFind(n);
9+
boolean[] res = new boolean[requests.length];
10+
for (int i = 0; i < requests.length; i++) {
11+
int p1 = uf.findParent(requests[i][0]);
12+
int p2 = uf.findParent(requests[i][1]);
13+
if (p1 == p2) {
14+
res[i] = true;
15+
continue;
16+
}
17+
// Check whether the current request will violate any restriction or not
18+
boolean flag = true;
19+
for (int[] restrict : restrictions) {
20+
int r1 = uf.findParent(restrict[0]);
21+
int r2 = uf.findParent(restrict[1]);
22+
if ((r1 == p1 && r2 == p2) || (r1 == p2 && r2 == p1)) {
23+
flag = false;
24+
break;
25+
}
26+
}
27+
if (flag) {
28+
res[i] = true;
29+
// Union
30+
uf.parent[p1] = p2;
31+
}
32+
}
33+
return res;
34+
}
35+
36+
private static class UnionFind {
37+
int n;
38+
int[] parent;
39+
40+
public UnionFind(int n) {
41+
this.n = n;
42+
this.parent = new int[n];
43+
for (int i = 0; i < n; i++) {
44+
parent[i] = i;
45+
}
46+
}
47+
48+
public int findParent(int user) {
49+
while (parent[user] != user) {
50+
parent[user] = parent[parent[user]];
51+
user = parent[user];
52+
}
53+
return user;
54+
}
55+
}
56+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2076\. Process Restricted Friend Requests
2+
3+
Hard
4+
5+
You are given an integer `n` indicating the number of people in a network. Each person is labeled from `0` to `n - 1`.
6+
7+
You are also given a **0-indexed** 2D integer array `restrictions`, where <code>restrictions[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> means that person <code>x<sub>i</sub></code> and person <code>y<sub>i</sub></code> **cannot** become **friends**, either **directly** or **indirectly** through other people.
8+
9+
Initially, no one is friends with each other. You are given a list of friend requests as a **0-indexed** 2D integer array `requests`, where <code>requests[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> is a friend request between person <code>u<sub>j</sub></code> and person <code>v<sub>j</sub></code>.
10+
11+
A friend request is **successful** if <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code> can be **friends**. Each friend request is processed in the given order (i.e., `requests[j]` occurs before `requests[j + 1]`), and upon a successful request, <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code> **become direct friends** for all future friend requests.
12+
13+
Return _a **boolean array**_ `result`, _where each_ `result[j]` _is_ `true` _if the_ <code>j<sup>th</sup></code> _friend request is **successful** or_ `false` _if it is not_.
14+
15+
**Note:** If <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code> are already direct friends, the request is still **successful**.
16+
17+
**Example 1:**
18+
19+
**Input:** n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1]]
20+
21+
**Output:** [true,false]
22+
23+
**Explanation:**
24+
25+
Request 0: Person 0 and person 2 can be friends, so they become direct friends.
26+
27+
Request 1: Person 2 and person 1 cannot be friends since person 0 and person 1 would be indirect friends (1--2--0).
28+
29+
**Example 2:**
30+
31+
**Input:** n = 3, restrictions = [[0,1]], requests = [[1,2],[0,2]]
32+
33+
**Output:** [true,false]
34+
35+
**Explanation:**
36+
37+
Request 0: Person 1 and person 2 can be friends, so they become direct friends.
38+
39+
Request 1: Person 0 and person 2 cannot be friends since person 0 and person 1 would be indirect friends (0--2--1).
40+
41+
**Example 3:**
42+
43+
**Input:** n = 5, restrictions = [[0,1],[1,2],[2,3]], requests = [[0,4],[1,2],[3,1],[3,4]]
44+
45+
**Output:** [true,false,true,false]
46+
47+
**Explanation:**
48+
49+
Request 0: Person 0 and person 4 can be friends, so they become direct friends.
50+
51+
Request 1: Person 1 and person 2 cannot be friends since they are directly restricted.
52+
53+
Request 2: Person 3 and person 1 can be friends, so they become direct friends.
54+
55+
Request 3: Person 3 and person 4 cannot be friends since person 0 and person 1 would be indirect friends (0--4--3--1).
56+
57+
**Constraints:**
58+
59+
* `2 <= n <= 1000`
60+
* `0 <= restrictions.length <= 1000`
61+
* `restrictions[i].length == 2`
62+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= n - 1</code>
63+
* <code>x<sub>i</sub> != y<sub>i</sub></code>
64+
* `1 <= requests.length <= 1000`
65+
* `requests[j].length == 2`
66+
* <code>0 <= u<sub>j</sub>, v<sub>j</sub> <= n - 1</code>
67+
* <code>u<sub>j</sub> != v<sub>j</sub></code>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g2001_2100.s2076_process_restricted_friend_requests;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void friendRequests() {
11+
assertThat(
12+
new Solution()
13+
.friendRequests(3, new int[][] {{0, 1}}, new int[][] {{0, 2}, {2, 1}}),
14+
equalTo(new boolean[] {true, false}));
15+
}
16+
17+
@Test
18+
void friendRequests2() {
19+
assertThat(
20+
new Solution()
21+
.friendRequests(3, new int[][] {{0, 1}}, new int[][] {{1, 2}, {0, 2}}),
22+
equalTo(new boolean[] {true, false}));
23+
}
24+
25+
@Test
26+
void friendRequests3() {
27+
assertThat(
28+
new Solution()
29+
.friendRequests(
30+
5,
31+
new int[][] {{0, 1}, {1, 2}, {2, 3}},
32+
new int[][] {{0, 4}, {1, 2}, {3, 1}, {3, 4}}),
33+
equalTo(new boolean[] {true, false, true, false}));
34+
}
35+
}

0 commit comments

Comments
 (0)