You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2700-2799/2731.Movement of Robots/README_EN.md
+91-3
Original file line number
Diff line number
Diff line change
@@ -66,30 +66,118 @@ The distance between the two robots is abs(-2 - 3) = 5.
66
66
67
67
## Solutions
68
68
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.
0 commit comments