Ctrl K

Robots Crashing on a Rail

Hard
n robots stand on a straight rail. Robot i is at point spots[i], has power[i] units of durability and faces heading[i], which is 'L' (left) or 'R' (right). All points are distinct, but the arrays are not necessarily sorted by position. All robots start moving in their directions at the same moment and at the same speed. When two robots end up at the same point, they collide: - the one with less durability is knocked out, and the other loses 1 unit of durability and keeps moving in the same direction; - if their durability is equal, both are knocked out. Once no more collisions can happen, return the durability of the surviving robots in the order the robots appear in the input. If none survive, return an empty list.
Example 1
spots=[8, 2, 5], power=[4, 6, 3], heading="LRR" → [5]

Robot 0 (at 8) heads left and first meets robot 2 (at 5): 4 > 3, so robot 2 is out and robot 0 drops to 3. Next it meets robot 1: 6 > 3, so robot 0 is out and robot 1 drops to 5.

Example 2
spots=[1, 3, 7, 9], power=[5, 5, 2, 8], heading="RLLR" → [2, 8]

Robots 0 and 1 meet with equal power and both drop out. Robot 2 drives off to the left and robot 3 to the right, with nobody in their way.

Example 3
spots=[4, 10], power=[7, 9], heading="LR" → [7, 9]

The robots move apart and never meet.

Constraints
  • 1 ≤ n = len(spots) = len(power) = len(heading) ≤ 10⁵
  • 1 ≤ spots[i], power[i] ≤ 10⁹
  • heading[i] is 'L' or 'R'
  • All spots[i] are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSorting Batches With Repeats HardBusiest Meeting Room HardSecond Larger to the Right HardShortest Server Uptime HardBest Pair Above the Bars Hard
loading Python…
Input
spots=[8, 2, 5], power=[4, 6, 3], heading="LRR"
Expected
[5]

Run checks the first 3 tests in your browser. Submit checks all 138 tests on the server, hidden ones included.