Some robots are standing on an infinite number line with their initial coordinates given by a 0-indexed integer array nums and will start moving once given the command to move. The robots will move a unit distance each second.
You are given a string s denoting the direction in which robots will move on command. ‘L’ means the robot will move towards the left side or negative side of the number line, whereas ‘R’ means the robot will move towards the right side or positive side of the number line.
If two robots collide, they will start moving in opposite directions.
Return the sum of distances between all the pairs of robots d seconds after the command. Since the sum can be very large, return it modulo 10^9 + 7
.
Note:
The first intuition was all collision events that can occur are actually non-events when looking at the collective robot system. In simpler terms, instead of assuming the robots collide and change directions, you can just assume the robots don’t collide and “pass through” each other.
d
distance to the left or right depending on the direction of it’s movement.d
seconds we can say the ith robot will move to either nums[i]+d
if direction is R
or nums[i]-d
if it’s L
.f[i] - f[i-1]
(f
represents the final positions) will be considered once in i*(n-i)
pairs where n is the total number of robots. So we can add (f[i] - f[i-1])*(i*(n-i))
for each consecutive distance.class Solution {
private:
int MOD = 1000000007;
public:
int sumDistance(vector<int>& nums, string s, int d) {
long long int dist = 0LL;
vector<long long int> f;
// Calculate final positions
for(int i=0;i<nums.size();++i) {
if(s[i] == 'R') f.push_back(nums[i] + d);
else f.push_back(nums[i] - d);
}
// Sort final positions
sort(f.begin(), f.end());
for(int i=1;i<f.size();++i) {
// Add contribution of each consecutive distance
dist += ((f[i] - f[i-1])*((i*(f.size()-i))%MOD))%MOD;
dist %= MOD;
}
return dist;
}
};
O(NlogN)
O(N)
extra space to store final positions