leetcode-5536. 最大网络秩

原始思路

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
class Solution {
public int maximalNetworkRank(int n, int[][] roads) {
Map<Integer, Set<Integer>> graph = new HashMap<>();
for(int i = 0; i < n; i++){
graph.put(i, new HashSet<Integer>());
}

for(int[] road: roads){
int a = road[0];
int b = road[1];
graph.get(a).add(b);
graph.get(b).add(a);
}

int result = 0;

//遍历每两个城市
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
int ans = 0;
ans += graph.get(i).size();
ans += graph.get(j).size();
//去除重叠的边
if(graph.get(i).contains(j)){
ans -= 1;
}
result = Math.max(result, ans);
}
}

return result;
}
}