Skip to content

Commit f5e30de

Browse files
authored
Merge pull request #932 from 0xff-dev/2751
Add solution and test-cases for problem 2751
2 parents ca755e9 + 9582929 commit f5e30de

File tree

6 files changed

+134
-12
lines changed

6 files changed

+134
-12
lines changed
13.9 KB
Loading
13.5 KB
Loading
9.63 KB
Loading
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# [2751. Robot Collisions][title]
2+
3+
## Description
4+
There are `n` **1-indexed** robots, each having a position on a line, health, and movement direction.
5+
6+
You are given **0-indexed** integer arrays `positions`, `healths`, and a string `directions` (`directions[i]` is either **'L'** for **left** or **'R'** for **right**). All integers in `positions` are **unique**.
7+
8+
All robots start moving on the line **simultaneously** at the **same speed** in their given directions. If two robots ever share the same position while moving, they will **collide**.
9+
10+
If two robots collide, the robot with **lower health** is **removed** from the line, and the health of the other robot **decreases by one**. The surviving robot continues in the **same** direction it was going. If both robots have the **same** health, they are both removed from the line.
11+
12+
Your task is to determine the **health** of the robots that survive the collisions, in the same **order** that the robots were given, i.e. final heath of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.
13+
14+
Return an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.
15+
16+
**Note**: The positions may be unsorted.
17+
18+
**Example 1:**
19+
20+
![1](./1.png)
21+
22+
```
23+
Input: positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRRR"
24+
Output: [2,17,9,15,10]
25+
Explanation: No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10].
26+
```
27+
28+
**Example 2:**
29+
30+
![2](./2.png)
31+
32+
```
33+
Input: positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"
34+
Output: [14]
35+
Explanation: There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4's health is smaller, it gets removed, and robot 3's health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14].
36+
```
37+
38+
**Example 3:**
39+
40+
![3](./3.png)
41+
42+
```
43+
Input: positions = [1,2,5,6], healths = [10,10,11,11], directions = "RLRL"
44+
Output: []
45+
Explanation: Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].
46+
```
47+
48+
## 结语
49+
50+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
51+
52+
[title]: https://leetcode.com/problems/robot-collisions
53+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,73 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
type tmp2751 struct {
6+
i []int
7+
p, h []int
8+
d []byte
9+
}
10+
11+
func (t tmp2751) Len() int {
12+
return len(t.p)
13+
}
14+
15+
func (t tmp2751) Swap(i, j int) {
16+
t.p[i], t.p[j] = t.p[j], t.p[i]
17+
t.h[i], t.h[j] = t.h[j], t.h[i]
18+
t.d[i], t.d[j] = t.d[j], t.d[i]
19+
t.i[i], t.i[j] = t.i[j], t.i[i]
20+
}
21+
22+
func (t tmp2751) Less(i, j int) bool {
23+
return t.p[i] < t.p[j]
24+
}
25+
26+
func Solution(positions []int, healths []int, directions string) []int {
27+
ans := make([]int, 0)
28+
l := len(positions)
29+
indies := make([]int, l)
30+
for i := 0; i < l; i++ {
31+
indies[i] = i
32+
}
33+
s := tmp2751{p: positions, h: healths, d: []byte(directions), i: indies}
34+
sort.Sort(s)
35+
36+
stack := make([]int, len(positions))
37+
stackIndex := -1
38+
for i := 0; i < l; i++ {
39+
dir := s.d[i]
40+
if dir == 'L' {
41+
for stackIndex >= 0 && healths[stack[stackIndex]] < healths[i] {
42+
healths[i]--
43+
stackIndex--
44+
}
45+
46+
if stackIndex == -1 {
47+
ans = append(ans, i)
48+
continue
49+
}
50+
if healths[stack[stackIndex]] == healths[i] {
51+
stackIndex--
52+
continue
53+
}
54+
55+
healths[stack[stackIndex]]--
56+
57+
continue
58+
}
59+
stackIndex++
60+
stack[stackIndex] = i
61+
}
62+
for i := 0; i <= stackIndex; i++ {
63+
ans = append(ans, stack[i])
64+
}
65+
sort.Slice(ans, func(i, j int) bool {
66+
return indies[ans[i]] < indies[ans[j]]
67+
})
68+
for i := 0; i < len(ans); i++ {
69+
ans[i] = healths[ans[i]]
70+
}
71+
72+
return ans
573
}

leetcode/2701-2800/2751.Robot-Collisions/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
p, h []int
14+
d string
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{5, 4, 3, 2, 1}, []int{2, 17, 9, 15, 10}, "RRRRR", []int{2, 17, 9, 15, 10}},
18+
{"TestCase2", []int{3, 5, 2, 6}, []int{10, 10, 15, 12}, "RLRL", []int{14}},
19+
{"TestCase3", []int{1, 2, 5, 6}, []int{10, 10, 11, 11}, "RLRL", []int{}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.p, c.h, c.d)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.p, c.h, c.d)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)