Problem solution · Python

Maximal Network Rank

Maximal Network Rank: a Python 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
61 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

  • 61 lines of Python from the credited upstream file 1615.py.
  • The implementation visibly relies on sequence storage.
  • No explicit 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 · PythonPython
Use this to learn the idea, then write your own version.
class Solution:  def maximalNetworkRank(self, n: int, roads: list[list[int]]) -> int:    degrees = [0] * n     for u, v in roads:      degrees[u] += 1      degrees[v] += 1     # Find the first maximum and the second maximum degrees.    maxDegree1 = 0    maxDegree2 = 0    for degree in degrees:      if degree > maxDegree1:        maxDegree2 = maxDegree1        maxDegree1 = degree      elif degree > maxDegree2:        maxDegree2 = degree     # There can be multiple nodes with `maxDegree1` or `maxDegree2`.    # Find the counts of such nodes.    countMaxDegree1 = 0    countMaxDegree2 = 0    for degree in degrees:      if degree == maxDegree1:        countMaxDegree1 += 1      elif degree == maxDegree2:        countMaxDegree2 += 1     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.      edgeCount = (self._getEdgeCount(roads, degrees, maxDegree1, maxDegree2) +                   self._getEdgeCount(roads, degrees, maxDegree2, maxDegree1))      return maxDegree1 + maxDegree2 - (countMaxDegree2 == edgeCount)    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.      edgeCount = self._getEdgeCount(roads, degrees, maxDegree1, maxDegree1)      maxPossibleEdgeCount = countMaxDegree1 * (countMaxDegree1 - 1) // 2      return 2 * maxDegree1 - (maxPossibleEdgeCount == edgeCount)   def _getEdgeCount(      self,      roads: list[list[int]],      degrees: list[int],      degreeU: int, degreeV: int,  ) -> int:    """    Returns the number of edges (u, v) where degress[u] == degreeU and    degrees[v] == degreeV.    """    edgeCount = 0    for u, v in roads:      if degrees[u] == degreeU and degrees[v] == degreeV:        edgeCount += 1    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 ↗