mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
187 lines
20 KiB
JSON
187 lines
20 KiB
JSON
|
{
|
||
|
"data": {
|
||
|
"question": {
|
||
|
"questionId": "2846",
|
||
|
"questionFrontendId": "2751",
|
||
|
"boundTopicId": null,
|
||
|
"title": "Robot Collisions",
|
||
|
"titleSlug": "robot-collisions",
|
||
|
"content": "<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p>\n\n<p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>'L'</strong> for <strong>left</strong> or <strong>'R'</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p>\n\n<p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p>\n\n<p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p>\n\n<p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final heath of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p>\n\n<p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p>\n\n<p><strong>Note:</strong> The positions may be unsorted.</p>\n\n<div class=\"notranslate\" style=\"all: initial;\"> </div>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<p><img height=\"169\" src=\"https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png\" width=\"808\" /></p>\n\n<pre>\n<strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRRR"\n<strong>Output:</strong> [2,17,9,15,10]\n<strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10].\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<p><img height=\"176\" src=\"https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png\" width=\"717\" /></p>\n\n<pre>\n<strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"\n<strong>Output:</strong> [14]\n<strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4's health is smaller, it gets removed, and robot 3's health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14].\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<p><img height=\"172\" src=\"https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png\" width=\"732\" /></p>\n\n<pre>\n<strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = "RLRL"\n<strong>Output:</strong> []\n<strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>\n\t<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>\n\t<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>\n\t<li>All values in <code>positions</code> are distinct</li>\n</ul>\n",
|
||
|
"translatedTitle": null,
|
||
|
"translatedContent": null,
|
||
|
"isPaidOnly": false,
|
||
|
"difficulty": "Hard",
|
||
|
"likes": 276,
|
||
|
"dislikes": 9,
|
||
|
"isLiked": null,
|
||
|
"similarQuestions": "[{\"title\": \"Asteroid Collision\", \"titleSlug\": \"asteroid-collision\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||
|
"exampleTestcases": "[5,4,3,2,1]\n[2,17,9,15,10]\n\"RRRRR\"\n[3,5,2,6]\n[10,10,15,12]\n\"RLRL\"\n[1,2,5,6]\n[10,10,11,11]\n\"RLRL\"",
|
||
|
"categoryTitle": "Algorithms",
|
||
|
"contributors": [],
|
||
|
"topicTags": [
|
||
|
{
|
||
|
"name": "Array",
|
||
|
"slug": "array",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
},
|
||
|
{
|
||
|
"name": "Stack",
|
||
|
"slug": "stack",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
},
|
||
|
{
|
||
|
"name": "Sorting",
|
||
|
"slug": "sorting",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
},
|
||
|
{
|
||
|
"name": "Simulation",
|
||
|
"slug": "simulation",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
}
|
||
|
],
|
||
|
"companyTagStats": null,
|
||
|
"codeSnippets": [
|
||
|
{
|
||
|
"lang": "C++",
|
||
|
"langSlug": "cpp",
|
||
|
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n \n }\n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Java",
|
||
|
"langSlug": "java",
|
||
|
"code": "class Solution {\n public List<Integer> survivedRobotsHealths(int[] positions, int[] healths, String directions) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Python",
|
||
|
"langSlug": "python",
|
||
|
"code": "class Solution(object):\n def survivedRobotsHealths(self, positions, healths, directions):\n \"\"\"\n :type positions: List[int]\n :type healths: List[int]\n :type directions: str\n :rtype: List[int]\n \"\"\"\n ",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Python3",
|
||
|
"langSlug": "python3",
|
||
|
"code": "class Solution:\n def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:\n ",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "C",
|
||
|
"langSlug": "c",
|
||
|
"code": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* survivedRobotsHealths(int* positions, int positionsSize, int* healths, int healthsSize, char * directions, int* returnSize){\n\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "C#",
|
||
|
"langSlug": "csharp",
|
||
|
"code": "public class Solution {\n public IList<int> SurvivedRobotsHealths(int[] positions, int[] healths, string directions) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "JavaScript",
|
||
|
"langSlug": "javascript",
|
||
|
"code": "/**\n * @param {number[]} positions\n * @param {number[]} healths\n * @param {string} directions\n * @return {number[]}\n */\nvar survivedRobotsHealths = function(positions, healths, directions) {\n \n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Ruby",
|
||
|
"langSlug": "ruby",
|
||
|
"code": "# @param {Integer[]} positions\n# @param {Integer[]} healths\n# @param {String} directions\n# @return {Integer[]}\ndef survived_robots_healths(positions, healths, directions)\n \nend",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Swift",
|
||
|
"langSlug": "swift",
|
||
|
"code": "class Solution {\n func survivedRobotsHealths(_ positions: [Int], _ healths: [Int], _ directions: String) -> [Int] {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Go",
|
||
|
"langSlug": "golang",
|
||
|
"code": "func survivedRobotsHealths(positions []int, healths []int, directions string) []int {\n \n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Scala",
|
||
|
"langSlug": "scala",
|
||
|
"code": "object Solution {\n def survivedRobotsHealths(positions: Array[Int], healths: Array[Int], directions: String): List[Int] = {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Kotlin",
|
||
|
"langSlug": "kotlin",
|
||
|
"code": "class Solution {\n fun survivedRobotsHealths(positions: IntArray, healths: IntArray, directions: String): List<Int> {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Rust",
|
||
|
"langSlug": "rust",
|
||
|
"code": "impl Solution {\n pub fn survived_robots_healths(positions: Vec<i32>, healths: Vec<i32>, directions: String) -> Vec<i32> {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "PHP",
|
||
|
"langSlug": "php",
|
||
|
"code": "class Solution {\n\n /**\n * @param Integer[] $positions\n * @param Integer[] $healths\n * @param String $directions\n * @return Integer[]\n */\n function survivedRobotsHealths($positions, $healths, $directions) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "TypeScript",
|
||
|
"langSlug": "typescript",
|
||
|
"code": "function survivedRobotsHealths(positions: number[], healths: number[], directions: string): number[] {\n\n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Racket",
|
||
|
"langSlug": "racket",
|
||
|
"code": "(define/contract (survived-robots-healths positions healths directions)\n (-> (listof exact-integer?) (listof exact-integer?) string? (listof exact-integer?))\n\n )",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Erlang",
|
||
|
"langSlug": "erlang",
|
||
|
"code": "-spec survived_robots_healths(Positions :: [integer()], Healths :: [integer()], Directions :: unicode:unicode_binary()) -> [integer()].\nsurvived_robots_healths(Positions, Healths, Directions) ->\n .",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Elixir",
|
||
|
"langSlug": "elixir",
|
||
|
"code": "defmodule Solution do\n @spec survived_robots_healths(positions :: [integer], healths :: [integer], directions :: String.t) :: [integer]\n def survived_robots_healths(positions, healths, directions) do\n\n end\nend",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Dart",
|
||
|
"langSlug": "dart",
|
||
|
"code": "class Solution {\n List<int> survivedRobotsHealths(List<int> positions, List<int> healths, String directions) {\n\n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
}
|
||
|
],
|
||
|
"stats": "{\"totalAccepted\": \"5.3K\", \"totalSubmission\": \"13.5K\", \"totalAcceptedRaw\": 5273, \"totalSubmissionRaw\": 13548, \"acRate\": \"38.9%\"}",
|
||
|
"hints": [
|
||
|
"Process the robots in the order of their positions to ensure that we process the collisions correctly.",
|
||
|
"To optimize the solution, use a stack to keep track of the surviving robots as we iterate through the positions.",
|
||
|
"Instead of simulating each collision, check the current robot against the top of the stack (if it exists) to determine if a collision occurs."
|
||
|
],
|
||
|
"solution": null,
|
||
|
"status": null,
|
||
|
"sampleTestCase": "[5,4,3,2,1]\n[2,17,9,15,10]\n\"RRRRR\"",
|
||
|
"metaData": "{\n \"name\": \"survivedRobotsHealths\",\n \"params\": [\n {\n \"name\": \"positions\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"healths\"\n },\n {\n \"type\": \"string\",\n \"name\": \"directions\"\n }\n ],\n \"return\": {\n \"type\": \"list<integer>\"\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 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
|
||
|
"libraryUrl": null,
|
||
|
"adminUrl": null,
|
||
|
"challengeQuestion": null,
|
||
|
"__typename": "QuestionNode"
|
||
|
}
|
||
|
}
|
||
|
}
|