1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode/originData/count-ways-to-build-rooms-in-an-ant-colony.json

181 lines
17 KiB
JSON
Raw Normal View History

2022-03-27 18:35:17 +08:00
{
"data": {
"question": {
"questionId": "1313",
"questionFrontendId": "1916",
"boundTopicId": null,
"title": "Count Ways to Build Rooms in an Ant Colony",
"titleSlug": "count-ways-to-build-rooms-in-an-ant-colony",
2023-12-09 18:42:21 +08:00
"content": "<p>You are an ant tasked with adding <code>n</code> new rooms numbered <code>0</code> to <code>n-1</code> to your colony. You are given the expansion plan as a <strong>0-indexed</strong> integer array of length <code>n</code>, <code>prevRoom</code>, where <code>prevRoom[i]</code> indicates that you must build room <code>prevRoom[i]</code> before building room <code>i</code>, and these two rooms must be connected <strong>directly</strong>. Room <code>0</code> is already built, so <code>prevRoom[0] = -1</code>. The expansion&nbsp;plan is given such that once all the rooms are built, every room will be reachable from room <code>0</code>.</p>\r\n\r\n<p>You can only build <strong>one room</strong> at a time, and you can travel freely between rooms you have <strong>already built</strong> only if they are <strong>connected</strong>.&nbsp;You can choose to build <strong>any room</strong> as long as its <strong>previous room</strong>&nbsp;is already built.</p>\r\n\r\n<p>Return <em>the <strong>number of different orders</strong> you can build all the rooms in</em>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>\r\n\r\n<p>&nbsp;</p>\r\n<p><strong class=\"example\">Example 1:</strong></p>\r\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/06/19/d1.JPG\" style=\"width: 200px; height: 212px;\" />\r\n<pre>\r\n<strong>Input:</strong> prevRoom = [-1,0,1]\r\n<strong>Output:</strong> 1\r\n<strong>Explanation:</strong>&nbsp;There is only one way to build the additional rooms: 0 &rarr; 1 &rarr; 2\r\n</pre>\r\n\r\n<p><strong class=\"example\">Example 2:</strong></p>\r\n<strong><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/06/19/d2.JPG\" style=\"width: 200px; height: 239px;\" /></strong>\r\n\r\n<pre>\r\n<strong>Input:</strong> prevRoom = [-1,0,0,1,2]\r\n<strong>Output:</strong> 6\r\n<strong>Explanation:\r\n</strong>The 6 ways are:\r\n0 &rarr; 1 &rarr; 3 &rarr; 2 &rarr; 4\r\n0 &rarr; 2 &rarr; 4 &rarr; 1 &rarr; 3\r\n0 &rarr; 1 &rarr; 2 &rarr; 3 &rarr; 4\r\n0 &rarr; 1 &rarr; 2 &rarr; 4 &rarr; 3\r\n0 &rarr; 2 &rarr; 1 &rarr; 3 &rarr; 4\r\n0 &rarr; 2 &rarr; 1 &rarr; 4 &rarr; 3\r\n</pre>\r\n\r\n<p>&nbsp;</p>\r\n<p><strong>Constraints:</strong></p>\r\n\r\n<ul>\r\n\t<li><code>n == prevRoom.length</code></li>\r\n\t<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>\r\n\t<li><code>prevRoom[0] == -1</code></li>\r\n\t<li><code>0 &lt;= prevRoom[i] &lt; n</code> for all <code>1 &lt;= i &lt; n</code></li>\r\n\t<li>Every room is reachable from room <code>0</code> once all the rooms are built.</li>\r\n</ul>",
2022-03-27 18:35:17 +08:00
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Hard",
2023-12-09 18:42:21 +08:00
"likes": 464,
"dislikes": 50,
2022-03-27 18:35:17 +08:00
"isLiked": null,
2023-12-09 18:42:21 +08:00
"similarQuestions": "[{\"title\": \"Count Anagrams\", \"titleSlug\": \"count-anagrams\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Count the Number of Good Subsequences\", \"titleSlug\": \"count-the-number-of-good-subsequences\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
2022-03-27 18:35:17 +08:00
"exampleTestcases": "[-1,0,1]\n[-1,0,0,1,2]",
"categoryTitle": "Algorithms",
"contributors": [],
"topicTags": [
{
"name": "Math",
"slug": "math",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Dynamic Programming",
"slug": "dynamic-programming",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Tree",
"slug": "tree",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Graph",
"slug": "graph",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Topological Sort",
"slug": "topological-sort",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Combinatorics",
"slug": "combinatorics",
"translatedName": null,
"__typename": "TopicTagNode"
}
],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "C++",
"langSlug": "cpp",
"code": "class Solution {\r\npublic:\r\n int waysToBuildRooms(vector<int>& prevRoom) {\r\n \r\n }\r\n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "Java",
"langSlug": "java",
"code": "class Solution {\r\n public int waysToBuildRooms(int[] prevRoom) {\r\n \r\n }\r\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python",
"langSlug": "python",
"code": "class Solution(object):\r\n def waysToBuildRooms(self, prevRoom):\r\n \"\"\"\r\n :type prevRoom: List[int]\r\n :rtype: int\r\n \"\"\"",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python3",
"langSlug": "python3",
"code": "class Solution:\r\n def waysToBuildRooms(self, prevRoom: List[int]) -> int:",
"__typename": "CodeSnippetNode"
},
{
"lang": "C",
"langSlug": "c",
"code": "int waysToBuildRooms(int* prevRoom, int prevRoomSize){\r\n\r\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "C#",
"langSlug": "csharp",
"code": "public class Solution {\r\n public int WaysToBuildRooms(int[] prevRoom) {\r\n \r\n }\r\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\r\n * @param {number[]} prevRoom\r\n * @return {number}\r\n */\r\nvar waysToBuildRooms = function(prevRoom) {\r\n \r\n};",
"__typename": "CodeSnippetNode"
},
{
2023-12-09 18:42:21 +08:00
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function waysToBuildRooms(prevRoom: number[]): number {\r\n\r\n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "PHP",
"langSlug": "php",
"code": "class Solution {\r\n\r\n /**\r\n * @param Integer[] $prevRoom\r\n * @return Integer\r\n */\r\n function waysToBuildRooms($prevRoom) {\r\n \r\n }\r\n}",
2022-03-27 18:35:17 +08:00
"__typename": "CodeSnippetNode"
},
{
"lang": "Swift",
"langSlug": "swift",
"code": "class Solution {\r\n func waysToBuildRooms(_ prevRoom: [Int]) -> Int {\r\n \r\n }\r\n}",
"__typename": "CodeSnippetNode"
},
2023-12-09 18:42:21 +08:00
{
"lang": "Kotlin",
"langSlug": "kotlin",
"code": "class Solution {\r\n fun waysToBuildRooms(prevRoom: IntArray): Int {\r\n \r\n }\r\n}",
"__typename": "CodeSnippetNode"
},
2022-03-27 18:35:17 +08:00
{
"lang": "Go",
"langSlug": "golang",
"code": "func waysToBuildRooms(prevRoom []int) int {\r\n \r\n}",
"__typename": "CodeSnippetNode"
},
{
2023-12-09 18:42:21 +08:00
"lang": "Ruby",
"langSlug": "ruby",
"code": "# @param {Integer[]} prev_room\r\n# @return {Integer}\r\ndef ways_to_build_rooms(prev_room)\r\n \r\nend",
2022-03-27 18:35:17 +08:00
"__typename": "CodeSnippetNode"
},
{
2023-12-09 18:42:21 +08:00
"lang": "Scala",
"langSlug": "scala",
"code": "object Solution {\r\n def waysToBuildRooms(prevRoom: Array[Int]): Int = {\r\n \r\n }\r\n}",
2022-03-27 18:35:17 +08:00
"__typename": "CodeSnippetNode"
},
{
"lang": "Rust",
"langSlug": "rust",
"code": "impl Solution {\r\n pub fn ways_to_build_rooms(prev_room: Vec<i32>) -> i32 {\r\n \r\n }\r\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Racket",
"langSlug": "racket",
"code": "(define/contract (ways-to-build-rooms prevRoom)\r\n (-> (listof exact-integer?) exact-integer?)\r\n\r\n )",
"__typename": "CodeSnippetNode"
}
],
2023-12-09 18:42:21 +08:00
"stats": "{\"totalAccepted\": \"6.5K\", \"totalSubmission\": \"13.2K\", \"totalAcceptedRaw\": 6502, \"totalSubmissionRaw\": 13213, \"acRate\": \"49.2%\"}",
2022-03-27 18:35:17 +08:00
"hints": [
"Use dynamic programming.",
"Let dp[i] be the number of ways to solve the problem for the subtree of node i.",
"Imagine you are trying to fill an array with the order of traversal, dp[i] equals the multiplications of the number of ways to distribute the subtrees of the children of i on the array using combinatorics, multiplied bu their dp values."
],
"solution": null,
"status": null,
"sampleTestCase": "[-1,0,1]",
"metaData": "{\n \"name\": \"waysToBuildRooms\",\n \"params\": [\n {\n \"name\": \"prevRoom\",\n \"type\": \"integer[]\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n }\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": true,
2023-12-09 18:42:21 +08:00
"envInfo": "{\"cpp\": [\"C++\", \"<p>Compiled with <code> clang 11 </code> using the latest C++ 20 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 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://githu
2022-03-27 18:35:17 +08:00
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}