Importance of Roads: P-Queue in Action
Not super fast solution here, pretty much O(nlogn) for an N=10^4, using pQueue. Here is the gist of it: 1/ It is a graph problem where you need to assign the largest number to the node with the most number of connections 2/ Use a hash table to implement the graph (hash of hash) 3/ Build the graph using #2 4/ Using a pQueue (descended order), add the cities based on the number of connections (as the weight) 5/ Dequeue and assign the different values for the cities ([n..1]) 6/ Traverse the graph again adding up the importance. Need to ensure that the same road isn't counted twice (another hash table of "visited") Code is down below, cheers, ACC. Maximum Total Importance of Roads - LeetCode 2285. Maximum Total Importance of Roads Medium 173 6 Add to List Share 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] = [a i ,...