This is one of those problems where the clean idea matters more than the amount of code. For 1615. Maximal Network Rank, the solution in this repository is mainly a greedy solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are maximalNetworkRank, degrees, encode, indices.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 240 ms, faster than 50.00% of C++ online submissions for Maximal Network Rank.
02//Memory Usage: 45.6 MB, less than 50.00% of C++ online submissions for Maximal Network Rank.
03struct vec_hash_int {
04 inline std::size_t operator()(const std::vector<int> & v) const {
05 return v[0]*31+v[1];
06 }
07};
08
09class Solution {
10public:
11 int maximalNetworkRank(int n, vector<vector<int>>& roads) {
12 int ans = 0;
13
14 vector<int> degrees(n, 0);
15 unordered_set<vector<int>, vec_hash_int> edges;
16
17 for(vector<int>& road : roads){
18 if(road[0] > road[1]) swap(road[0], road[1]);
19 edges.insert(road);
20 ++degrees[road[0]];
21 ++degrees[road[1]];
22 }
23
24 for(int i = 0; i < n; ++i){
25 for(int j = i+1; j < n; ++j){
26 int connected = (edges.find({i,j}) != edges.end());
27 ans = max(ans, degrees[i] + degrees[j] - connected);
28 }
29 }
30
31 return ans;
32 }
33};
34
35//average O(NlogN), worst O(N^2)
36//https://leetcode.com/problems/maximal-network-rank/discuss/889001/C%2B%2B-quadratic...-linearithmic...-linear!
37//Runtime: 148 ms, faster than 50.00% of C++ online submissions for Maximal Network Rank.
38//Memory Usage: 33.3 MB, less than 50.00% of C++ online submissions for Maximal Network Rank.
39class Solution {
40public:
41 int encode(int i, int j){
42 if(i > j) swap(i, j);
43 //n's range [2,100]
44 //i*128+j
45 return (i<<7)+j;
46 }
47
48 int maximalNetworkRank(int n, vector<vector<int>>& roads) {
49 vector<int> degrees(n, 0);
50 unordered_set<int> edges;
51
52 for(vector<int>& road : roads){
53 edges.insert(encode(road[0], road[1]));
54 ++degrees[road[0]];
55 ++degrees[road[1]];
56 }
57
58 vector<int> indices(n);
59 iota(indices.begin(), indices.end(), 0);
60 //the index whose degree is larger is put former
61 sort(indices.begin(), indices.end(), [°rees](int i, int j){return degrees[i] > degrees[j];});
62
63 size_t ans = 0;
64 bool stop = false;
65 for(int i = 0; i < n; ++i){
66 for(int j = i+1; j < n; ++j){
67 if(degrees[indices[i]] + degrees[indices[j]] < ans){
68 stop = true;
69 break;
70 }
71 ans = max(ans, degrees[indices[i]] + degrees[indices[j]] - edges.count(encode(indices[i], indices[j])));
72 }
73 if(stop) break;
74 }
75
76 return ans;
77 }
78};
79
80//O(N)
81//https://leetcode.com/problems/maximal-network-rank/discuss/889001/C%2B%2B-quadratic...-linearithmic...-linear!
82//Runtime: 152 ms, faster than 50.00% of C++ online submissions for Maximal Network Rank.
83//Memory Usage: 33.2 MB, less than 50.00% of C++ online submissions for Maximal Network Rank.
84class Solution {
85public:
86 int encode(int i, int j){
87 if(i > j) swap(i, j);
88 //n's range [2,100]
89 //i*128+j
90 return (i<<7)+j;
91 }
92
93 int maximalNetworkRank(int n, vector<vector<int>>& roads) {
94 vector<int> degrees(n, 0);
95 unordered_set<int> edges;
96
97 for(vector<int>& road : roads){
98 edges.insert(encode(road[0], road[1]));
99 ++degrees[road[0]];
100 ++degrees[road[1]];
101 }
102
103 int max_i = max_element(degrees.begin(), degrees.end()) - degrees.begin();
104 int max_degree = degrees[max_i];
105 int max_degree_count = count(degrees.begin(), degrees.end(), max_degree);
106
107 size_t ans = 0;
108
109 if(max_degree_count == 1){
110 //there are only one city with max degree
111 //try all other cities
112 for(int j = 0; j < n; ++j){
113 if(j == max_i) continue;
114 ans = max(ans, max_degree+degrees[j]-edges.count(encode(max_i, j)));
115 }
116 }else{
117 //there are multiple cities with max degree
118 //if these cities are fully connected, then ans is max_degree * 2 - 1
119 //otherwise we can find two such cities which are not directly connected, so ans is max_degree * 2
120 int roads_btw_max_degree = count_if(roads.begin(), roads.end(),
121 [°rees, &max_degree](vector<int>& r){
122 return (degrees[r[0]] == max_degree) && (degrees[r[1]] == max_degree);});
123 ans = max_degree * 2 - (roads_btw_max_degree == max_degree_count*(max_degree_count-1)/2);
124 }
125
126 return ans;
127 }
128};
Cost