Skip to content
neoideasblog

neoideasblog

  • Home
    • Privacy Policy
    • About Us
    • Contact Us
    • Terms
  • Trivia
  • Results
  • Admit Cards
  • Sitemaps
  • Toggle search form

[Question] Maximum Total Importance of Roads solution leetcode

Posted on May 28, 2022May 28, 2022 By Admin No Comments on [Question] Maximum Total Importance of Roads solution leetcode

You are given an integer n denoting the number of cities in a country. The cities are numbered from  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.

[Question] Maximum Total Importance of Roads solution leetcode

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.

ALSO READ:-   [Question] What Is Matilda’s Last Name In The Novel “Matilda”?

Return the maximum total importance of all roads possible after assigning the values optimally.

Example 1:

Input: n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
Output: 43
 Maximum Total Importance of Roads solution leetcode
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: Maximum Total Importance of Roads solution leetcode

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.
ALSO READ:-   [Answer] “Spasibo” Means “Thank You'' In What Language?

SOLUTION

“Click here“

Admin

I am the Founder and the creator of the blog neoideasblog.com where we share latest trivia questions and answers on a daily basis.

Trivia

Post navigation

Previous Post: [Question] What does UNESCO stand for?
Next Post: [Ques] Wendy Kaufman became famous as what beverage spokesperson?

Related Posts

[Question] What is a synonym for “reliquary”? Trivia
[Question] The Kentucky Derby is part of which horseracing feat? Trivia
Consecutive Points Segment codeforces solution Trivia
[Question] "For Whom the Bell Tolls" draws on Hemingway’s experience from what war? Trivia
[Question] Aleph, bet, and gimel represent one, two, and three in what system? Trivia
[Question] "Edo" Is The Former Name Of Which World Capital? Trivia

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • June 2022
  • May 2022
  • April 2022

Categories

  • Admit Cards
  • Results
  • Trivia

Disclaimer

Disclaimer: Our website name Neoideasblog.com. we are just a news portal that covers various updates and stories.

Copyright © 2022 neoideasblog.

Powered by PressBook Grid Blogs theme