|
| 1 | +# [2751. Robot Collisions][title] |
| 2 | + |
| 3 | +## Description |
| 4 | +There are `n` **1-indexed** robots, each having a position on a line, health, and movement direction. |
| 5 | + |
| 6 | +You are given **0-indexed** integer arrays `positions`, `healths`, and a string `directions` (`directions[i]` is either **'L'** for **left** or **'R'** for **right**). All integers in `positions` are **unique**. |
| 7 | + |
| 8 | +All robots start moving on the line **simultaneously** at the **same speed** in their given directions. If two robots ever share the same position while moving, they will **collide**. |
| 9 | + |
| 10 | +If two robots collide, the robot with **lower health** is **removed** from the line, and the health of the other robot **decreases by one**. The surviving robot continues in the **same** direction it was going. If both robots have the **same** health, they are both removed from the line. |
| 11 | + |
| 12 | +Your task is to determine the **health** of the robots that survive the collisions, in the same **order** that the robots were given, 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. |
| 13 | + |
| 14 | +Return an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur. |
| 15 | + |
| 16 | +**Note**: The positions may be unsorted. |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +``` |
| 23 | +Input: positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRRR" |
| 24 | +Output: [2,17,9,15,10] |
| 25 | +Explanation: 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]. |
| 26 | +``` |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +``` |
| 33 | +Input: positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL" |
| 34 | +Output: [14] |
| 35 | +Explanation: 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]. |
| 36 | +``` |
| 37 | + |
| 38 | +**Example 3:** |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +``` |
| 43 | +Input: positions = [1,2,5,6], healths = [10,10,11,11], directions = "RLRL" |
| 44 | +Output: [] |
| 45 | +Explanation: 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, []. |
| 46 | +``` |
| 47 | + |
| 48 | +## 结语 |
| 49 | + |
| 50 | +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] |
| 51 | + |
| 52 | +[title]: https://leetcode.com/problems/robot-collisions |
| 53 | +[me]: https://github.com/kylesliu/awesome-golang-algorithm |
0 commit comments