-
Notifications
You must be signed in to change notification settings - Fork 2
/
2731.cpp
32 lines (31 loc) · 966 Bytes
/
2731.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
int sumDistance(vector<int>& nums, string s, int d) {
int n = nums.size();
vector<long long> newLocation(n, 0);
for (int i = 0; i < n; ++i) {
if (s[i] == 'L') {
newLocation[i] = static_cast<long long>(nums[i]) - d;
}
else {
newLocation[i] = static_cast<long long>(nums[i]) + d;
}
}
long long res = 0;
long long mod = 1e9 + 7;
sort(newLocation.begin(), newLocation.end());
for (int i = 0; i < n - 1; ++i) {
long long left = 1 + i;
long long right = n - left;
long long distance = newLocation[i + 1] - newLocation[i];
distance %= mod;
distance *= left;
distance %= mod;
distance *= right;
distance %= mod;
res += distance;
res %= mod;
}
return res;
}
};