-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
73 lines (63 loc) · 2.23 KB
/
Main.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
public class Main {
public static List<Integer> getRecommendedFriends(int n, List<List<Integer>> friendships) {
List<HashSet<Integer>> g = new ArrayList<>();
for (int i = 0; i < n; i++) {
g.add(new HashSet<>());
}
for (List<Integer> pair : friendships) {
int a = pair.get(0);
int b = pair.get(1);
g.get(a).add(b);
g.get(b).add(a);
}
// 初始化推荐好友列表
List<Integer> re = new ArrayList<>();
for (int i = 0; i < n; i++) {
re.add(-1); // 默认没有推荐
}
// 遍历每个用户以寻找推荐好友
for (int cur = 0; cur < n; cur++) {
HashSet<Integer> curUser = g.get(cur);
int mmax = -1;
int be = -1;
// 检查所有其他用户作为潜在的推荐好友
for (int i = 0; i < n; i++) {
if (cur != i && !curUser.contains(i)) {
// 计算共同好友数量
HashSet<Integer> other = g.get(i);
int comm = 0;
for (Integer friend : curUser) {
if (other.contains(friend)) {
comm++;
}
}
// 更新最佳推荐
if (comm > mmax || (comm == mmax && i < be)) {
mmax = comm;
be = i;
}
}
}
// 设置该用户的推荐好友
if (mmax > 0) { // 只有当有共同好友时才推荐
re.set(cur, be);
}
}
return re;
}
public static void main(String[] args) {
// 示例输入
int n = 3;
List<List<Integer>> friendships = List.of(
// List.of(0, 1), List.of(0, 2), List.of(1, 3),
// List.of(2, 3), List.of(3, 4)
List.of(0, 1), List.of(1, 2), List.of(2, 0)
);
// 获取并打印推荐结果
List<Integer> result = getRecommendedFriends(n, friendships);
System.out.println(result);
}
}