From 38a0bf2b12299a11ee422134d1e410d53d38a544 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 4 Dec 2025 22:08:40 +0800 Subject: [PATCH] Add solution and test-cases for problem 2211 --- .../2211.Count-Collisions-on-a-Road/README.md | 41 ++++++++++++------- .../Solution.go | 34 ++++++++++++++- .../Solution_test.go | 14 +++---- 3 files changed, 66 insertions(+), 23 deletions(-) diff --git a/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/README.md b/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/README.md index 2095a11f0..44be00920 100755 --- a/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/README.md +++ b/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/README.md @@ -1,28 +1,41 @@ # [2211.Count Collisions on a Road][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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. + +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**. + +The number of collisions can be calculated as follows: + +- When two cars moving in **opposite** directions collide with each other, the number of collisions increases by `2`. +- When a moving car collides with a stationary car, the number of collisions increases by `1`. + +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. + +Return the **total number of collisions** that will happen on the road. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: directions = "RLRSLL" +Output: 5 +Explanation: +The collisions that will happen on the road are: +- Cars 0 and 1 will collide with each other. Since they are moving in opposite directions, the number of collisions becomes 0 + 2 = 2. +- Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3. +- Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4. +- 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. +Thus, the total number of collisions that will happen on the road is 5. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Count Collisions on a Road -```go ``` - +Input: directions = "LLRR" +Output: 0 +Explanation: +No cars will collide with each other. Thus, the total number of collisions that will happen on the road is 0. +``` ## 结语 diff --git a/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/Solution.go b/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/Solution.go index d115ccf5e..6ea23f9db 100644 --- a/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/Solution.go +++ b/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/Solution.go @@ -1,5 +1,35 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(directions string) int { + var ret int + l := len(directions) + points := [][2]int{} + for i := 0; i < l-1; i++ { + if directions[i] == 'R' && directions[i+1] == 'L' { + points = append(points, [2]int{i, i + 1}) + continue + } + if directions[i] == 'S' { + points = append(points, [2]int{i, l}) + } + } + if directions[l-1] == 'S' { + points = append(points, [2]int{l - 1, l}) + } + + for _, p := range points { + left := p[0] - 1 + right := p[0] + 1 + if p[1] != l { + ret += 2 + right = p[1] + 1 + } + for ; left >= 0 && directions[left] == 'R'; left-- { + ret++ + } + for ; right < l && directions[right] == 'L'; right++ { + ret++ + } + } + return ret } diff --git a/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/Solution_test.go b/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/Solution_test.go index 14ff50eb4..88a89e6dc 100644 --- a/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/Solution_test.go +++ b/leetcode/2201-2300/2211.Count-Collisions-on-a-Road/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "RLRSLL", 5}, + {"TestCase2", "LLRR", 0}, + {"TestCase3", "SRRLRLRSRLRSSRRLSLRLLRSLSLLSSRRLSRSLSLRRS", 28}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }