Ctrl K

Bouncing Robots

Medium
Robots stand on an infinite number line; positions[i] is the starting coordinate of robot i (all coordinates are distinct). The string directions says where each robot heads once the command is given: L toward smaller coordinates, R toward larger ones. Every robot moves at one unit per second. Whenever two robots end up at the same point, they instantly turn around and keep going in opposite directions. A meeting can happen mid-unit too: robots at 0 and 1 heading toward each other bounce off after half a second and are back at 0 and 1 half a second later. Return the sum of distances over all pairs of robots seconds seconds after the command. Count each pair once. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
positions=[0, 5], directions="RL", seconds=1 → 3

After one second the robots stand at 1 and 4, which are 3 apart.

Example 2
positions=[-1, 1, 4], directions="RLR", seconds=2 → 14

After one second the first two robots meet at 0 and turn around; one second later they are at -1 and 1. The third robot is at 6. The pairwise distances add up to 2 + 7 + 5 = 14.

Constraints
  • 2 ≤ len(positions) ≤ 10⁵
  • -2·10⁹ ≤ positions[i] ≤ 2·10⁹, all values distinct
  • len(directions) = len(positions), made of L and R only
  • 0 ≤ seconds ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStudios for Recording Sessions MediumShuttle Seat Plan MediumArrange for the Busiest Ranges MediumK-th Largest Corner XOR MediumEven Out With Increments Medium
loading Python…
Input
positions=[0, 5], directions="RL", seconds=1
Expected
3

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