{x}
blog image

Maximum Total Importance of Roads

You are given an integer n denoting the number of cities in a country. The cities are numbered from 0 to n - 1.

You are also given a 2D integer array roads where roads[i] = [ai, bi] denotes that there exists a bidirectional road connecting cities ai and bi.

You need to assign each city with an integer value from 1 to n, where each value can only be used once. The importance of a road is then defined as the sum of the values of the two cities it connects.

Return the maximum total importance of all roads possible after assigning the values optimally.

 

Example 1:

Input: n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
Output: 43
Explanation: The figure above shows the country and the assigned values of [2,4,5,3,1].
- The road (0,1) has an importance of 2 + 4 = 6.
- The road (1,2) has an importance of 4 + 5 = 9.
- The road (2,3) has an importance of 5 + 3 = 8.
- The road (0,2) has an importance of 2 + 5 = 7.
- The road (1,3) has an importance of 4 + 3 = 7.
- The road (2,4) has an importance of 5 + 1 = 6.
The total importance of all roads is 6 + 9 + 8 + 7 + 7 + 6 = 43.
It can be shown that we cannot obtain a greater total importance than 43.

Example 2:

Input: n = 5, roads = [[0,3],[2,4],[1,3]]
Output: 20
Explanation: The figure above shows the country and the assigned values of [4,3,2,5,1].
- The road (0,3) has an importance of 4 + 5 = 9.
- The road (2,4) has an importance of 2 + 1 = 3.
- The road (1,3) has an importance of 3 + 5 = 8.
The total importance of all roads is 9 + 3 + 8 = 20.
It can be shown that we cannot obtain a greater total importance than 20.

 

Constraints:

  • 2 <= n <= 5 * 104
  • 1 <= roads.length <= 5 * 104
  • roads[i].length == 2
  • 0 <= ai, bi <= n - 1
  • ai != bi
  • There are no duplicate roads.

Solution Explanation: Maximum Total Importance of Roads

This problem asks us to find the maximum total importance of roads in a network of cities by assigning unique integer values (from 1 to n) to each city. The importance of a road is the sum of the values of the cities it connects. The goal is to maximize the sum of the importance of all roads.

Core Idea: A greedy approach works optimally here. The intuition is that cities with more roads connected to them should receive higher values to contribute more significantly to the overall road importance.

Algorithm:

  1. Degree Calculation: We first calculate the degree of each city (the number of roads connected to it). This is stored in an array deg. We iterate through the roads array; for each road [a, b], we increment the degree of city a and city b.

  2. Sorting: We sort the deg array in ascending order. This ensures that cities with fewer connections get smaller values, while cities with more connections receive larger values.

  3. Importance Calculation: We iterate through the sorted deg array. For each city's degree deg[i], we multiply it by (i+1). This assigns the values 1, 2, 3,...n to the cities in increasing order of their degree. This is where the greedy strategy comes into play: cities with higher degrees (more connections) receive larger values, maximizing the overall importance.

  4. Return Value: The sum of these products is the maximum total importance of all roads.

Time and Space Complexity:

  • Time Complexity: O(n log n). The dominant operation is sorting the deg array, which takes O(n log n) time using efficient sorting algorithms. The other operations (degree calculation and importance calculation) take linear time, O(n).

  • Space Complexity: O(n). We use an array deg of size n to store the degrees of the cities.

Code Implementation (Python):

class Solution:
    def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
        deg = [0] * n  # Initialize degree array
        for u, v in roads: # Iterate through roads
            deg[u] += 1   # Increment degree for each city
            deg[v] += 1
        deg.sort()        # Sort degrees in ascending order
        total_importance = 0
        for i in range(n): # Assign values and calculate total importance
            total_importance += deg[i] * (i + 1) # City with degree deg[i] gets value (i+1)
        return total_importance
 

The code in other languages (Java, C++, Go, TypeScript) follows the same algorithm with minor syntactic differences. The core steps of degree calculation, sorting, and importance calculation remain consistent.