There is an infrastructure of n cities with some number of roads connecting these cities. Each roads[i] = [ai, bi] indicates that there is a bidirectional road between cities ai and bi.
The network rank of two different cities is defined as the total number of directly connected roads to either city. If a road is directly connected to both cities, it is only counted once.
The maximal network rank of the infrastructure is the maximum network rank of all pairs of different cities.
Given the integer n and the array roads, return the maximal network rank of the entire infrastructure.
Input: n =4, roads =[[0,1],[0,3],[1,2],[1,3]]Output: 4Explanation: The network rank of cities 0 and 1is4 as there are 4 roads that are connected to either 0 or 1. The road between 0 and 1is only counted once.
Input: n =5, roads =[[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]Output: 5Explanation: There are 5 roads that are connected to cities 1 or 2.
Example 3:
1
2
3
Input: n =8, roads =[[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]]Output: 5Explanation: The network rank of 2 and 5is5. Notice that all the cities do not have to be connected.
The key idea is that the network rank for any pair of cities is the sum of their degrees (number of roads connected to each) minus one if they are directly connected (since the shared road is counted twice). By counting degrees and using a set to check direct connections, we can efficiently compute the maximal network rank.
classSolution {
publicintmaximalNetworkRank(int n, int[][] roads) {
int[] deg =newint[n];
Set<String> st =new HashSet<>();
for (int[] r : roads) {
deg[r[0]]++;
deg[r[1]]++;
st.add(r[0]+","+ r[1]);
st.add(r[1]+","+ r[0]);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int cur = deg[i]+ deg[j];
if (st.contains(i +","+ j)) cur--;
ans = Math.max(ans, cur);
}
}
return ans;
}
}
classSolution {
funmaximalNetworkRank(n: Int, roads: Array<IntArray>): Int {
val deg = IntArray(n)
val st = mutableSetOf<Pair<Int, Int>>()
for (r in roads) {
deg[r[0]]++ deg[r[1]]++ st.add(Pair(r[0], r[1]))
st.add(Pair(r[1], r[0]))
}
var ans = 0for (i in0 until n) {
for (j in i + 1 until n) {
var cur = deg[i] + deg[j]
if (st.contains(Pair(i, j))) cur-- ans = maxOf(ans, cur)
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defmaximalNetworkRank(self, n: int, roads: list[list[int]]) -> int:
deg = [0] * n
st = set()
for a, b in roads:
deg[a] +=1 deg[b] +=1 st.add((a, b))
st.add((b, a))
ans =0for i in range(n):
for j in range(i +1, n):
cur = deg[i] + deg[j]
if (i, j) in st:
cur -=1 ans = max(ans, cur)
return ans
impl Solution {
pubfnmaximal_network_rank(n: i32, roads: Vec<Vec<i32>>) -> i32 {
let n = n asusize;
letmut deg =vec![0; n];
letmut st = std::collections::HashSet::new();
for r in&roads {
deg[r[0] asusize] +=1;
deg[r[1] asusize] +=1;
st.insert((r[0], r[1]));
st.insert((r[1], r[0]));
}
letmut ans =0;
for i in0..n {
for j in i +1..n {
letmut cur = deg[i] + deg[j];
if st.contains(&(i asi32, j asi32)) {
cur -=1;
}
ans = ans.max(cur);
}
}
ans
}
}