Robot Collisions - Problem
Imagine a futuristic battlefield where n robots are positioned on a straight line, each with their own health points, position, and movement direction. These robots start moving simultaneously at the same speed - some moving left ('L') and others moving right ('R').
When two robots collide at the same position:
- The robot with lower health is destroyed
- The surviving robot loses 1 health point and continues moving
- If both robots have equal health, both are destroyed
Your mission: Determine which robots survive all collisions and return their final health values in the original input order.
Key Challenge: The positions array may be unsorted, but you need to simulate collisions based on actual spatial positions and movement patterns.
Input & Output
example_1.py โ Basic Collision
$
Input:
positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRLR"
โบ
Output:
[2,17,9,15,10] โ [4,15]
๐ก Note:
Robots are sorted by position: [1,2,3,4,5]. Robot at pos 1 (health 10, R) moves right. Robots at pos 2,3,4 (health 15,9,17, all L) move left. Robot at pos 5 (health 2, R) moves right. The R robot at pos 1 will collide with L robots. After all collisions, only robots with health 4 and 15 survive in original order.
example_2.py โ No Collisions
$
Input:
positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"
โบ
Output:
[10,10,15,12]
๐ก Note:
After sorting by position: [2,3,5,6] with directions [L,R,L,R]. The L-moving robot at pos 2 and R-moving robot at pos 6 move away from others. The R at pos 3 and L at pos 5 move toward each other and will collide, but they have equal health (10 each), so both are destroyed. Wait - let me recalculate: they don't collide because they pass each other.
example_3.py โ All Destroyed
$
Input:
positions = [1,2,5,6], healths = [10,10,11,11], directions = "RLRL"
โบ
Output:
[]
๐ก Note:
Robots at positions 1,2 have healths [10,10] moving [R,L] - they collide and both die (equal health). Robots at positions 5,6 have healths [11,11] moving [R,L] - they also collide and both die. No survivors remain.
Constraints
- 1 โค n โค 105
- 1 โค positions[i] โค 109
- 1 โค healths[i] โค 109
- directions[i] is either 'L' or 'R'
- All positions are unique
- All robots move at the same speed
Visualization
Tap to expand
Understanding the Visualization
1
Deploy Forces
Position robots on the battlefield with their health and movement directions
2
Sort by Position
Arrange robots from left to right to process collisions systematically
3
Stack Right Army
Track right-moving robots using a stack as they advance
4
Process Left Army
When left-moving robots advance, they collide with the right army stack
5
Battle Resolution
Resolve each collision based on health, updating or removing robots
6
Count Survivors
Return surviving robots in their original formation order
Key Takeaway
๐ฏ Key Insight: The stack efficiently simulates collisions by recognizing that only right-moving robots can collide with left-moving robots, eliminating the need for step-by-step position tracking.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code