Maximal Network Rank SOLUTION

Maximal Network Rank SOLUTION

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: 4

Explanation: The network rank of cities 0 and 1 is 4 as there are 4 roads that are connected to either 0 or 1. The road between 0 and 1 is only counted once.


Input: n = 5, roads = [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]

Output: 5

Explanation: There are 5 roads that are connected to cities 1 or 2.


Example 3:

Input: n = 8, roads = [[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]]

Output: 5

Explanation: The network rank of 2 and 5 is 5. Notice that all the cities do not have to be connected.



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.

from collections import defaultdict
class Solution:
    def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
        d = defaultdict(set)
        for i in range(len(roads)):
            r = roads[i]
        c = 0
        for k in d:
            for k2 in d:
                if k != k2:
                    i = len(d[k].intersection(d[k2]))
                    c = max(len(d[k]) + len(d[k2]) – i, c)
        return c

March Long Challenge 2021 Solutions

April Long Challenge 2021 Solutions

Codechef Long Challenge Solutions

February Long Challenge 2021

1. Frog Sort Solution Codechef

2. Chef and Meetings Solution Codechef

3. Maximise Function Solution Codechef

4. Highest Divisor Solution Codechef

5. Cut the Cake Challenge Solution Codechef

6. Dream and the Multiverse Solution Codechef

7. Cell Shell Solution Codechef

8. Multiple Games Solution Codechef

9. Another Tree with Number Theory Solution Codechef

10. XOR Sums Solution Codechef

11. Prime Game Solution CodeChef

12. Team Name Solution Codechef

January Long Challenge 2021

November Challenge 2020 SOLUTION CodeChef

October Lunchtime 2020 CodeChef SOLUTIONS


Related :

Related :

Leave a Comment

one × four =