Problem

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.

Examples

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.

Solution

Method 1 - Count the degrees

  • To maximize the total importance of all roads, we assign the highest number to the city with highest number of connection with neighbours i.e. highest degree.
  • So, give maximum number to the node with maximum degree (edges connected to it).

So, here are the steps we follow:

  1. We compute the degree of each node, and
  2. then sort them.
  3. Finally, we assign increasing values starting from the smallest degree.

Here is the video explanation:

Code

Java
class Solution {
    public long maximumImportance(int n, int[][] roads) {
		int degrees[] = new int[n];
        for(int road[] : roads){
            degrees[road[0]]++; 
			degrees[road[1]]++;
        }
        
        Arrays.sort(degrees);

        long ans = 0;
        int num = 1;
        for(int degree : degrees) {
	        ans +=  (long) degree * (num++) ;
	    }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(e + n log n) where n is number of nodes, and e is number of edges.
  • 🧺 Space complexity: O(n) for storing degrees

Dry Run

Lets dry run above code for eg 1. i.e.

n = 5 and roads =[[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]].
Calculate Degrees

This is how the degrees array will look after going through roads:

degrees = [2, 3, 4, 2, 1]
Sort the degrees

Now, we sort the degrees:

[1, 2, 2, 3, 4]
Calculate max importance
For degree = 1
ans += (long) 1 * 1;  // ans = 0 + 1*1 = 1 num++;  // num = 2 // ans = 1
For degree = 2
ans += (long) 2 * 2;  // ans = 1 + 2*2 = 5 num++;  // num = 3 // ans = 5
For degree = 2
ans += (long) 2 * 3;  // ans = 5 + 2*3 = 11 num++;  // num = 4 // ans = 11
For degree = 3
ans += (long) 3 * 4;  // ans = 11 + 3*4 = 23 num++;  // num = 5 // ans = 23
For degree = 4
ans += (long) 4 * 5;  // ans = 23 + 4*5 = 43 num++;  // num = 6 // ans = 43
Final Output
ans = 43