Microsoft Online Assessment (OA) 2021 - Max Network Rank | Codility
An infrastructure consisting of
n cities from
m bidirectional roads between them are given.
Roads do not intersect apart from at their start and endpoints (they can pass through underground tunnels to avoid collisions).
For each pair of cities directly connected by a road, let’s define their network rank as the total number of roads that are connected to either of the two cities.
Write a function, given two arrays
ends consisting of
m integers each and an integer
ends[i] are cities at the two
ends of the
i-th road, returns the maximal network rank in the whole infrastructure.
1starts = [1, 2, 3, 3] 2ends = [2, 3, 1, 4] 3n = 4
The chosen cities may be
3, and the four roads connected to them are:
(2,1), (2,3), (3,1), (3,4).
Try it yourself
1from typing import List 2 3def max_network_rank(starts: List[int], ends: List[int], n: int) -> int: 4 adj =  * (n + 1) 5 6 for a, b in zip(starts, ends): 7 adj[a] += 1 8 adj[b] += 1 9 10 max_rank = 0 11 12 for a, b in zip(starts, ends): 13 max_rank = max(max_rank, adj[a] + adj[b] - 1) 14 15 return max_rank 16 17if __name__ == '__main__': 18 starts = [int(x) for x in input().split()] 19 ends = [int(x) for x in input().split()] 20 n = int(input()) 21 res = max_network_rank(starts, ends, n) 22 print(res) 23