Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 68b4860

Browse files
authored
feat: add solutions to lc problem: No.2076 (doocs#2606)
1 parent a32a03a commit 68b4860

File tree

7 files changed

+313
-214
lines changed

7 files changed

+313
-214
lines changed

solution/2000-2099/2076.Process Restricted Friend Requests/README.md

Lines changed: 107 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@
7272

7373
## 解法
7474

75-
### 方法一
75+
### 方法一:并查集
76+
77+
我们可以用并查集来维护朋友关系,然后对于每个请求,判断是否满足限制条件。
78+
79+
对于当前请求的两个人 $(u, v)$,如果他们已经是朋友,那么可以直接接受请求;否则,我们遍历限制条件,如果存在限制条件 $(x, y)$,使得 $u$ 和 $x$ 互为朋友并且 $v$ 和 $y$ 互为朋友,或者 $u$ 和 $y$ 互为朋友并且 $v$ 和 $x$ 互为朋友,那么就不能接受请求。
80+
81+
时间复杂度 $O(q \times m \times \log(n))$,空间复杂度 $O(n)$。其中 $q$ 和 $m$ 分别是请求的数量和限制条件的数量。
7682

7783
<!-- tabs:start -->
7884

@@ -81,29 +87,27 @@ class Solution:
8187
def friendRequests(
8288
self, n: int, restrictions: List[List[int]], requests: List[List[int]]
8389
) -> List[bool]:
84-
p = list(range(n))
85-
86-
def find(x):
90+
def find(x: int) -> int:
8791
if p[x] != x:
8892
p[x] = find(p[x])
8993
return p[x]
9094

95+
p = list(range(n))
9196
ans = []
92-
i = 0
9397
for u, v in requests:
94-
if find(u) == find(v):
98+
pu, pv = find(u), find(v)
99+
if pu == pv:
95100
ans.append(True)
96101
else:
97-
valid = True
102+
ok = True
98103
for x, y in restrictions:
99-
if (find(u) == find(x) and find(v) == find(y)) or (
100-
find(u) == find(y) and find(v) == find(x)
101-
):
102-
valid = False
104+
px, py = find(x), find(y)
105+
if (pu == px and pv == py) or (pu == py and pv == px):
106+
ok = False
103107
break
104-
ans.append(valid)
105-
if valid:
106-
p[find(u)] = find(v)
108+
ans.append(ok)
109+
if ok:
110+
p[pu] = pv
107111
return ans
108112
```
109113

@@ -116,27 +120,25 @@ class Solution {
116120
for (int i = 0; i < n; ++i) {
117121
p[i] = i;
118122
}
119-
boolean[] ans = new boolean[requests.length];
120-
int i = 0;
121-
for (int[] req : requests) {
122-
int u = req[0], v = req[1];
123-
if (find(u) == find(v)) {
124-
ans[i++] = true;
123+
int m = requests.length;
124+
boolean[] ans = new boolean[m];
125+
for (int i = 0; i < m; ++i) {
126+
int u = requests[i][0], v = requests[i][1];
127+
int pu = find(u), pv = find(v);
128+
if (pu == pv) {
129+
ans[i] = true;
125130
} else {
126-
boolean valid = true;
127-
for (int[] res : restrictions) {
128-
int x = res[0], y = res[1];
129-
if ((find(u) == find(x) && find(v) == find(y))
130-
|| (find(u) == find(y) && find(v) == find(x))) {
131-
valid = false;
131+
boolean ok = true;
132+
for (var r : restrictions) {
133+
int px = find(r[0]), py = find(r[1]);
134+
if ((pu == px && pv == py) || (pu == py && pv == px)) {
135+
ok = false;
132136
break;
133137
}
134138
}
135-
if (valid) {
136-
p[find(u)] = find(v);
137-
ans[i++] = true;
138-
} else {
139-
ans[i++] = false;
139+
if (ok) {
140+
ans[i] = true;
141+
p[pu] = pv;
140142
}
141143
}
142144
}
@@ -155,75 +157,109 @@ class Solution {
155157
```cpp
156158
class Solution {
157159
public:
158-
vector<int> p;
159-
160160
vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {
161-
p.resize(n);
162-
for (int i = 0; i < n; ++i) p[i] = i;
161+
vector<int> p(n);
162+
iota(p.begin(), p.end(), 0);
163+
function<int(int)> find = [&](int x) {
164+
if (p[x] != x) {
165+
p[x] = find(p[x]);
166+
}
167+
return p[x];
168+
};
163169
vector<bool> ans;
164170
for (auto& req : requests) {
165171
int u = req[0], v = req[1];
166-
if (find(u) == find(v))
172+
int pu = find(u), pv = find(v);
173+
if (pu == pv) {
167174
ans.push_back(true);
168-
else {
169-
bool valid = true;
170-
for (auto& res : restrictions) {
171-
int x = res[0], y = res[1];
172-
if ((find(u) == find(x) && find(v) == find(y)) || (find(u) == find(y) && find(v) == find(x))) {
173-
valid = false;
175+
} else {
176+
bool ok = true;
177+
for (auto& r : restrictions) {
178+
int px = find(r[0]), py = find(r[1]);
179+
if ((pu == px && pv == py) || (pu == py && pv == px)) {
180+
ok = false;
174181
break;
175182
}
176183
}
177-
ans.push_back(valid);
178-
if (valid) p[find(u)] = find(v);
184+
ans.push_back(ok);
185+
if (ok) {
186+
p[pu] = pv;
187+
}
179188
}
180189
}
181190
return ans;
182191
}
183-
184-
int find(int x) {
185-
if (p[x] != x) p[x] = find(p[x]);
186-
return p[x];
187-
}
188192
};
189193
```
190194
191195
```go
192-
var p []int
193-
194-
func friendRequests(n int, restrictions [][]int, requests [][]int) []bool {
195-
p = make([]int, n)
196-
for i := 0; i < n; i++ {
196+
func friendRequests(n int, restrictions [][]int, requests [][]int) (ans []bool) {
197+
p := make([]int, n)
198+
for i := range p {
197199
p[i] = i
198200
}
199-
var ans []bool
201+
var find func(int) int
202+
find = func(x int) int {
203+
if p[x] != x {
204+
p[x] = find(p[x])
205+
}
206+
return p[x]
207+
}
200208
for _, req := range requests {
201-
u, v := req[0], req[1]
202-
if find(u) == find(v) {
209+
pu, pv := find(req[0]), find(req[1])
210+
if pu == pv {
203211
ans = append(ans, true)
204212
} else {
205-
valid := true
206-
for _, res := range restrictions {
207-
x, y := res[0], res[1]
208-
if (find(u) == find(x) && find(v) == find(y)) || (find(u) == find(y) && find(v) == find(x)) {
209-
valid = false
213+
ok := true
214+
for _, r := range restrictions {
215+
px, py := find(r[0]), find(r[1])
216+
if px == pu && py == pv || px == pv && py == pu {
217+
ok = false
210218
break
211219
}
212220
}
213-
ans = append(ans, valid)
214-
if valid {
215-
p[find(u)] = find(v)
221+
ans = append(ans, ok)
222+
if ok {
223+
p[pv] = pu
216224
}
217225
}
218226
}
219-
return ans
227+
return
220228
}
229+
```
221230

222-
func find(x int) int {
223-
if p[x] != x {
224-
p[x] = find(p[x])
225-
}
226-
return p[x]
231+
```ts
232+
function friendRequests(n: number, restrictions: number[][], requests: number[][]): boolean[] {
233+
const p: number[] = Array.from({ length: n }, (_, i) => i);
234+
const find = (x: number): number => {
235+
if (p[x] !== x) {
236+
p[x] = find(p[x]);
237+
}
238+
return p[x];
239+
};
240+
const ans: boolean[] = [];
241+
for (const [u, v] of requests) {
242+
const pu = find(u);
243+
const pv = find(v);
244+
if (pu === pv) {
245+
ans.push(true);
246+
} else {
247+
let ok = true;
248+
for (const [x, y] of restrictions) {
249+
const px = find(x);
250+
const py = find(y);
251+
if ((px === pu && py === pv) || (px === pv && py === pu)) {
252+
ok = false;
253+
break;
254+
}
255+
}
256+
ans.push(ok);
257+
if (ok) {
258+
p[pu] = pv;
259+
}
260+
}
261+
}
262+
return ans;
227263
}
228264
```
229265

0 commit comments

Comments
 (0)