> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Robot Collisions Python Solution with Tests

> Tested Python solution for LeetCode 2751 with 30 pytest cases. Generate a practice environment with lcpy.

LeetCode 2751, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Stack](/catalog/topics/stack), [Sorting](/catalog/topics/sorting), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/robot-collisions/description/).

Generate this problem as a practice environment: tested reference solution, 30 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2751   # by problem number
lcpy gen -s robot_collisions   # by problem name
```

## Problem

There are n 1-indexed robots, each having a position on a line, health, and movement direction.

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.

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.

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.

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 health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.

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.

Note: The positions may be unsorted.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png)

```
Input: positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRRR"
Output: [2,17,9,15,10]
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].
```

![Example 2](https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png)

```
Input: positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"
Output: [14]
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].
```

![Example 3](https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png)

```
Input: positions = [1,2,5,6], healths = [10,10,11,11], directions = "RLRL"
Output: []
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, [].
```

### Constraints

* 1 \<= positions.length == healths.length == directions.length == n \<= 10^5
* 1 \<= positions\[i], healths\[i] \<= 10^9
* `directions[i]` is `'L'` or `'R'`
* All values in `positions` are distinct

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/robot_collisions/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/robot_collisions/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def survived_robots_healths(
        self, positions: list[int], healths: list[int], directions: str
    ) -> list[int]:
        remaining = list(healths)
        stack: list[int] = []
        for i in sorted(range(len(positions)), key=positions.__getitem__):
            if directions[i] == "R":
                stack.append(i)
                continue
            while stack and remaining[i] > 0:
                j = stack[-1]
                if remaining[j] > remaining[i]:
                    remaining[j] -= 1
                    remaining[i] = 0
                elif remaining[j] < remaining[i]:
                    stack.pop()
                    remaining[j] = 0
                    remaining[i] -= 1
                else:
                    stack.pop()
                    remaining[j] = 0
                    remaining[i] = 0
        return [remaining[i] for i in range(len(positions)) if remaining[i] > 0]
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.