java.lang.Object
g2201_2300.s2285_maximum_total_importance_of_roads.Solution

public class Solution extends Object
2285 - Maximum Total Importance of Roads.<p>Medium</p> <p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional</strong> road connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>You need to assign each city with an integer value from <code>1</code> to <code>n</code>, where each value can only be used <strong>once</strong>. The <strong>importance</strong> of a road is then defined as the <strong>sum</strong> of the values of the two cities it connects.</p> <p>Return <em>the <strong>maximum total importance</strong> of all roads possible after assigning the values optimally.</em></p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/04/07/ex1drawio.png" alt="" /></p> <p><strong>Input:</strong> n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]</p> <p><strong>Output:</strong> 43</p> <p><strong>Explanation:</strong> The figure above shows the country and the assigned values of [2,4,5,3,1].</p> <ul> <li> <p>The road (0,1) has an importance of 2 + 4 = 6.</p> </li> <li> <p>The road (1,2) has an importance of 4 + 5 = 9.</p> </li> <li> <p>The road (2,3) has an importance of 5 + 3 = 8.</p> </li> <li> <p>The road (0,2) has an importance of 2 + 5 = 7.</p> </li> <li> <p>The road (1,3) has an importance of 4 + 3 = 7.</p> </li> <li> <p>The road (2,4) has an importance of 5 + 1 = 6.</p> </li> </ul> <p>The total importance of all roads is 6 + 9 + 8 + 7 + 7 + 6 = 43.</p> <p>It can be shown that we cannot obtain a greater total importance than 43.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/04/07/ex2drawio.png" alt="" /></p> <p><strong>Input:</strong> n = 5, roads = [[0,3],[2,4],[1,3]]</p> <p><strong>Output:</strong> 20</p> <p><strong>Explanation:</strong> The figure above shows the country and the assigned values of [4,3,2,5,1].</p> <ul> <li> <p>The road (0,3) has an importance of 4 + 5 = 9.</p> </li> <li> <p>The road (2,4) has an importance of 2 + 1 = 3.</p> </li> <li> <p>The road (1,3) has an importance of 3 + 5 = 8.</p> </li> </ul> <p>The total importance of all roads is 9 + 3 + 8 = 20.</p> <p>It can be shown that we cannot obtain a greater total importance than 20.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= n <= 5 * 10<sup>4</sup></code></li> <li><code>1 <= roads.length <= 5 * 10<sup>4</sup></code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li>There are no duplicate roads.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maximumImportance

      public long maximumImportance(int n, int[][] roads)