Similar Problems

Similar Problems not available

Maximum Total Importance Of Roads - Leetcode Solution

Companies:

LeetCode:  Maximum Total Importance Of Roads Leetcode Solution

Difficulty: Medium

Topics: greedy sorting heap-priority-queue graph  

There are N cities in a country, with city numbered from 0 to N-1. Each city[i] contains some importance value, which is the number of people living in that city. You are given a list of roads. road[i] = [a, b] connects city a and city b. The importance value of a road is defined as the sum of importance of all the cities connected with this road. Your task is to find out which road has the maximum importance value. If there are multiple answers, then return the road with the minimum index.

Example 1:

Input: N = 4, roads = [[0,1],[1,2],[2,3]], city = [1,2,3,4] Output: [2,3] Explanation: There are 4 cities as shown above with their respective importance. The roads connecting the cities and their respective importance is as follows: Road connecting cities 0 and 1 has importance 3 (1+2) Road connecting cities 1 and 2 has importance 5 (2+3) Road connecting cities 2 and 3 has importance 7 (3+4) Hence the road with maximum importance is Road 2 connecting Cities 2 and 3.

To solve the problem, we will use a graph data structure. Each city will be represented as a node of the graph and the roads will be represented as edges connecting the nodes. The weight of an edge will be the sum of the importance of the cities connected by the edge.

To find the road with the maximum importance value, we will use a modified version of Dijkstra's algorithm to find the shortest path between two cities. In our case, instead of minimizing the distance between two cities, we will maximize the importance value of the road connecting the two cities.

We will start from any city and set the importance of the start city to 0. We will maintain a priority queue of cities to be visited. The priority queue will store the cities in increasing order of the importance value of the road connecting the city to the start city. We will also maintain an array called parent, which will store the parent city of each visited city. We will initialize the parent of all cities as -1.

We will repeat the following steps until the priority queue is empty:

  1. Pop the city with the maximum importance value from the priority queue.
  2. For each neighbor of the visited city, calculate the importance value of the road connecting the neighbor to the start city as the sum of the importance of the neighbor and the importance value of the road connecting the visited city to the neighbor.
  3. If the calculated importance value is greater than the importance value of the current parent of the neighbor, update the parent of the neighbor to the visited city and insert the neighbor into the priority queue with the new importance value.

After visiting all the cities, we will have computed the importance value of the road connecting each pair of cities. We will then go through the list of roads and find the road with the maximum importance value. If multiple roads have the same maximum importance value, we will return the road with the minimum index.

Maximum Total Importance Of Roads Solution Code

1