Skip to content

Commit 2b64054

Browse files
authored
Merge pull request #1386 from 0xff-dev/2211
Add solution and test-cases for problem 2211
2 parents 04950df + 38a0bf2 commit 2b64054

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

leetcode/2201-2300/2211.Count-Collisions-on-a-Road/README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [2211.Count Collisions on a Road][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There are `n` cars on an infinitely long road. The cars are numbered from `0` to `n - 1` from left to right and each car is present at a **unique** point.
5+
6+
You are given a **0-indexed** string `directions` of length `n`. `directions[i]` can be either `'L'`, `'R'`, or `'S'` denoting whether the `ith` car is moving towards the **left**, towards the **right**, or **staying** at its current point respectively. Each moving car has the **same speed**.
7+
8+
The number of collisions can be calculated as follows:
9+
10+
- When two cars moving in **opposite** directions collide with each other, the number of collisions increases by `2`.
11+
- When a moving car collides with a stationary car, the number of collisions increases by `1`.
12+
13+
After a collision, the cars involved can no longer move and will stay at the point where they collided. Other than that, cars cannot change their state or direction of motion.
14+
15+
Return the **total number of collisions** that will happen on the road.
716

817
**Example 1:**
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
20+
Input: directions = "RLRSLL"
21+
Output: 5
22+
Explanation:
23+
The collisions that will happen on the road are:
24+
- Cars 0 and 1 will collide with each other. Since they are moving in opposite directions, the number of collisions becomes 0 + 2 = 2.
25+
- Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3.
26+
- Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4.
27+
- Cars 4 and 5 will collide with each other. After car 4 collides with car 3, it will stay at the point of collision and get hit by car 5. The number of collisions becomes 4 + 1 = 5.
28+
Thus, the total number of collisions that will happen on the road is 5.
1329
```
1430

15-
## 题意
16-
> ...
17-
18-
## 题解
31+
**Example 2:**
1932

20-
### 思路1
21-
> ...
22-
Count Collisions on a Road
23-
```go
2433
```
25-
34+
Input: directions = "LLRR"
35+
Output: 0
36+
Explanation:
37+
No cars will collide with each other. Thus, the total number of collisions that will happen on the road is 0.
38+
```
2639

2740
## 结语
2841

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(directions string) int {
4+
var ret int
5+
l := len(directions)
6+
points := [][2]int{}
7+
for i := 0; i < l-1; i++ {
8+
if directions[i] == 'R' && directions[i+1] == 'L' {
9+
points = append(points, [2]int{i, i + 1})
10+
continue
11+
}
12+
if directions[i] == 'S' {
13+
points = append(points, [2]int{i, l})
14+
}
15+
}
16+
if directions[l-1] == 'S' {
17+
points = append(points, [2]int{l - 1, l})
18+
}
19+
20+
for _, p := range points {
21+
left := p[0] - 1
22+
right := p[0] + 1
23+
if p[1] != l {
24+
ret += 2
25+
right = p[1] + 1
26+
}
27+
for ; left >= 0 && directions[left] == 'R'; left-- {
28+
ret++
29+
}
30+
for ; right < l && directions[right] == 'L'; right++ {
31+
ret++
32+
}
33+
}
34+
return ret
535
}

leetcode/2201-2300/2211.Count-Collisions-on-a-Road/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "RLRSLL", 5},
17+
{"TestCase2", "LLRR", 0},
18+
{"TestCase3", "SRRLRLRSRLRSSRRLSLRLLRSLSLLSSRRLSRSLSLRRS", 28},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)