Problem solution · C++

Maximal Network Rank

Maximal Network Rank: a C++ solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Direct simulation
Source
walkccc LeetCode Solutions
Length
72 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Maximal Network Rank, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 72 lines of C++ from the credited upstream file 1615.cpp.
  • The implementation visibly relies on sequence storage.
  • 4 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMaximal Network Rank · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int maximalNetworkRank(int n, vector<vector<int>>& roads) {    vector<int> degrees(n);     for (const vector<int>& road : roads) {      const int u = road[0];      const int v = road[1];      ++degrees[u];      ++degrees[v];    }     // Find the first maximum and the second maximum degrees.    int maxDegree1 = 0;    int maxDegree2 = 0;    for (const int degree : degrees) {      if (degree > maxDegree1) {        maxDegree2 = maxDegree1;        maxDegree1 = degree;      } else if (degree > maxDegree2) {        maxDegree2 = degree;      }    }     // There can be multiple nodes with `maxDegree1` or `maxDegree2`.    // Find the counts of such nodes.    int countMaxDegree1 = 0;    int countMaxDegree2 = 0;    for (const int degree : degrees)      if (degree == maxDegree1)        ++countMaxDegree1;      else if (degree == maxDegree2)        ++countMaxDegree2;     if (countMaxDegree1 == 1) {      // 1. If there is only one node with degree = `maxDegree1`, then we'll      // need to use the node with degree = `maxDegree2`. The answer in general      // will be (maxDegree1 + maxDegree2), but if the two nodes that we're      // considering are connected, then we'll have to subtract 1.      const int edgeCount =          getEdgeCount(roads, degrees, maxDegree1, maxDegree2) +          getEdgeCount(roads, degrees, maxDegree2, maxDegree1);      return maxDegree1 + maxDegree2 - (countMaxDegree2 == edgeCount ? 1 : 0);    } else {      // 2. If there are more than one node with degree = `maxDegree1`, then we      // can consider `maxDegree1` twice, and we don't need to use `maxDegree2`.      // The answer in general will be 2 * maxDegree1, but if the two nodes that      // we're considering are connected, then we'll have to subtract 1.      const int edgeCount =          getEdgeCount(roads, degrees, maxDegree1, maxDegree1);      const int maxPossibleEdgeCount =          countMaxDegree1 * (countMaxDegree1 - 1) / 2;      return 2 * maxDegree1 - (maxPossibleEdgeCount == edgeCount ? 1 : 0);    }  }  private:  // Returns the number of edges (u, v) where degress[u] == degreeU and  // degrees[v] == degreeV.  int getEdgeCount(const vector<vector<int>>& roads, const vector<int>& degrees,                   int degreeU, int degreeV) {    int edgeCount = 0;    for (const vector<int>& road : roads) {      const int u = road[0];      const int v = road[1];      if (degrees[u] == degreeU && degrees[v] == degreeV)        ++edgeCount;    }    return edgeCount;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗