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 * 1041 <= roads.length <= 5 * 104roads[i].length == 20 <= ai, bi <= n - 1ai != bi- There are no duplicate roads.
Approach 01:
-
C++
-
Python
class Solution {
public:
long long maximumImportance(int n, vector<vector<int>>& roads) {
long result = 0;
vector<long> count(n);
for (const vector<int>& road : roads) {
const int u = road[0];
const int v = road[1];
++count[u];
++count[v];
}
ranges::sort(count);
for (int i = 0; i < n; ++i)
result += (i + 1) * count[i];
return result;
}
};
class Solution:
def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
result = 0
count = [0] * n # Initialize count array with zeros for each city
for road in roads:
u, v = road # Unpack road into city indices
count[u] += 1 # Increment count for city u
count[v] += 1 # Increment count for city v
count.sort() # Sort the count array in ascending order
for i in range(n):
result += (i + 1) * count[i] # Calculate importance contribution
return result
Time Complexity
-
Initialization of
countVector: A vectorcountof size \( n \) is initialized with zeros to keep track of the degree of each node.Time Complexity: \( O(n) \)
-
Counting Degrees of Nodes: Iterate through each road to count the degrees of the nodes connected by the roads. Increment the count for each node connected by a road.
Time Complexity: \( O(E) \), where \( E \) is the number of roads (or edges).
-
Sorting the
countVector: Sort thecountvector to assign the highest importance to the most connected nodes.Time Complexity: \( O(n \log n) \)
-
Calculating the Result: Iterate through the sorted
countvector to compute the final result based on the assigned importance.Time Complexity: \( O(n) \)
-
Overall Time Complexity: \( O(n) + O(E) + O(n \log n) + O(n) \approx O(n \log n + E) \)
Space Complexity
-
Space for
countVector: The space required for thecountvector is \( O(n) \). -
Other Variables: The space used by other variables (
result,u,v, and the loop variables) is constant, i.e., \( O(1) \). -
Overall Space Complexity: \( O(n) \)