Tap here to switch tabs
Problem
Submissions

Problem: Robot Collisions

med
30 min
Try to solve the Robot Collisions problem.

Statement

There are nn robots, each positioned on a one dimensional line. You are given three arrays: positions, healths, and a string directions, where positions[i], healths[i], and directions[i] represent the position, health, and movement direction of the iextthi^{ ext{th}} robot respectively.

All robots start moving simultaneously at the same speed. A robot moves to the left if its direction is 'L' and to the right if its direction is 'R'.

When two robots moving towards each other collide:

  • The robot with lower health is destroyed, and the health of the surviving robot decreases by 11. The surviving robot continues moving in its original direction.

  • If both robots have the exact same health, both are destroyed.

Robots moving in the same direction or moving away from each other never collide.

Return an array containing the health of the surviving robots, ordered as they appeared in the initial input.

Constraints:

  • 1≤1 \leq positions.length ==== healths.length ==== directions.length ==n≤105== n \leq 10^5

  • 1≤1 \leq positions[i], healths[i] ≤109\leq 10^9

  • directions[i] ==== 'L' or directions[i] ==== 'R'

  • All values in positions are distinct

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Robot Collisions

med
30 min
Try to solve the Robot Collisions problem.

Statement

There are nn robots, each positioned on a one dimensional line. You are given three arrays: positions, healths, and a string directions, where positions[i], healths[i], and directions[i] represent the position, health, and movement direction of the iextthi^{ ext{th}} robot respectively.

All robots start moving simultaneously at the same speed. A robot moves to the left if its direction is 'L' and to the right if its direction is 'R'.

When two robots moving towards each other collide:

  • The robot with lower health is destroyed, and the health of the surviving robot decreases by 11. The surviving robot continues moving in its original direction.

  • If both robots have the exact same health, both are destroyed.

Robots moving in the same direction or moving away from each other never collide.

Return an array containing the health of the surviving robots, ordered as they appeared in the initial input.

Constraints:

  • 1≤1 \leq positions.length ==== healths.length ==== directions.length ==n≤105== n \leq 10^5

  • 1≤1 \leq positions[i], healths[i] ≤109\leq 10^9

  • directions[i] ==== 'L' or directions[i] ==== 'R'

  • All values in positions are distinct