-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmaximal_network_rank.dart
123 lines (96 loc) · 3.34 KB
/
maximal_network_rank.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
-* 1615. 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:
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.
Example 2:
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.
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.
*/
class Solution {
int maximalNetworkRank(int n, List<List<int>> roads) {
final List<int> degrees = List<int>.filled(n, 0);
for (List<int> road in roads) {
final int a = road[0];
final int b = road[1];
degrees[a]++;
degrees[b]++;
}
int maxDegree = 0;
int secondMaxDegree = 0;
for (int degree in degrees) {
if (degree < secondMaxDegree) {
continue;
}
secondMaxDegree = degree;
if (secondMaxDegree > maxDegree) {
int temp = secondMaxDegree;
secondMaxDegree = maxDegree;
maxDegree = temp;
}
}
final List<bool> isCandidate = List<bool>.filled(n, false);
int candidateCount = 0;
int king = -1;
for (int i = 0; i < n; i++) {
if (degrees[i] == secondMaxDegree) {
isCandidate[i] = true;
candidateCount++;
}
if (maxDegree > secondMaxDegree && degrees[i] == maxDegree) {
king = i;
}
}
if (maxDegree == secondMaxDegree) {
// Case 1: We have multiple candidates with the same max degrees.
if (candidateCount > maxDegree + 1) {
return maxDegree * 2;
}
int connectionCount = 0;
for (List<int> road in roads) {
final int a = road[0];
final int b = road[1];
if (isCandidate[a] && isCandidate[b]) {
connectionCount++;
}
}
if (connectionCount < candidateCount * (candidateCount - 1) ~/ 2) {
return maxDegree * 2;
}
return maxDegree * 2 - 1;
}
// Case 2: We have a single max degree (king) and multiple second max degree candidates.
int connectionCount = 0;
for (List<int> road in roads) {
final int a = road[0];
final int b = road[1];
if (a != king && b != king) {
continue;
}
if (isCandidate[a] || isCandidate[b]) {
connectionCount++;
}
}
if (connectionCount < candidateCount) {
return maxDegree + secondMaxDegree;
}
return maxDegree + secondMaxDegree - 1;
}
}