You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2200-2299/2211.Count Collisions on a Road/README_EN.md
+57-32
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,7 @@ The collisions that will happen on the road are:
47
47
- Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3.
48
48
- Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4.
49
49
- 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.
50
-
Thus, the total number of collisions that will happen on the road is 5.
50
+
Thus, the total number of collisions that will happen on the road is 5.
51
51
</pre>
52
52
53
53
<p><strongclass="example">Example 2:</strong></p>
@@ -72,7 +72,13 @@ No cars will collide with each other. Thus, the total number of collisions that
72
72
73
73
<!-- solution:start -->
74
74
75
-
### Solution 1
75
+
### Solution 1: Brain Teaser
76
+
77
+
According to the problem description, when two cars moving in opposite directions collide, the collision count increases by $2$, meaning both cars stop, and the answer increases by $2$. When a moving car collides with a stationary car, the collision count increases by $1$, meaning one car stops, and the answer increases by $1$.
78
+
79
+
Obviously, the prefix $\textit{L}$ and the suffix $\textit{R}$ will not collide, so we only need to count the number of characters in the middle that are not $\textit{S}$.
80
+
81
+
The time complexity is $O(n)$, and the space complexity is $O(n)$ or $O(1)$. Here, $n$ is the length of the string $\textit{directions}$.
76
82
77
83
<!-- tabs:start -->
78
84
@@ -81,30 +87,27 @@ No cars will collide with each other. Thus, the total number of collisions that
81
87
```python
82
88
classSolution:
83
89
defcountCollisions(self, directions: str) -> int:
84
-
d= directions.lstrip('L').rstrip('R')
85
-
returnlen(d) -d.count('S')
90
+
s= directions.lstrip("L").rstrip("R")
91
+
returnlen(s) -s.count("S")
86
92
```
87
93
88
94
#### Java
89
95
90
96
```java
91
97
classSolution {
92
98
publicintcountCollisions(Stringdirections) {
93
-
char[] ds = directions.toCharArray();
94
-
int n = ds.length;
95
-
int l =0;
96
-
int r = n -1;
97
-
while (l < n && ds[l] =='L') {
99
+
char[] s = directions.toCharArray();
100
+
int n = s.length;
101
+
int l =0, r = n -1;
102
+
while (l < n && s[l] =='L') {
98
103
++l;
99
104
}
100
-
while (r >=0&&ds[r] =='R') {
105
+
while (r >=0&&s[r] =='R') {
101
106
--r;
102
107
}
103
-
int ans =0;
108
+
int ans =r - l +1;
104
109
for (int i = l; i <= r; ++i) {
105
-
if (ds[i] !='S') {
106
-
++ans;
107
-
}
110
+
ans -= s[i] =='S'?1:0;
108
111
}
109
112
return ans;
110
113
}
@@ -116,18 +119,16 @@ class Solution {
116
119
```cpp
117
120
classSolution {
118
121
public:
119
-
int countCollisions(string directions) {
120
-
int l = 0, r = directions.size() - 1, count = 0;
121
-
while (l <= r && directions[l] == 'L') {
122
-
l++;
123
-
}
124
-
while (l <= r && directions[r] == 'R') {
125
-
r--;
122
+
int countCollisions(string s) {
123
+
int n = s.size();
124
+
int l = 0, r = n - 1;
125
+
while (l < n && s[l] == 'L') {
126
+
++l;
126
127
}
127
-
for (int i = l; i <= r; i++) {
128
-
count += directions[i] != 'S';
128
+
while (r >= 0 && s[r] == 'R') {
129
+
--r;
129
130
}
130
-
return count;
131
+
return r - l + 1 - count(s.begin() + l, s.begin() + r + 1, 'S');
131
132
}
132
133
};
133
134
```
@@ -136,9 +137,8 @@ public:
136
137
137
138
```go
138
139
func countCollisions(directions string) int {
139
-
d := strings.TrimLeft(directions, "L")
140
-
d = strings.TrimRight(d, "R")
141
-
return len(d) - strings.Count(d, "S")
140
+
s := strings.TrimRight(strings.TrimLeft(directions, "L"), "R")
141
+
return len(s) - strings.Count(s, "S")
142
142
}
143
143
```
144
144
@@ -147,24 +147,49 @@ func countCollisions(directions string) int {
147
147
```ts
148
148
function countCollisions(directions:string):number {
0 commit comments