2731. Movement of Robots Medium
1/**
2 * [2731] Movement of Robots
3 *
4 * 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.
5 * 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.
6 * If two robots collide, they will start moving in opposite directions.
7 * 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.
8 * Note:
9 *
10 * For two robots at the index i and j, pair (i,j) and pair (j,i) are considered the same pair.
11 * When robots collide, they instantly change their directions without wasting any time.
12 * Collision happens when two robots share the same place in a moment.
13 *
14 * For example, if a robot is positioned in 0 going to the right and another is positioned in 2 going to the left, the next second they'll be both in 1 and they will change direction and the next second the first one will be in 0, heading left, and another will be in 2, heading right.
15 * For example, if a robot is positioned in 0 going to the right and another is positioned in 1 going to the left, the next second the first one will be in 0, heading left, and another will be in 1, heading right.
16 *
17 *
18 *
19 *
20 * <strong class="example">Example 1:
21 *
22 * Input: nums = [-2,0,2], s = "RLL", d = 3
23 * Output: 8
24 * Explanation:
25 * After 1 second, the positions are [-1,-1,1]. Now, the robot at index 0 will move left, and the robot at index 1 will move right.
26 * After 2 seconds, the positions are [-2,0,0]. Now, the robot at index 1 will move left, and the robot at index 2 will move right.
27 * After 3 seconds, the positions are [-3,-1,1].
28 * The distance between the robot at index 0 and 1 is abs(-3 - (-1)) = 2.
29 * The distance between the robot at index 0 and 2 is abs(-3 - 1) = 4.
30 * The distance between the robot at index 1 and 2 is abs(-1 - 1) = 2.
31 * The sum of the pairs of all distances = 2 + 4 + 2 = 8.
32 *
33 * <strong class="example">Example 2:
34 *
35 * Input: nums = [1,0], s = "RL", d = 2
36 * Output: 5
37 * Explanation:
38 * After 1 second, the positions are [2,-1].
39 * After 2 seconds, the positions are [3,-2].
40 * The distance between the two robots is abs(-2 - 3) = 5.
41 *
42 *
43 * Constraints:
44 *
45 * 2 <= nums.length <= 10^5
46 * -2 * 10^9 <= nums[i] <= 2 * 10^9
47 * 0 <= d <= 10^9
48 * nums.length == s.length
49 * s consists of 'L' and 'R' only
50 * nums[i] will be unique.
51 *
52 */
53pub struct Solution {}
54
55// problem: https://leetcode.com/problems/movement-of-robots/
56// discuss: https://leetcode.com/problems/movement-of-robots/discuss/?currentPage=1&orderBy=most_votes&query=
57
58// submission codes start here
59
60impl Solution {
61 pub fn sum_distance(nums: Vec<i32>, s: String, d: i32) -> i32 {
62 0
63 }
64}
65
66// submission codes end
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_2731() {
74 }
75}
76
Back
© 2025 bowen.ge All Rights Reserved.