Mid/SeniorGraph Theory

Maximal Network Rank

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.

Example 1
Inputn = 4, roads = [[0,1],[0,3],[1,2],[1,3]]
Output4
The network rank of cities 0 and 1 is 4 because there are 4 roads connected to either city, with the road between them counted only once.
Example 2
Inputn = 5, roads = [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]
Output5
There are 5 roads that are connected to cities 1 or 2.

Constraints

  • 2 <= n <= 100
  • 0 <= roads.length <= n * (n - 1) / 2
  • roads[i].length == 2
  • 0 <= ai, bi <= n-1
  • ai != bi
  • Each pair of cities has at most one road connecting them.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate