1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode/originData/maximum-total-importance-of-roads.json
2022-06-10 23:34:17 +08:00

180 lines
17 KiB
JSON

{
"data": {
"question": {
"questionId": "2379",
"questionFrontendId": "2285",
"boundTopicId": null,
"title": "Maximum Total Importance of Roads",
"titleSlug": "maximum-total-importance-of-roads",
"content": "<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>\n\n<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>\n\n<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>\n\n<p>Return <em>the <strong>maximum total importance</strong> of all roads possible after assigning the values optimally.</em></p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/04/07/ex1drawio.png\" style=\"width: 290px; height: 215px;\" />\n<pre>\n<strong>Input:</strong> n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]\n<strong>Output:</strong> 43\n<strong>Explanation:</strong> The figure above shows the country and the assigned values of [2,4,5,3,1].\n- The road (0,1) has an importance of 2 + 4 = 6.\n- The road (1,2) has an importance of 4 + 5 = 9.\n- The road (2,3) has an importance of 5 + 3 = 8.\n- The road (0,2) has an importance of 2 + 5 = 7.\n- The road (1,3) has an importance of 4 + 3 = 7.\n- The road (2,4) has an importance of 5 + 1 = 6.\nThe total importance of all roads is 6 + 9 + 8 + 7 + 7 + 6 = 43.\nIt can be shown that we cannot obtain a greater total importance than 43.\n</pre>\n\n<p><strong>Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/04/07/ex2drawio.png\" style=\"width: 281px; height: 151px;\" />\n<pre>\n<strong>Input:</strong> n = 5, roads = [[0,3],[2,4],[1,3]]\n<strong>Output:</strong> 20\n<strong>Explanation:</strong> The figure above shows the country and the assigned values of [4,3,2,5,1].\n- The road (0,3) has an importance of 4 + 5 = 9.\n- The road (2,4) has an importance of 2 + 1 = 3.\n- The road (1,3) has an importance of 3 + 5 = 8.\nThe total importance of all roads is 9 + 3 + 8 = 20.\nIt can be shown that we cannot obtain a greater total importance than 20.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>\n\t<li><code>1 &lt;= roads.length &lt;= 5 * 10<sup>4</sup></code></li>\n\t<li><code>roads[i].length == 2</code></li>\n\t<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= n - 1</code></li>\n\t<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>\n\t<li>There are no duplicate roads.</li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 223,
"dislikes": 6,
"isLiked": null,
"similarQuestions": "[]",
"exampleTestcases": "5\n[[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]\n5\n[[0,3],[2,4],[1,3]]",
"categoryTitle": "Algorithms",
"contributors": [],
"topicTags": [
{
"name": "Greedy",
"slug": "greedy",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Graph",
"slug": "graph",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Sorting",
"slug": "sorting",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Heap (Priority Queue)",
"slug": "heap-priority-queue",
"translatedName": null,
"__typename": "TopicTagNode"
}
],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "C++",
"langSlug": "cpp",
"code": "class Solution {\npublic:\n long long maximumImportance(int n, vector<vector<int>>& roads) {\n \n }\n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "Java",
"langSlug": "java",
"code": "class Solution {\n public long maximumImportance(int n, int[][] roads) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python",
"langSlug": "python",
"code": "class Solution(object):\n def maximumImportance(self, n, roads):\n \"\"\"\n :type n: int\n :type roads: List[List[int]]\n :rtype: int\n \"\"\"\n ",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python3",
"langSlug": "python3",
"code": "class Solution:\n def maximumImportance(self, n: int, roads: List[List[int]]) -> int:\n ",
"__typename": "CodeSnippetNode"
},
{
"lang": "C",
"langSlug": "c",
"code": "\n\nlong long maximumImportance(int n, int** roads, int roadsSize, int* roadsColSize){\n\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "C#",
"langSlug": "csharp",
"code": "public class Solution {\n public long MaximumImportance(int n, int[][] roads) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {number} n\n * @param {number[][]} roads\n * @return {number}\n */\nvar maximumImportance = function(n, roads) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "Ruby",
"langSlug": "ruby",
"code": "# @param {Integer} n\n# @param {Integer[][]} roads\n# @return {Integer}\ndef maximum_importance(n, roads)\n \nend",
"__typename": "CodeSnippetNode"
},
{
"lang": "Swift",
"langSlug": "swift",
"code": "class Solution {\n func maximumImportance(_ n: Int, _ roads: [[Int]]) -> Int {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Go",
"langSlug": "golang",
"code": "func maximumImportance(n int, roads [][]int) int64 {\n \n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Scala",
"langSlug": "scala",
"code": "object Solution {\n def maximumImportance(n: Int, roads: Array[Array[Int]]): Long = {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Kotlin",
"langSlug": "kotlin",
"code": "class Solution {\n fun maximumImportance(n: Int, roads: Array<IntArray>): Long {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Rust",
"langSlug": "rust",
"code": "impl Solution {\n pub fn maximum_importance(n: i32, roads: Vec<Vec<i32>>) -> i64 {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "PHP",
"langSlug": "php",
"code": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[][] $roads\n * @return Integer\n */\n function maximumImportance($n, $roads) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function maximumImportance(n: number, roads: number[][]): number {\n\n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "Racket",
"langSlug": "racket",
"code": "(define/contract (maximum-importance n roads)\n (-> exact-integer? (listof (listof exact-integer?)) exact-integer?)\n\n )",
"__typename": "CodeSnippetNode"
},
{
"lang": "Erlang",
"langSlug": "erlang",
"code": "-spec maximum_importance(N :: integer(), Roads :: [[integer()]]) -> integer().\nmaximum_importance(N, Roads) ->\n .",
"__typename": "CodeSnippetNode"
},
{
"lang": "Elixir",
"langSlug": "elixir",
"code": "defmodule Solution do\n @spec maximum_importance(n :: integer, roads :: [[integer]]) :: integer\n def maximum_importance(n, roads) do\n\n end\nend",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"13.5K\", \"totalSubmission\": \"22.7K\", \"totalAcceptedRaw\": 13467, \"totalSubmissionRaw\": 22679, \"acRate\": \"59.4%\"}",
"hints": [
"Consider what each city contributes to the total importance of all roads.",
"Based on that, how can you sort the cities such that assigning them values in that order will yield the maximum total importance?"
],
"solution": null,
"status": null,
"sampleTestCase": "5\n[[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]",
"metaData": "{\n \"name\": \"maximumImportance\",\n \"params\": [\n {\n \"name\": \"n\",\n \"type\": \"integer\"\n },\n {\n \"type\": \"integer[][]\",\n \"name\": \"roads\"\n }\n ],\n \"return\": {\n \"type\": \"long\"\n }\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": true,
"envInfo": "{\"cpp\": [\"C++\", \"<p>Compiled with <code> clang 11 </code> using the latest C++ 17 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\"], \"java\": [\"Java\", \"<p><code> OpenJDK 17 </code>. Java 8 features such as lambda expressions and stream API can be used. </p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>\"], \"python\": [\"Python\", \"<p><code>Python 2.7.12</code>.</p>\\r\\n\\r\\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\\\"https://docs.python.org/2/library/array.html\\\" target=\\\"_blank\\\">array</a>, <a href=\\\"https://docs.python.org/2/library/bisect.html\\\" target=\\\"_blank\\\">bisect</a>, <a href=\\\"https://docs.python.org/2/library/collections.html\\\" target=\\\"_blank\\\">collections</a>. If you need more libraries, you can import it yourself.</p>\\r\\n\\r\\n<p>For Map/TreeMap data structure, you may use <a href=\\\"http://www.grantjenks.com/docs/sortedcontainers/\\\" target=\\\"_blank\\\">sortedcontainers</a> library.</p>\\r\\n\\r\\n<p>Note that Python 2.7 <a href=\\\"https://www.python.org/dev/peps/pep-0373/\\\" target=\\\"_blank\\\">will not be maintained past 2020</a>. For the latest Python, please choose Python3 instead.</p>\"], \"c\": [\"C\", \"<p>Compiled with <code>gcc 8.2</code> using the gnu99 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level one optimization (<code>-O1</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n\\r\\n<p>For hash table operations, you may use <a href=\\\"https://troydhanson.github.io/uthash/\\\" target=\\\"_blank\\\">uthash</a>. \\\"uthash.h\\\" is included by default. Below are some examples:</p>\\r\\n\\r\\n<p><b>1. Adding an item to a hash.</b>\\r\\n<pre>\\r\\nstruct hash_entry {\\r\\n int id; /* we'll use this field as the key */\\r\\n char name[10];\\r\\n UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>2. Looking up an item in a hash:</b>\\r\\n<pre>\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n return s;\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>3. Deleting an item in a hash:</b>\\r\\n<pre>\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n HASH_DEL(users, user); \\r\\n}\\r\\n</pre>\\r\\n</p>\"], \"csharp\": [\"C#\", \"<p><a href=\\\"https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9\\\" target=\\\"_blank\\\">C# 10 with .NET 6 runtime</a></p>\\r\\n\\r\\n<p>Your code is compiled with debug flag enabled (<code>/debug</code>).</p>\"], \"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use <a href=\\\"https://github.com/datastructures-js/priority-queue\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and <a href=\\\"https://github.com/datastructures-js/queue\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"ruby\": [\"Ruby\", \"<p><code>Ruby 3.1</code></p>\\r\\n\\r\\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>\"], \"swift\": [\"Swift\", \"<p><code>Swift 5.5.2</code>.</p>\"], \"golang\": [\"Go\", \"<p><code>Go 1.17.6</code>.</p>\\r\\n\\r\\n<p>Support <a href=\\\"https://godoc.org/github.com/emirpasic/gods\\\" target=\\\"_blank\\\">https://godoc.org/github.com/emirpasic/gods</a> library.</p>\"], \"python3\": [\"Python3\", \"<p><code>Python 3.10</code>.</p>\\r\\n\\r\\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\\\"https://docs.python.org/3/library/array.html\\\" target=\\\"_blank\\\">array</a>, <a href=\\\"https://docs.python.org/3/library/bisect.html\\\" target=\\\"_blank\\\">bisect</a>, <a href=\\\"https://docs.python.org/3/library/collections.html\\\" target=\\\"_blank\\\">collections</a>. If you need more libraries, you can import it yourself.</p>\\r\\n\\r\\n<p>For Map/TreeMap data structure, you may use <a href=\\\"http://www.grantjenks.com/docs/sortedcontainers/\\\" target=\\\"_blank\\\">sortedcontainers</a> library.</p>\"], \"scala\": [\"Scala\", \"<p><code>Scala 2.13.7</code>.</p>\"], \"kotlin\": [\"Kotlin\", \"<p><code>Kotlin 1.3.10</code>.</p>\"], \"rust\": [\"Rust\", \"<p><code>Rust 1.58.1</code></p>\\r\\n\\r\\n<p>Supports <a href=\\\"https://crates.io/crates/rand\\\" target=\\\"_blank\\\">rand </a> v0.6\\u00a0from crates.io</p>\"], \"php\": [\"PHP\", \"<p><code>PHP 8.1</code>.</p>\\r\\n<p>With bcmath module</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"], \"racket\": [\"Racket\", \"<p>Run with <code>Racket 8.3</code>.</p>\"], \"erlang\": [\"Erlang\", \"Erlang/OTP 24.2\"], \"elixir\": [\"Elixir\", \"Elixir 1.13.0 with Erlang/OTP 24.2\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}