forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
32 lines (32 loc) · 930 Bytes
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function friendRequests(n: number, restrictions: number[][], requests: number[][]): boolean[] {
const p: number[] = Array.from({ length: n }, (_, i) => i);
const find = (x: number): number => {
if (p[x] !== x) {
p[x] = find(p[x]);
}
return p[x];
};
const ans: boolean[] = [];
for (const [u, v] of requests) {
const pu = find(u);
const pv = find(v);
if (pu === pv) {
ans.push(true);
} else {
let ok = true;
for (const [x, y] of restrictions) {
const px = find(x);
const py = find(y);
if ((px === pu && py === pv) || (px === pv && py === pu)) {
ok = false;
break;
}
}
ans.push(ok);
if (ok) {
p[pu] = pv;
}
}
}
return ans;
}