Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Robot Collisions

XPChallenge Points: 30
levelLevel: Hard

You have robots on a line with unique positions[i], initial healths[i], and directions[i] in 'L' or 'R'. All move simultaneously at the same speed.
Whenever a right-moving robot meets a left-moving robot, they collide:

  • The one with lower health is removed; the other’s health decreases by 1 and continues.

  • If healths are equal, both are removed.

Return the remaining robots’ healths in the original input order (skip removed). If none survive, return [].

Key idea: Sort robots by position. Sweep left→right while maintaining a stack of indices of right-moving robots. When a left robot appears, repeatedly resolve collisions against the top of the stack until one side is gone or no more conflicts. Track mutated healths; at the end, output survivors in original order.

Example 1:

Input: positions = [2, 10, 7] healths = [5, 3, 4] directions = "RLL"

Output: [3]

Explanation:

Sort by position → (2,R,5),(7,L,4),(10,L,3). R(5) vs L(4) → L dies, R→4; then R(4) vs L(3) → L dies, R→3. Only robot at original index 0 survives with health 3.

Example 2:

Input: positions = [4, 1, 3] healths = [2, 2, 1] directions = "LLL"

Output: [2, 2, 1]

Explanation:

All move left → no collisions. Return healths in original order.

Example 3:

Input: positions = [1, 4, 6] healths = [3, 3, 2] directions = "RRL"

Output: [3, 2]

Explanation:

Sorted → (1,R,3),(4,R,3),(6,L,2). L(2) meets R(3 at pos4) → L dies, that R →2. No more conflicts. Survivors: original idx 0 (3), idx 1 (2) → [3,2].

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result