Imagine a group of autonomous robots positioned on an infinite number line, each starting at specific coordinates. When given a movement command, all robots begin moving simultaneously at a constant speed of 1 unit per second.
You're given:
nums: An array containing the initial positions of robotss: A string where each character represents the initial direction ('L' for left/negative, 'R' for right/positive)d: Number of seconds after the command
Here's the interesting part: when two robots collide, they instantly bounce off each other and reverse directions without losing any time!
Your task is to calculate the sum of distances between all pairs of robots after d seconds. Since this sum can be enormous, return it modulo 109 + 7.
Example collision: Robot at position 0 moving right meets robot at position 2 moving left. After 1 second, both are at position 1, they collide and reverse directions.
Input & Output
Visualization
Time & Space Complexity
Dominated by sorting the final positions. Distance calculation is O(n)
Need array to store final positions
Constraints
- 1 ≤ nums.length ≤ 105
- -109 ≤ nums[i] ≤ 109
- 0 ≤ d ≤ 109
- s consists of 'L' and 'R' only
- s.length == nums.length