Skip to content

Commit

Permalink
Merge pull request #659 from 0xff-dev/2731
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2731
  • Loading branch information
6boris authored Oct 28, 2023
2 parents 3d5eac6 + e900ba1 commit 3eaf233
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 12 deletions.
51 changes: 51 additions & 0 deletions leetcode/2701-2800/2731.Movement-of-Robots/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# [2731. Movement of Robots][title]

## Description
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:**

- For two robots at the index `i` and `j`, pair (`i,j`) and pair (`j,i`) are considered the same pair.
- When robots collide, they **instantly change** their directions without wasting any time.
- Collision happens when two robots share the same place in a moment.
- 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.
- 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.

**Example 1:**

```
Input: nums = [-2,0,2], s = "RLL", d = 3
Output: 8
Explanation:
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.
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.
After 3 seconds, the positions are [-3,-1,1].
The distance between the robot at index 0 and 1 is abs(-3 - (-1)) = 2.
The distance between the robot at index 0 and 2 is abs(-3 - 1) = 4.
The distance between the robot at index 1 and 2 is abs(-1 - 1) = 2.
The sum of the pairs of all distances = 2 + 4 + 2 = 8.
```

**Example 2:**

```
Input: nums = [1,0], s = "RL", d = 2
Output: 5
Explanation:
After 1 second, the positions are [2,-1].
After 2 seconds, the positions are [3,-2].
The distance between the two robots is abs(-2 - 3) = 5.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/movement-of-robots
[me]: https://github.com/kylesliu/awesome-golang-algorithm
61 changes: 59 additions & 2 deletions leetcode/2701-2800/2731.Movement-of-Robots/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
package Solution

func Solution(x bool) bool {
return x
import (
"sort"
)

const mod2731 = 1000000007

func Solution(nums []int, s string, d int) int {

length := len(nums)
for i := range nums {
if s[i] == 'R' {
nums[i] += d
continue
}
nums[i] -= d
}
sort.Ints(nums)

sum := make([][]int, length)
for i := len(nums) - 1; i >= 0; i-- {
sum[i] = make([]int, 4)
if i == len(nums)-1 {
if nums[i] < 0 {
sum[i][0] = nums[i]
sum[i][1] = 1
} else {
sum[i][2] = nums[i]
sum[i][3] = 1
}
continue
}
if nums[i] < 0 {
sum[i][0] = sum[i+1][0] + nums[i]
sum[i][1] = sum[i+1][1] + 1
sum[i][2] = sum[i+1][2]
sum[i][3] = sum[i+1][3]
} else {
sum[i][2] = sum[i+1][2] + nums[i]
sum[i][3] = sum[i+1][3] + 1
sum[i][0] = sum[i+1][0]
sum[i][1] = sum[i+1][1]
}
}
ans := 0
var a, b int
for i := 0; i < length-1; i++ {
a = nums[i]*sum[i+1][3] - sum[i+1][2]
b = nums[i]*sum[i+1][1] - sum[i+1][0]
if a < 0 {
a = -a
}
if b < 0 {
b = -b
}
a += b

ans = (ans + a) % mod2731
}
return ans
}
21 changes: 11 additions & 10 deletions leetcode/2701-2800/2731.Movement-of-Robots/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
nums []int
s string
d int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{-2, 0, 2}, "RLL", 3, 8},
{"TestCase2", []int{1, -67, 68, -26, -13, -40, -56, 62, 21}, "LLLRLLRRR", 4, 2106},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.s, c.d)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.nums, c.s, c.d)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 3eaf233

Please sign in to comment.