Skip to content

Commit 758e622

Browse files
authored
feat: add solutions to lc problem: No.2211 (#4089)
No.2211.Count Collisions on a Road
1 parent a6c9fbe commit 758e622

File tree

8 files changed

+160
-96
lines changed

8 files changed

+160
-96
lines changed

solution/2200-2299/2211.Count Collisions on a Road/README.md

+56-31
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ tags:
7474

7575
<!-- solution:start -->
7676

77-
### 方法一
77+
### 方法一:脑筋急转弯
78+
79+
根据题意,当两辆移动方向相反的车相撞时,碰撞次数加 $2$,即两辆车被撞停,答案加 $2$;当一辆移动的车和一辆静止的车相撞时,碰撞次数加 $1$,即一辆车被撞停,答案加 $1$。
80+
81+
而显然前缀的 $\textit{L}$ 和后缀的 $\textit{R}$ 是不会发生碰撞的,所以我们只需要统计中间不等于 $\textit{S}$ 的字符个数即可。
82+
83+
时间复杂度 $O(n)$,空间复杂度 $O(n)$ 或 $O(1)$。其中 $n$ 是字符串 $\textit{directions}$ 的长度。
7884

7985
<!-- tabs:start -->
8086

@@ -83,30 +89,27 @@ tags:
8389
```python
8490
class Solution:
8591
def countCollisions(self, directions: str) -> int:
86-
d = directions.lstrip('L').rstrip('R')
87-
return len(d) - d.count('S')
92+
s = directions.lstrip("L").rstrip("R")
93+
return len(s) - s.count("S")
8894
```
8995

9096
#### Java
9197

9298
```java
9399
class Solution {
94100
public int countCollisions(String directions) {
95-
char[] ds = directions.toCharArray();
96-
int n = ds.length;
97-
int l = 0;
98-
int r = n - 1;
99-
while (l < n && ds[l] == 'L') {
101+
char[] s = directions.toCharArray();
102+
int n = s.length;
103+
int l = 0, r = n - 1;
104+
while (l < n && s[l] == 'L') {
100105
++l;
101106
}
102-
while (r >= 0 && ds[r] == 'R') {
107+
while (r >= 0 && s[r] == 'R') {
103108
--r;
104109
}
105-
int ans = 0;
110+
int ans = r - l + 1;
106111
for (int i = l; i <= r; ++i) {
107-
if (ds[i] != 'S') {
108-
++ans;
109-
}
112+
ans -= s[i] == 'S' ? 1 : 0;
110113
}
111114
return ans;
112115
}
@@ -118,18 +121,16 @@ class Solution {
118121
```cpp
119122
class Solution {
120123
public:
121-
int countCollisions(string directions) {
122-
int l = 0, r = directions.size() - 1, count = 0;
123-
while (l <= r && directions[l] == 'L') {
124-
l++;
125-
}
126-
while (l <= r && directions[r] == 'R') {
127-
r--;
124+
int countCollisions(string s) {
125+
int n = s.size();
126+
int l = 0, r = n - 1;
127+
while (l < n && s[l] == 'L') {
128+
++l;
128129
}
129-
for (int i = l; i <= r; i++) {
130-
count += directions[i] != 'S';
130+
while (r >= 0 && s[r] == 'R') {
131+
--r;
131132
}
132-
return count;
133+
return r - l + 1 - count(s.begin() + l, s.begin() + r + 1, 'S');
133134
}
134135
};
135136
```
@@ -138,9 +139,8 @@ public:
138139
139140
```go
140141
func countCollisions(directions string) int {
141-
d := strings.TrimLeft(directions, "L")
142-
d = strings.TrimRight(d, "R")
143-
return len(d) - strings.Count(d, "S")
142+
s := strings.TrimRight(strings.TrimLeft(directions, "L"), "R")
143+
return len(s) - strings.Count(s, "S")
144144
}
145145
```
146146

@@ -149,24 +149,49 @@ func countCollisions(directions string) int {
149149
```ts
150150
function countCollisions(directions: string): number {
151151
const n = directions.length;
152-
let l = 0,
153-
r = n - 1;
152+
let [l, r] = [0, n - 1];
154153
while (l < n && directions[l] == 'L') {
155154
++l;
156155
}
157156
while (r >= 0 && directions[r] == 'R') {
158157
--r;
159158
}
160-
let ans = 0;
159+
let ans = r - l + 1;
161160
for (let i = l; i <= r; ++i) {
162-
if (directions[i] != 'S') {
163-
++ans;
161+
if (directions[i] === 'S') {
162+
--ans;
164163
}
165164
}
166165
return ans;
167166
}
168167
```
169168

169+
#### JavaScript
170+
171+
```js
172+
/**
173+
* @param {string} directions
174+
* @return {number}
175+
*/
176+
var countCollisions = function (directions) {
177+
const n = directions.length;
178+
let [l, r] = [0, n - 1];
179+
while (l < n && directions[l] == 'L') {
180+
++l;
181+
}
182+
while (r >= 0 && directions[r] == 'R') {
183+
--r;
184+
}
185+
let ans = r - l + 1;
186+
for (let i = l; i <= r; ++i) {
187+
if (directions[i] === 'S') {
188+
--ans;
189+
}
190+
}
191+
return ans;
192+
};
193+
```
194+
170195
<!-- tabs:end -->
171196

172197
<!-- solution:end -->

solution/2200-2299/2211.Count Collisions on a Road/README_EN.md

+57-32
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The collisions that will happen on the road are:
4747
- Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3.
4848
- Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4.
4949
- 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.
5151
</pre>
5252

5353
<p><strong class="example">Example 2:</strong></p>
@@ -72,7 +72,13 @@ No cars will collide with each other. Thus, the total number of collisions that
7272

7373
<!-- solution:start -->
7474

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}$.
7682

7783
<!-- tabs:start -->
7884

@@ -81,30 +87,27 @@ No cars will collide with each other. Thus, the total number of collisions that
8187
```python
8288
class Solution:
8389
def countCollisions(self, directions: str) -> int:
84-
d = directions.lstrip('L').rstrip('R')
85-
return len(d) - d.count('S')
90+
s = directions.lstrip("L").rstrip("R")
91+
return len(s) - s.count("S")
8692
```
8793

8894
#### Java
8995

9096
```java
9197
class Solution {
9298
public int countCollisions(String directions) {
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') {
98103
++l;
99104
}
100-
while (r >= 0 && ds[r] == 'R') {
105+
while (r >= 0 && s[r] == 'R') {
101106
--r;
102107
}
103-
int ans = 0;
108+
int ans = r - l + 1;
104109
for (int i = l; i <= r; ++i) {
105-
if (ds[i] != 'S') {
106-
++ans;
107-
}
110+
ans -= s[i] == 'S' ? 1 : 0;
108111
}
109112
return ans;
110113
}
@@ -116,18 +119,16 @@ class Solution {
116119
```cpp
117120
class Solution {
118121
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;
126127
}
127-
for (int i = l; i <= r; i++) {
128-
count += directions[i] != 'S';
128+
while (r >= 0 && s[r] == 'R') {
129+
--r;
129130
}
130-
return count;
131+
return r - l + 1 - count(s.begin() + l, s.begin() + r + 1, 'S');
131132
}
132133
};
133134
```
@@ -136,9 +137,8 @@ public:
136137
137138
```go
138139
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")
142142
}
143143
```
144144

@@ -147,24 +147,49 @@ func countCollisions(directions string) int {
147147
```ts
148148
function countCollisions(directions: string): number {
149149
const n = directions.length;
150-
let l = 0,
151-
r = n - 1;
150+
let [l, r] = [0, n - 1];
152151
while (l < n && directions[l] == 'L') {
153152
++l;
154153
}
155154
while (r >= 0 && directions[r] == 'R') {
156155
--r;
157156
}
158-
let ans = 0;
157+
let ans = r - l + 1;
159158
for (let i = l; i <= r; ++i) {
160-
if (directions[i] != 'S') {
161-
++ans;
159+
if (directions[i] === 'S') {
160+
--ans;
162161
}
163162
}
164163
return ans;
165164
}
166165
```
167166

167+
#### JavaScript
168+
169+
```js
170+
/**
171+
* @param {string} directions
172+
* @return {number}
173+
*/
174+
var countCollisions = function (directions) {
175+
const n = directions.length;
176+
let [l, r] = [0, n - 1];
177+
while (l < n && directions[l] == 'L') {
178+
++l;
179+
}
180+
while (r >= 0 && directions[r] == 'R') {
181+
--r;
182+
}
183+
let ans = r - l + 1;
184+
for (let i = l; i <= r; ++i) {
185+
if (directions[i] === 'S') {
186+
--ans;
187+
}
188+
}
189+
return ans;
190+
};
191+
```
192+
168193
<!-- tabs:end -->
169194

170195
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
class Solution {
22
public:
3-
int countCollisions(string directions) {
4-
int l = 0, r = directions.size() - 1, count = 0;
5-
while (l <= r && directions[l] == 'L') {
6-
l++;
3+
int countCollisions(string s) {
4+
int n = s.size();
5+
int l = 0, r = n - 1;
6+
while (l < n && s[l] == 'L') {
7+
++l;
78
}
8-
while (l <= r && directions[r] == 'R') {
9-
r--;
9+
while (r >= 0 && s[r] == 'R') {
10+
--r;
1011
}
11-
for (int i = l; i <= r; i++) {
12-
count += directions[i] != 'S';
13-
}
14-
return count;
12+
return r - l + 1 - count(s.begin() + l, s.begin() + r + 1, 'S');
1513
}
16-
};
14+
};
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
func countCollisions(directions string) int {
2-
d := strings.TrimLeft(directions, "L")
3-
d = strings.TrimRight(d, "R")
4-
return len(d) - strings.Count(d, "S")
5-
}
2+
s := strings.TrimRight(strings.TrimLeft(directions, "L"), "R")
3+
return len(s) - strings.Count(s, "S")
4+
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
class Solution {
22
public int countCollisions(String directions) {
3-
char[] ds = directions.toCharArray();
4-
int n = ds.length;
5-
int l = 0;
6-
int r = n - 1;
7-
while (l < n && ds[l] == 'L') {
3+
char[] s = directions.toCharArray();
4+
int n = s.length;
5+
int l = 0, r = n - 1;
6+
while (l < n && s[l] == 'L') {
87
++l;
98
}
10-
while (r >= 0 && ds[r] == 'R') {
9+
while (r >= 0 && s[r] == 'R') {
1110
--r;
1211
}
13-
int ans = 0;
12+
int ans = r - l + 1;
1413
for (int i = l; i <= r; ++i) {
15-
if (ds[i] != 'S') {
16-
++ans;
17-
}
14+
ans -= s[i] == 'S' ? 1 : 0;
1815
}
1916
return ans;
2017
}
21-
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} directions
3+
* @return {number}
4+
*/
5+
var countCollisions = function (directions) {
6+
const n = directions.length;
7+
let [l, r] = [0, n - 1];
8+
while (l < n && directions[l] == 'L') {
9+
++l;
10+
}
11+
while (r >= 0 && directions[r] == 'R') {
12+
--r;
13+
}
14+
let ans = r - l + 1;
15+
for (let i = l; i <= r; ++i) {
16+
if (directions[i] === 'S') {
17+
--ans;
18+
}
19+
}
20+
return ans;
21+
};

0 commit comments

Comments
 (0)