Robot Collisions - Problem
There are n robots positioned on a line, each with a unique position, health value, and movement direction.
You are given three inputs:
positions: array of robot positions (0-indexed, unsorted)healths: array of robot health valuesdirections: string where each character is 'L' (left) or 'R' (right)
All robots move simultaneously at the same speed. When two robots collide:
- The robot with lower health is removed
- The surviving robot's health decreases by 1
- If both have equal health, both are removed
Return the final health values of surviving robots in their original input order. If no robots survive, return an empty array.
Input & Output
Example 1 — Basic Collision
$
Input:
positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRRR"
›
Output:
[2,17,9,15,10]
💡 Note:
All robots move right in same direction, so no collisions occur. All survive with original health.
Example 2 — Head-on Collision
$
Input:
positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"
›
Output:
[14]
💡 Note:
After sorting by position: R(H:15) at pos 2, L(H:10) at pos 3, R(H:10) at pos 5, L(H:12) at pos 6. Multiple collisions result in one survivor with health 14.
Example 3 — No Survivors
$
Input:
positions = [1,2,3,4,5], healths = [1,1,1,1,1], directions = "RLRLR"
›
Output:
[]
💡 Note:
Alternating directions with equal health cause all robots to destroy each other in collisions.
Constraints
- 1 ≤ n ≤ 105
- 1 ≤ positions[i] ≤ 109
- 1 ≤ healths[i] ≤ 109
- directions[i] is either 'L' or 'R'
- All values in positions are distinct
Visualization
Tap to expand
Understanding the Visualization
1
Initial Setup
Robots positioned on line with health and direction
2
Collision Detection
Find robots moving toward each other
3
Battle Resolution
Weaker robot removed, survivor's health decreases
4
Final Survivors
Return remaining robots' health in original order
Key Takeaway
🎯 Key Insight: Only R→L collisions matter - use stack to efficiently process robots by position
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code