@@ -68,7 +68,13 @@ Request 3: Person 3 and person 4 cannot be friends since person 0 and person 1 w
68
68
69
69
## Solutions
70
70
71
- ### Solution 1
71
+ ### Solution 1: Union-Find
72
+
73
+ We can use a union-find set to maintain the friend relationships, and then for each request, we determine whether it meets the restriction conditions.
74
+
75
+ For the two people $(u, v)$ in the current request, if they are already friends, then the request can be directly accepted; otherwise, we traverse the restriction conditions. If there exists a restriction condition $(x, y)$ such that $u$ and $x$ are friends and $v$ and $y$ are friends, or $u$ and $y$ are friends and $v$ and $x$ are friends, then the request cannot be accepted.
76
+
77
+ The time complexity is $O(q \times m \times \log(n))$, and the space complexity is $O(n)$. Where $q$ and $m$ are the number of requests and the number of restriction conditions respectively.
72
78
73
79
<!-- tabs:start -->
74
80
@@ -77,29 +83,27 @@ class Solution:
77
83
def friendRequests (
78
84
self , n : int , restrictions : List[List[int ]], requests : List[List[int ]]
79
85
) -> List[bool ]:
80
- p = list (range (n))
81
-
82
- def find (x ):
86
+ def find (x : int ) -> int :
83
87
if p[x] != x:
84
88
p[x] = find(p[x])
85
89
return p[x]
86
90
91
+ p = list (range (n))
87
92
ans = []
88
- i = 0
89
93
for u, v in requests:
90
- if find(u) == find(v):
94
+ pu, pv = find(u), find(v)
95
+ if pu == pv:
91
96
ans.append(True )
92
97
else :
93
- valid = True
98
+ ok = True
94
99
for x, y in restrictions:
95
- if (find(u) == find(x) and find(v) == find(y)) or (
96
- find(u) == find(y) and find(v) == find(x)
97
- ):
98
- valid = False
100
+ px, py = find(x), find(y)
101
+ if (pu == px and pv == py) or (pu == py and pv == px):
102
+ ok = False
99
103
break
100
- ans.append(valid )
101
- if valid :
102
- p[find(u) ] = find(v)
104
+ ans.append(ok )
105
+ if ok :
106
+ p[pu ] = pv
103
107
return ans
104
108
```
105
109
@@ -112,27 +116,25 @@ class Solution {
112
116
for (int i = 0 ; i < n; ++ i) {
113
117
p[i] = i;
114
118
}
115
- boolean [] ans = new boolean [requests. length];
116
- int i = 0 ;
117
- for (int [] req : requests) {
118
- int u = req[0 ], v = req[1 ];
119
- if (find(u) == find(v)) {
120
- ans[i++ ] = true ;
119
+ int m = requests. length;
120
+ boolean [] ans = new boolean [m];
121
+ for (int i = 0 ; i < m; ++ i) {
122
+ int u = requests[i][0 ], v = requests[i][1 ];
123
+ int pu = find(u), pv = find(v);
124
+ if (pu == pv) {
125
+ ans[i] = true ;
121
126
} else {
122
- boolean valid = true ;
123
- for (int [] res : restrictions) {
124
- int x = res[0 ], y = res[1 ];
125
- if ((find(u) == find(x) && find(v) == find(y))
126
- || (find(u) == find(y) && find(v) == find(x))) {
127
- valid = false ;
127
+ boolean ok = true ;
128
+ for (var r : restrictions) {
129
+ int px = find(r[0 ]), py = find(r[1 ]);
130
+ if ((pu == px && pv == py) || (pu == py && pv == px)) {
131
+ ok = false ;
128
132
break ;
129
133
}
130
134
}
131
- if (valid) {
132
- p[find(u)] = find(v);
133
- ans[i++ ] = true ;
134
- } else {
135
- ans[i++ ] = false ;
135
+ if (ok) {
136
+ ans[i] = true ;
137
+ p[pu] = pv;
136
138
}
137
139
}
138
140
}
@@ -151,75 +153,109 @@ class Solution {
151
153
``` cpp
152
154
class Solution {
153
155
public:
154
- vector<int > p;
155
-
156
156
vector<bool > friendRequests(int n, vector<vector<int >>& restrictions, vector<vector<int >>& requests) {
157
- p.resize(n);
158
- for (int i = 0; i < n; ++i) p[i] = i;
157
+ vector<int > p(n);
158
+ iota(p.begin(), p.end(), 0);
159
+ function<int(int)> find = [ &] (int x) {
160
+ if (p[ x] != x) {
161
+ p[ x] = find(p[ x] );
162
+ }
163
+ return p[ x] ;
164
+ };
159
165
vector<bool > ans;
160
166
for (auto& req : requests) {
161
167
int u = req[ 0] , v = req[ 1] ;
162
- if (find(u) == find(v))
168
+ int pu = find(u), pv = find(v);
169
+ if (pu == pv) {
163
170
ans.push_back(true);
164
- else {
165
- bool valid = true;
166
- for (auto& res : restrictions) {
167
- int x = res [0], y = res [1];
168
- if ((find(u) == find(x) && find(v) == find(y)) || (find(u) == find(y) && find(v) == find(x) )) {
169
- valid = false;
171
+ } else {
172
+ bool ok = true;
173
+ for (auto& r : restrictions) {
174
+ int px = find(r [ 0] ), py = find(r [ 1] ) ;
175
+ if ((pu == px && pv == py) || (pu == py && pv == px )) {
176
+ ok = false;
170
177
break;
171
178
}
172
179
}
173
- ans.push_back(valid);
174
- if (valid) p[find(u)] = find(v);
180
+ ans.push_back(ok);
181
+ if (ok) {
182
+ p[ pu] = pv;
183
+ }
175
184
}
176
185
}
177
186
return ans;
178
187
}
179
-
180
- int find (int x) {
181
- if (p[ x] != x) p[ x] = find(p[ x] );
182
- return p[ x] ;
183
- }
184
188
};
185
189
```
186
190
187
191
```go
188
- var p []int
189
-
190
- func friendRequests(n int, restrictions [][]int, requests [][]int) []bool {
191
- p = make([]int, n)
192
- for i := 0; i < n; i++ {
192
+ func friendRequests(n int, restrictions [][]int, requests [][]int) (ans []bool) {
193
+ p := make([]int, n)
194
+ for i := range p {
193
195
p[i] = i
194
196
}
195
- var ans []bool
197
+ var find func(int) int
198
+ find = func(x int) int {
199
+ if p[x] != x {
200
+ p[x] = find(p[x])
201
+ }
202
+ return p[x]
203
+ }
196
204
for _, req := range requests {
197
- u, v := req[0], req[1]
198
- if find(u) == find(v) {
205
+ pu, pv := find( req[0]), find( req[1])
206
+ if pu == pv {
199
207
ans = append(ans, true)
200
208
} else {
201
- valid := true
202
- for _, res := range restrictions {
203
- x, y := res [0], res [1]
204
- if (find(u) == find(x) && find(v) == find(y)) || (find(u) == find(y) && find(v) == find(x)) {
205
- valid = false
209
+ ok := true
210
+ for _, r := range restrictions {
211
+ px, py := find(r [0]), find(r [1])
212
+ if px == pu && py == pv || px == pv && py == pu {
213
+ ok = false
206
214
break
207
215
}
208
216
}
209
- ans = append(ans, valid )
210
- if valid {
211
- p[find(u) ] = find(v)
217
+ ans = append(ans, ok )
218
+ if ok {
219
+ p[pv ] = pu
212
220
}
213
221
}
214
222
}
215
- return ans
223
+ return
216
224
}
225
+ ```
217
226
218
- func find(x int) int {
219
- if p[x] != x {
220
- p[x] = find(p[x])
221
- }
222
- return p[x]
227
+ ``` ts
228
+ function friendRequests(n : number , restrictions : number [][], requests : number [][]): boolean [] {
229
+ const p: number [] = Array .from ({ length: n }, (_ , i ) => i );
230
+ const find = (x : number ): number => {
231
+ if (p [x ] !== x ) {
232
+ p [x ] = find (p [x ]);
233
+ }
234
+ return p [x ];
235
+ };
236
+ const ans: boolean [] = [];
237
+ for (const [u, v] of requests ) {
238
+ const pu = find (u );
239
+ const pv = find (v );
240
+ if (pu === pv ) {
241
+ ans .push (true );
242
+ } else {
243
+ let ok = true ;
244
+ for (const [x, y] of restrictions ) {
245
+ const px = find (x );
246
+ const py = find (y );
247
+ if ((px === pu && py === pv ) || (px === pv && py === pu )) {
248
+ ok = false ;
249
+ break ;
250
+ }
251
+ }
252
+ ans .push (ok );
253
+ if (ok ) {
254
+ p [pu ] = pv ;
255
+ }
256
+ }
257
+ }
258
+ return ans ;
223
259
}
224
260
```
225
261
0 commit comments