mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
186 lines
19 KiB
JSON
186 lines
19 KiB
JSON
|
{
|
||
|
"data": {
|
||
|
"question": {
|
||
|
"questionId": "2568",
|
||
|
"questionFrontendId": "2477",
|
||
|
"boundTopicId": null,
|
||
|
"title": "Minimum Fuel Cost to Report to the Capital",
|
||
|
"titleSlug": "minimum-fuel-cost-to-report-to-the-capital",
|
||
|
"content": "<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are 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 road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>\n\n<p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p>\n\n<p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p>\n\n<p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p>\n\n<p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png\" style=\"width: 303px; height: 332px;\" />\n<pre>\n<strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> \n- Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel.\n- Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel.\n- Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel.\nIt costs 3 liters of fuel at minimum. \nIt can be proven that 3 is the minimum number of liters of fuel needed.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/11/16/2.png\" style=\"width: 274px; height: 340px;\" />\n<pre>\n<strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2\n<strong>Output:</strong> 7\n<strong>Explanation:</strong> \n- Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel.\n- Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel.\n- Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel.\n- Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel.\n- Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel.\n- Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel.\n- Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel.\nIt costs 7 liters of fuel at minimum. \nIt can be proven that 7 is the minimum number of liters of fuel needed.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png\" style=\"width: 108px; height: 86px;\" />\n<pre>\n<strong>Input:</strong> roads = [], seats = 1\n<strong>Output:</strong> 0\n<strong>Explanation:</strong> No representatives need to travel to the capital city.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= n <= 10<sup>5</sup></code></li>\n\t<li><code>roads.length == n - 1</code></li>\n\t<li><code>roads[i].length == 2</code></li>\n\t<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>\n\t<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>\n\t<li><code>roads</code> represents a valid tree.</li>\n\t<li><code>1 <= seats <= 10<sup>5</sup></code></li>\n</ul>\n",
|
||
|
"translatedTitle": null,
|
||
|
"translatedContent": null,
|
||
|
"isPaidOnly": false,
|
||
|
"difficulty": "Medium",
|
||
|
"likes": 261,
|
||
|
"dislikes": 5,
|
||
|
"isLiked": null,
|
||
|
"similarQuestions": "[{\"title\": \"Binary Tree Postorder Traversal\", \"titleSlug\": \"binary-tree-postorder-traversal\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
|
||
|
"exampleTestcases": "[[0,1],[0,2],[0,3]]\n5\n[[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]]\n2\n[]\n1",
|
||
|
"categoryTitle": "Algorithms",
|
||
|
"contributors": [],
|
||
|
"topicTags": [
|
||
|
{
|
||
|
"name": "Tree",
|
||
|
"slug": "tree",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
},
|
||
|
{
|
||
|
"name": "Depth-First Search",
|
||
|
"slug": "depth-first-search",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
},
|
||
|
{
|
||
|
"name": "Breadth-First Search",
|
||
|
"slug": "breadth-first-search",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
},
|
||
|
{
|
||
|
"name": "Graph",
|
||
|
"slug": "graph",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
}
|
||
|
],
|
||
|
"companyTagStats": null,
|
||
|
"codeSnippets": [
|
||
|
{
|
||
|
"lang": "C++",
|
||
|
"langSlug": "cpp",
|
||
|
"code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \n }\n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Java",
|
||
|
"langSlug": "java",
|
||
|
"code": "class Solution {\n public long minimumFuelCost(int[][] roads, int seats) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Python",
|
||
|
"langSlug": "python",
|
||
|
"code": "class Solution(object):\n def minimumFuelCost(self, roads, seats):\n \"\"\"\n :type roads: List[List[int]]\n :type seats: int\n :rtype: int\n \"\"\"\n ",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Python3",
|
||
|
"langSlug": "python3",
|
||
|
"code": "class Solution:\n def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:\n ",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "C",
|
||
|
"langSlug": "c",
|
||
|
"code": "long long minimumFuelCost(int** roads, int roadsSize, int* roadsColSize, int seats){\n\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "C#",
|
||
|
"langSlug": "csharp",
|
||
|
"code": "public class Solution {\n public long MinimumFuelCost(int[][] roads, int seats) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "JavaScript",
|
||
|
"langSlug": "javascript",
|
||
|
"code": "/**\n * @param {number[][]} roads\n * @param {number} seats\n * @return {number}\n */\nvar minimumFuelCost = function(roads, seats) {\n \n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Ruby",
|
||
|
"langSlug": "ruby",
|
||
|
"code": "# @param {Integer[][]} roads\n# @param {Integer} seats\n# @return {Integer}\ndef minimum_fuel_cost(roads, seats)\n \nend",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Swift",
|
||
|
"langSlug": "swift",
|
||
|
"code": "class Solution {\n func minimumFuelCost(_ roads: [[Int]], _ seats: Int) -> Int {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Go",
|
||
|
"langSlug": "golang",
|
||
|
"code": "func minimumFuelCost(roads [][]int, seats int) int64 {\n \n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Scala",
|
||
|
"langSlug": "scala",
|
||
|
"code": "object Solution {\n def minimumFuelCost(roads: Array[Array[Int]], seats: Int): Long = {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Kotlin",
|
||
|
"langSlug": "kotlin",
|
||
|
"code": "class Solution {\n fun minimumFuelCost(roads: Array<IntArray>, seats: Int): Long {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Rust",
|
||
|
"langSlug": "rust",
|
||
|
"code": "impl Solution {\n pub fn minimum_fuel_cost(roads: Vec<Vec<i32>>, seats: i32) -> i64 {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "PHP",
|
||
|
"langSlug": "php",
|
||
|
"code": "class Solution {\n\n /**\n * @param Integer[][] $roads\n * @param Integer $seats\n * @return Integer\n */\n function minimumFuelCost($roads, $seats) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "TypeScript",
|
||
|
"langSlug": "typescript",
|
||
|
"code": "function minimumFuelCost(roads: number[][], seats: number): number {\n\n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Racket",
|
||
|
"langSlug": "racket",
|
||
|
"code": "(define/contract (minimum-fuel-cost roads seats)\n (-> (listof (listof exact-integer?)) exact-integer? exact-integer?)\n\n )",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Erlang",
|
||
|
"langSlug": "erlang",
|
||
|
"code": "-spec minimum_fuel_cost(Roads :: [[integer()]], Seats :: integer()) -> integer().\nminimum_fuel_cost(Roads, Seats) ->\n .",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Elixir",
|
||
|
"langSlug": "elixir",
|
||
|
"code": "defmodule Solution do\n @spec minimum_fuel_cost(roads :: [[integer]], seats :: integer) :: integer\n def minimum_fuel_cost(roads, seats) do\n\n end\nend",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Dart",
|
||
|
"langSlug": "dart",
|
||
|
"code": "class Solution {\n int minimumFuelCost(List<List<int>> roads, int seats) {\n\n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
}
|
||
|
],
|
||
|
"stats": "{\"totalAccepted\": \"6.6K\", \"totalSubmission\": \"12.5K\", \"totalAcceptedRaw\": 6601, \"totalSubmissionRaw\": 12470, \"acRate\": \"52.9%\"}",
|
||
|
"hints": [
|
||
|
"Can you record the size of each subtree?",
|
||
|
"If n people meet on the same node, what is the minimum number of cars needed?"
|
||
|
],
|
||
|
"solution": null,
|
||
|
"status": null,
|
||
|
"sampleTestCase": "[[0,1],[0,2],[0,3]]\n5",
|
||
|
"metaData": "{\n \"name\": \"minimumFuelCost\",\n \"params\": [\n {\n \"name\": \"roads\",\n \"type\": \"integer[][]\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"seats\"\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 gnu11 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://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10\\\" target=\\\"_blank\\\">C# 10 with .NET 6 runtime</a></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<
|
||
|
"libraryUrl": null,
|
||
|
"adminUrl": null,
|
||
|
"challengeQuestion": null,
|
||
|
"__typename": "QuestionNode"
|
||
|
}
|
||
|
}
|
||
|
}
|