-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
37 lines (31 loc) · 972 Bytes
/
Solution.cpp
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
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <vector>
class Solution {
public:
int maximalNetworkRank(int n, std::vector<std::vector<int>>& roads) {
// seems to be counting the degree. Pick the Max degree node.
// Then, for the second max, if there is a tie, pick the one that
// is not connected to the max.
// Adjacency matrix for fast connection lookup.
std::vector<std::vector<char>> connected(n, std::vector<char>(n, false));
std::vector<int> degree(n, 0);
for (const auto& edge : roads) {
const int from = edge[0];
const int to = edge[1];
++degree[from];
++degree[to];
connected[from][to] = true;
connected[to][from] = true;
}
int maxRank = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int rank = degree[i] + degree[j] - connected[i][j];
maxRank = std::max(maxRank, rank);
}
}
return maxRank;
}
};