Microsoft Online Assessment (OA) 2021 - Max Network Rank | Codility
An infrastructure consisting of n
cities from l
to n
, and 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 starts
, ends
consisting of m
integers each and an integer n
, where starts[i]
and ends[i]
are cities at the two
ends of the i-th
road, returns the maximal network rank in the whole infrastructure.
Example:
Input:
starts = [ 1 , 2 , 3 , 3 ]
ends = [ 2 , 3 , 1 , 4 ]
n = 4
Output:
4
Explanation:
The chosen cities may be 2
and 3
, and the four roads connected to them are: (2,1), (2,3), (3,1), (3,4)
.
Try it yourself
Python Java JavaScript from typing import List
def max_network_rank ( starts: List [ int ], ends: List [ int ], n: int ) -> int :
adj = [ 0 ] * (n + 1 )
for a, b in zip (starts, ends):
adj[a] += 1
adj[b] += 1
max_rank = 0
for a, b in zip (starts, ends):
max_rank = max (max_rank, adj[a] + adj[b] - 1 )
return max_rank
if __name__ == '__main__' :
starts = [ int (x) for x in input ().split()]
ends = [ int (x) for x in input ().split()]
n = int ( input ())
res = max_network_rank(starts, ends, n)
print (res)
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
class Solution {
public static int maxNetworkRank (List<Integer> starts, List<Integer> ends, int n) {
int [] edgeCount = new int [n];
int m = starts.size();
int maxRank = Integer.MIN_VALUE;
for ( int i = 0 ; i < m; i++) {
edgeCount[starts.get(i) - 1 ]++;
edgeCount[ends.get(i) - 1 ]++;
}
for ( int i = 0 ; i < m; i++) {
int rank = edgeCount[starts.get(i) - 1 ] + edgeCount[ends.get(i) - 1 ] - 1 ;
if (rank > maxRank) {
maxRank = rank;
}
}
return maxRank;
}
public static List<String> splitWords (String s) {
return s.isEmpty() ? List.of() : Arrays.asList(s.split( " " ));
}
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> starts = splitWords(scanner.nextLine()).stream().map(Integer::parseInt).collect(Collectors.toList());
List<Integer> ends = splitWords(scanner.nextLine()).stream().map(Integer::parseInt).collect(Collectors.toList());
int n = Integer.parseInt(scanner.nextLine());
scanner.close();
int res = maxNetworkRank(starts, ends, n);
System.out.println(res);
}
}
function maxNetworkRank ( starts, ends, n ) {
const edgeCount = {};
let maxRank = 0 ;
const m = starts.length;
for ( let i = 0 ; i < m; i++) {
edgeCount[starts[i]] = (edgeCount[starts[i]] ? edgeCount[starts[i]] : 0 ) + 1 ;
edgeCount[ends[i]] = (edgeCount[ends[i]] ? edgeCount[ends[i]] : 0 ) + 1 ;
}
for ( let j = 0 ; j < m; j++) {
const rank = edgeCount[starts[j]] + edgeCount[ends[j]] - 1 ;
if (rank > maxRank) {
maxRank = rank;
}
}
return maxRank;
}
function splitWords ( s ) {
return s == "" ? [] : s.split( ' ' );
}
function * main ( ) {
const starts = splitWords( yield ).map( ( v ) => parseInt (v));
const ends = splitWords( yield ).map( ( v ) => parseInt (v));
const n = parseInt ( yield );
const res = maxNetworkRank(starts, ends, n);
console .log(res);
}
class EOFError extends Error {}
{
const gen = main();
const next = ( line ) => gen.next(line).done && process.exit();
let buf = '' ;
next();
process.stdin.setEncoding( 'utf8' );
process.stdin.on( 'data' , ( data ) => {
const lines = (buf + data).split( '\n' );
buf = lines.pop();
lines.forEach(next);
});
process.stdin.on( 'end' , () => {
buf && next(buf);
gen.throw( new EOFError());
});
}