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
Robot Battlefield - Stack AlgorithmBattle LineInitial Setup:5Pos: 13Pos: 32Pos: 5Stack (Right Army):H: 5H: 3TOPCollision Processing:Left robot (H=2) attacks stack top (H=3)โ€ข Stack robot stronger: 3 > 2โ€ข Left robot destroyed โŒโ€ข Stack robot health: 3 โ†’ 2โœ“ Stack robot survivesBattle Survivors:H: 5H: 2Return in original order: [5, 2]Algorithm Steps:1. Sort by position2. Stack right-movers3. Process left-movers4. Resolve collisions5. Return survivorsTime: O(n log n)Space: O(n)
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.
Asked in
Google 45 Amazon 38 Meta 31 Microsoft 25 Apple 18
78.6K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen