Skip to content

Commit 4fe30d8

Browse files
authored
feat: add solutions to lc problem: No.2731 (doocs#1393)
No.2731.Movement of Robots
1 parent 9edb1a4 commit 4fe30d8

File tree

8 files changed

+262
-8
lines changed

8 files changed

+262
-8
lines changed

solution/2700-2799/2731.Movement of Robots/README.md

+91-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<li>当机器人相撞时,它们 <strong>立即改变</strong>&nbsp;它们的前进时间,这个过程不消耗任何时间。</li>
2222
<li>
2323
<p>当两个机器人在同一时刻占据相同的位置时,就会相撞。</p>
24-
2524
<ul>
2625
<li>
2726
<p>例如,如果一个机器人位于位置 0 并往右移动,另一个机器人位于位置 2 并往左移动,下一秒,它们都将占据位置 1,并改变方向。再下一秒钟后,第一个机器人位于位置 0 并往左移动,而另一个机器人位于位置 2 并往右移动。</p>
@@ -31,7 +30,6 @@
3130
</li>
3231
</ul>
3332
</li>
34-
3533
</ul>
3634

3735
<p>&nbsp;</p>
@@ -79,34 +77,122 @@
7977

8078
<!-- 这里可写通用的实现逻辑 -->
8179

80+
**方法一:脑筋急转弯 + 排序**
81+
82+
两个机器人相撞后,它们会立即改变方向,实际上相当于两个机器人继续往原来的方向移动。因此,我们遍历数组 $nums$,按照字符串 $s$ 的指令,将每个机器人的位置加上或减去 $d$,然后对数组 $nums$ 进行排序。
83+
84+
接下来,我们从小到大枚举每个机器人的位置,计算出当前机器人与前面所有机器人的距离之和,即为答案。
85+
86+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是机器人的数目。
87+
8288
<!-- tabs:start -->
8389

8490
### **Python3**
8591

8692
<!-- 这里可写当前语言的特殊实现逻辑 -->
8793

8894
```python
89-
95+
class Solution:
96+
def sumDistance(self, nums: List[int], s: str, d: int) -> int:
97+
mod = 10**9 + 7
98+
for i, c in enumerate(s):
99+
nums[i] += d if c == "R" else -d
100+
nums.sort()
101+
ans = s = 0
102+
for i, x in enumerate(nums):
103+
ans += i * x - s
104+
s += x
105+
return ans % mod
90106
```
91107

92108
### **Java**
93109

94110
<!-- 这里可写当前语言的特殊实现逻辑 -->
95111

96112
```java
97-
113+
class Solution {
114+
public int sumDistance(int[] nums, String s, int d) {
115+
int n = nums.length;
116+
long[] arr = new long[n];
117+
for (int i = 0; i < n; ++i) {
118+
arr[i] = (long) nums[i] + (s.charAt(i) == 'L' ? -d : d);
119+
}
120+
Arrays.sort(arr);
121+
long ans = 0, sum = 0;
122+
final int mod = (int) 1e9 + 7;
123+
for (int i = 0; i < n; ++i) {
124+
ans = (ans + i * arr[i] - sum) % mod;
125+
sum += arr[i];
126+
}
127+
return (int) ans;
128+
}
129+
}
98130
```
99131

100132
### **C++**
101133

102134
```cpp
103-
135+
class Solution {
136+
public:
137+
int sumDistance(vector<int>& nums, string s, int d) {
138+
int n = nums.size();
139+
vector<long long> arr(n);
140+
for (int i = 0; i < n; ++i) {
141+
arr[i] = 1LL * nums[i] + (s[i] == 'L' ? -d : d);
142+
}
143+
sort(arr.begin(), arr.end());
144+
long long ans = 0;
145+
long long sum = 0;
146+
const int mod = 1e9 + 7;
147+
for (int i = 0; i < n; ++i) {
148+
ans = (ans + i * arr[i] - sum) % mod;
149+
sum += arr[i];
150+
}
151+
return ans;
152+
}
153+
};
104154
```
105155
106156
### **Go**
107157
108158
```go
159+
func sumDistance(nums []int, s string, d int) (ans int) {
160+
for i, c := range s {
161+
if c == 'R' {
162+
nums[i] += d
163+
} else {
164+
nums[i] -= d
165+
}
166+
}
167+
sort.Ints(nums)
168+
sum := 0
169+
const mod int = 1e9 + 7
170+
for i, x := range nums {
171+
ans = (ans + i*x - sum) % mod
172+
sum += x
173+
}
174+
return
175+
}
176+
```
109177

178+
### **TypeScript**
179+
180+
```ts
181+
function sumDistance(nums: number[], s: string, d: number): number {
182+
const n = nums.length;
183+
for (let i = 0; i < n; ++i) {
184+
nums[i] += s[i] === 'L' ? -d : d;
185+
}
186+
nums.sort((a, b) => a - b);
187+
let ans = 0;
188+
let sum = 0;
189+
const mod = 1e9 + 7;
190+
for (let i = 0; i < n; ++i) {
191+
ans = (ans + i * nums[i] - sum) % mod;
192+
sum += nums[i];
193+
}
194+
return ans;
195+
}
110196
```
111197

112198
### **...**

solution/2700-2799/2731.Movement of Robots/README_EN.md

+91-3
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,118 @@ The distance between the two robots is abs(-2 - 3) = 5.
6666

6767
## Solutions
6868

69+
**Solution 1: Quick thinking + Sorting**
70+
71+
After two robots collide, they will immediately change direction, which is equivalent to the two robots continuing to move in their original direction. Therefore, we traverse the array $nums$, and according to the instructions in the string $s$, we add or subtract $d$ from the position of each robot, and then sort the array $nums$.
72+
73+
Next, we enumerate the position of each robot from small to large, and calculate the sum of the distances between the current robot and all robots in front, which is the answer.
74+
75+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$, where $n$ is the number of robots.
76+
6977
<!-- tabs:start -->
7078

7179
### **Python3**
7280

7381
```python
74-
82+
class Solution:
83+
def sumDistance(self, nums: List[int], s: str, d: int) -> int:
84+
mod = 10**9 + 7
85+
for i, c in enumerate(s):
86+
nums[i] += d if c == "R" else -d
87+
nums.sort()
88+
ans = s = 0
89+
for i, x in enumerate(nums):
90+
ans += i * x - s
91+
s += x
92+
return ans % mod
7593
```
7694

7795
### **Java**
7896

7997
```java
80-
98+
class Solution {
99+
public int sumDistance(int[] nums, String s, int d) {
100+
int n = nums.length;
101+
long[] arr = new long[n];
102+
for (int i = 0; i < n; ++i) {
103+
arr[i] = (long) nums[i] + (s.charAt(i) == 'L' ? -d : d);
104+
}
105+
Arrays.sort(arr);
106+
long ans = 0, sum = 0;
107+
final int mod = (int) 1e9 + 7;
108+
for (int i = 0; i < n; ++i) {
109+
ans = (ans + i * arr[i] - sum) % mod;
110+
sum += arr[i];
111+
}
112+
return (int) ans;
113+
}
114+
}
81115
```
82116

83117
### **C++**
84118

85119
```cpp
86-
120+
class Solution {
121+
public:
122+
int sumDistance(vector<int>& nums, string s, int d) {
123+
int n = nums.size();
124+
vector<long long> arr(n);
125+
for (int i = 0; i < n; ++i) {
126+
arr[i] = 1LL * nums[i] + (s[i] == 'L' ? -d : d);
127+
}
128+
sort(arr.begin(), arr.end());
129+
long long ans = 0;
130+
long long sum = 0;
131+
const int mod = 1e9 + 7;
132+
for (int i = 0; i < n; ++i) {
133+
ans = (ans + i * arr[i] - sum) % mod;
134+
sum += arr[i];
135+
}
136+
return ans;
137+
}
138+
};
87139
```
88140
89141
### **Go**
90142
91143
```go
144+
func sumDistance(nums []int, s string, d int) (ans int) {
145+
for i, c := range s {
146+
if c == 'R' {
147+
nums[i] += d
148+
} else {
149+
nums[i] -= d
150+
}
151+
}
152+
sort.Ints(nums)
153+
sum := 0
154+
const mod int = 1e9 + 7
155+
for i, x := range nums {
156+
ans = (ans + i*x - sum) % mod
157+
sum += x
158+
}
159+
return
160+
}
161+
```
92162

163+
### **TypeScript**
164+
165+
```ts
166+
function sumDistance(nums: number[], s: string, d: number): number {
167+
const n = nums.length;
168+
for (let i = 0; i < n; ++i) {
169+
nums[i] += s[i] === 'L' ? -d : d;
170+
}
171+
nums.sort((a, b) => a - b);
172+
let ans = 0;
173+
let sum = 0;
174+
const mod = 1e9 + 7;
175+
for (let i = 0; i < n; ++i) {
176+
ans = (ans + i * nums[i] - sum) % mod;
177+
sum += nums[i];
178+
}
179+
return ans;
180+
}
93181
```
94182

95183
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int sumDistance(vector<int>& nums, string s, int d) {
4+
int n = nums.size();
5+
vector<long long> arr(n);
6+
for (int i = 0; i < n; ++i) {
7+
arr[i] = 1LL * nums[i] + (s[i] == 'L' ? -d : d);
8+
}
9+
sort(arr.begin(), arr.end());
10+
long long ans = 0;
11+
long long sum = 0;
12+
const int mod = 1e9 + 7;
13+
for (int i = 0; i < n; ++i) {
14+
ans = (ans + i * arr[i] - sum) % mod;
15+
sum += arr[i];
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func sumDistance(nums []int, s string, d int) (ans int) {
2+
for i, c := range s {
3+
if c == 'R' {
4+
nums[i] += d
5+
} else {
6+
nums[i] -= d
7+
}
8+
}
9+
sort.Ints(nums)
10+
sum := 0
11+
const mod int = 1e9 + 7
12+
for i, x := range nums {
13+
ans = (ans + i*x - sum) % mod
14+
sum += x
15+
}
16+
return
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int sumDistance(int[] nums, String s, int d) {
3+
int n = nums.length;
4+
long[] arr = new long[n];
5+
for (int i = 0; i < n; ++i) {
6+
arr[i] = (long) nums[i] + (s.charAt(i) == 'L' ? -d : d);
7+
}
8+
Arrays.sort(arr);
9+
long ans = 0, sum = 0;
10+
final int mod = (int) 1e9 + 7;
11+
for (int i = 0; i < n; ++i) {
12+
ans = (ans + i * arr[i] - sum) % mod;
13+
sum += arr[i];
14+
}
15+
return (int) ans;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def sumDistance(self, nums: List[int], s: str, d: int) -> int:
3+
mod = 10**9 + 7
4+
for i, c in enumerate(s):
5+
nums[i] += d if c == "R" else -d
6+
nums.sort()
7+
ans = s = 0
8+
for i, x in enumerate(nums):
9+
ans += i * x - s
10+
s += x
11+
return ans % mod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function sumDistance(nums: number[], s: string, d: number): number {
2+
const n = nums.length;
3+
for (let i = 0; i < n; ++i) {
4+
nums[i] += s[i] === 'L' ? -d : d;
5+
}
6+
nums.sort((a, b) => a - b);
7+
let ans = 0;
8+
let sum = 0;
9+
const mod = 1e9 + 7;
10+
for (let i = 0; i < n; ++i) {
11+
ans = (ans + i * nums[i] - sum) % mod;
12+
sum += nums[i];
13+
}
14+
return ans;
15+
}

solution/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
2606,
7878
2643,
7979
2682,
80+
2731,
8081
2788,
8182
2790,
8283
2800,

0 commit comments

Comments
 (0)