DEV Community

Cover image for 2285. Maximum Total Importance of Roads
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on • Edited on

2285. Maximum Total Importance of Roads

2285. Maximum Total Importance of Roads

Medium

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:

ex1drawio

  • 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:

ex2drawio

  • 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 * 104
  • 1 <= roads.length <= 5 * 104
  • roads[i].length == 2
  • 0 <= ai, bi <= n - 1
  • ai != bi
  • There are no duplicate roads.

Solution:

class Solution {

    /**
     * @param Integer $n
     * @param Integer[][] $roads
     * @return Integer
     */
    function maximumImportance($n, $roads) {
        $ans = 0;
        $count = array_fill(0, $n, 0);

        foreach ($roads as $road) {
            $u = $road[0];
            $v = $road[1];
            ++$count[$u];
            ++$count[$v];
        }

        sort($count);

        for ($i = 0; $i < $n; ++$i) {
            $ans += ($i + 1) * $count[$i];
        }

        return $ans;
    }
}
Enter fullscreen mode Exit fullscreen mode

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

Billboard image

Imagine monitoring that's actually built for developers

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay