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 is given. Roads do not intersect except 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:

1starts = [1, 2, 3, 3]
2ends = [2, 3, 1, 4]
3n = 4

Output:

14

Explanation:

The chosen cities may be 2 and 3, with the four roads connected to them being: (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 = [0] * (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
1import java.util.Arrays;
2import java.util.List;
3import java.util.Scanner;
4import java.util.stream.Collectors;
5
6class Solution {
7    public static int maxNetworkRank(List<Integer> starts, List<Integer> ends, int n) {
8        int[] edgeCount = new int[n];
9        int m = starts.size();
10        int maxRank = Integer.MIN_VALUE;
11
12        for (int i = 0; i < m; i++) {
13            edgeCount[starts.get(i) - 1]++;
14            edgeCount[ends.get(i) - 1]++;
15        }
16
17        for (int i = 0; i < m; i++) {
18            int rank = edgeCount[starts.get(i) - 1] + edgeCount[ends.get(i) - 1] - 1;
19            if (rank > maxRank) {
20                maxRank = rank;
21            }
22        }
23
24        return maxRank;
25    }
26
27    public static List<String> splitWords(String s) {
28        return s.isEmpty() ? List.of() : Arrays.asList(s.split(" "));
29    }
30
31    public static void main(String[] args) {
32        Scanner scanner = new Scanner(System.in);
33        List<Integer> starts = splitWords(scanner.nextLine()).stream().map(Integer::parseInt).collect(Collectors.toList());
34        List<Integer> ends = splitWords(scanner.nextLine()).stream().map(Integer::parseInt).collect(Collectors.toList());
35        int n = Integer.parseInt(scanner.nextLine());
36        scanner.close();
37        int res = maxNetworkRank(starts, ends, n);
38        System.out.println(res);
39    }
40}
41
1function maxNetworkRank(starts, ends, n) {
2    const edgeCount = {};
3    let maxRank = 0;
4    const m = starts.length;
5
6    for (let i = 0; i < m; i++) {
7        edgeCount[starts[i]] = (edgeCount[starts[i]] ? edgeCount[starts[i]] : 0) + 1;
8        edgeCount[ends[i]] = (edgeCount[ends[i]] ? edgeCount[ends[i]] : 0) + 1;
9    }
10
11    for (let j = 0; j < m; j++) {
12        const rank = edgeCount[starts[j]] + edgeCount[ends[j]] - 1;
13        if (rank > maxRank) {
14            maxRank = rank;
15        }
16    }
17
18    return maxRank;
19}
20
21function splitWords(s) {
22    return s == "" ? [] : s.split(' ');
23}
24
25function* main() {
26    const starts = splitWords(yield).map((v) => parseInt(v));
27    const ends = splitWords(yield).map((v) => parseInt(v));
28    const n = parseInt(yield);
29    const res = maxNetworkRank(starts, ends, n);
30    console.log(res);
31}
32
33class EOFError extends Error {}
34{
35    const gen = main();
36    const next = (line) => gen.next(line).done && process.exit();
37    let buf = '';
38    next();
39    process.stdin.setEncoding('utf8');
40    process.stdin.on('data', (data) => {
41        const lines = (buf + data).split('\n');
42        buf = lines.pop();
43        lines.forEach(next);
44    });
45    process.stdin.on('end', () => {
46        buf && next(buf);
47        gen.throw(new EOFError());
48    });
49}
50

Got a question?Β Ask the Teaching AssistantΒ anything you don't understand.

Still not clear? Ask in the Forum, Β DiscordΒ orΒ SubmitΒ the part you don't understand to our editors.

←
↑TA πŸ‘¨β€πŸ«