Skip to content

feat: add solutions to lc problem: No.2211 #4089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 56 additions & 31 deletions solution/2200-2299/2211.Count Collisions on a Road/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:脑筋急转弯

根据题意,当两辆移动方向相反的车相撞时,碰撞次数加 $2$,即两辆车被撞停,答案加 $2$;当一辆移动的车和一辆静止的车相撞时,碰撞次数加 $1$,即一辆车被撞停,答案加 $1$。

而显然前缀的 $\textit{L}$ 和后缀的 $\textit{R}$ 是不会发生碰撞的,所以我们只需要统计中间不等于 $\textit{S}$ 的字符个数即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$ 或 $O(1)$。其中 $n$ 是字符串 $\textit{directions}$ 的长度。

<!-- tabs:start -->

Expand All @@ -83,30 +89,27 @@ tags:
```python
class Solution:
def countCollisions(self, directions: str) -> int:
d = directions.lstrip('L').rstrip('R')
return len(d) - d.count('S')
s = directions.lstrip("L").rstrip("R")
return len(s) - s.count("S")
```

#### Java

```java
class Solution {
public int countCollisions(String directions) {
char[] ds = directions.toCharArray();
int n = ds.length;
int l = 0;
int r = n - 1;
while (l < n && ds[l] == 'L') {
char[] s = directions.toCharArray();
int n = s.length;
int l = 0, r = n - 1;
while (l < n && s[l] == 'L') {
++l;
}
while (r >= 0 && ds[r] == 'R') {
while (r >= 0 && s[r] == 'R') {
--r;
}
int ans = 0;
int ans = r - l + 1;
for (int i = l; i <= r; ++i) {
if (ds[i] != 'S') {
++ans;
}
ans -= s[i] == 'S' ? 1 : 0;
}
return ans;
}
Expand All @@ -118,18 +121,16 @@ class Solution {
```cpp
class Solution {
public:
int countCollisions(string directions) {
int l = 0, r = directions.size() - 1, count = 0;
while (l <= r && directions[l] == 'L') {
l++;
}
while (l <= r && directions[r] == 'R') {
r--;
int countCollisions(string s) {
int n = s.size();
int l = 0, r = n - 1;
while (l < n && s[l] == 'L') {
++l;
}
for (int i = l; i <= r; i++) {
count += directions[i] != 'S';
while (r >= 0 && s[r] == 'R') {
--r;
}
return count;
return r - l + 1 - count(s.begin() + l, s.begin() + r + 1, 'S');
}
};
```
Expand All @@ -138,9 +139,8 @@ public:

```go
func countCollisions(directions string) int {
d := strings.TrimLeft(directions, "L")
d = strings.TrimRight(d, "R")
return len(d) - strings.Count(d, "S")
s := strings.TrimRight(strings.TrimLeft(directions, "L"), "R")
return len(s) - strings.Count(s, "S")
}
```

Expand All @@ -149,24 +149,49 @@ func countCollisions(directions string) int {
```ts
function countCollisions(directions: string): number {
const n = directions.length;
let l = 0,
r = n - 1;
let [l, r] = [0, n - 1];
while (l < n && directions[l] == 'L') {
++l;
}
while (r >= 0 && directions[r] == 'R') {
--r;
}
let ans = 0;
let ans = r - l + 1;
for (let i = l; i <= r; ++i) {
if (directions[i] != 'S') {
++ans;
if (directions[i] === 'S') {
--ans;
}
}
return ans;
}
```

#### JavaScript

```js
/**
* @param {string} directions
* @return {number}
*/
var countCollisions = function (directions) {
const n = directions.length;
let [l, r] = [0, n - 1];
while (l < n && directions[l] == 'L') {
++l;
}
while (r >= 0 && directions[r] == 'R') {
--r;
}
let ans = r - l + 1;
for (let i = l; i <= r; ++i) {
if (directions[i] === 'S') {
--ans;
}
}
return ans;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
89 changes: 57 additions & 32 deletions solution/2200-2299/2211.Count Collisions on a Road/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The collisions that will happen on the road are:
- 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.
Thus, the total number of collisions that will happen on the road is 5.
</pre>

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

<!-- solution:start -->

### Solution 1
### Solution 1: Brain Teaser

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

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

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

<!-- tabs:start -->

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

#### Java

```java
class Solution {
public int countCollisions(String directions) {
char[] ds = directions.toCharArray();
int n = ds.length;
int l = 0;
int r = n - 1;
while (l < n && ds[l] == 'L') {
char[] s = directions.toCharArray();
int n = s.length;
int l = 0, r = n - 1;
while (l < n && s[l] == 'L') {
++l;
}
while (r >= 0 && ds[r] == 'R') {
while (r >= 0 && s[r] == 'R') {
--r;
}
int ans = 0;
int ans = r - l + 1;
for (int i = l; i <= r; ++i) {
if (ds[i] != 'S') {
++ans;
}
ans -= s[i] == 'S' ? 1 : 0;
}
return ans;
}
Expand All @@ -116,18 +119,16 @@ class Solution {
```cpp
class Solution {
public:
int countCollisions(string directions) {
int l = 0, r = directions.size() - 1, count = 0;
while (l <= r && directions[l] == 'L') {
l++;
}
while (l <= r && directions[r] == 'R') {
r--;
int countCollisions(string s) {
int n = s.size();
int l = 0, r = n - 1;
while (l < n && s[l] == 'L') {
++l;
}
for (int i = l; i <= r; i++) {
count += directions[i] != 'S';
while (r >= 0 && s[r] == 'R') {
--r;
}
return count;
return r - l + 1 - count(s.begin() + l, s.begin() + r + 1, 'S');
}
};
```
Expand All @@ -136,9 +137,8 @@ public:

```go
func countCollisions(directions string) int {
d := strings.TrimLeft(directions, "L")
d = strings.TrimRight(d, "R")
return len(d) - strings.Count(d, "S")
s := strings.TrimRight(strings.TrimLeft(directions, "L"), "R")
return len(s) - strings.Count(s, "S")
}
```

Expand All @@ -147,24 +147,49 @@ func countCollisions(directions string) int {
```ts
function countCollisions(directions: string): number {
const n = directions.length;
let l = 0,
r = n - 1;
let [l, r] = [0, n - 1];
while (l < n && directions[l] == 'L') {
++l;
}
while (r >= 0 && directions[r] == 'R') {
--r;
}
let ans = 0;
let ans = r - l + 1;
for (let i = l; i <= r; ++i) {
if (directions[i] != 'S') {
++ans;
if (directions[i] === 'S') {
--ans;
}
}
return ans;
}
```

#### JavaScript

```js
/**
* @param {string} directions
* @return {number}
*/
var countCollisions = function (directions) {
const n = directions.length;
let [l, r] = [0, n - 1];
while (l < n && directions[l] == 'L') {
++l;
}
while (r >= 0 && directions[r] == 'R') {
--r;
}
let ans = r - l + 1;
for (let i = l; i <= r; ++i) {
if (directions[i] === 'S') {
--ans;
}
}
return ans;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
20 changes: 9 additions & 11 deletions solution/2200-2299/2211.Count Collisions on a Road/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
class Solution {
public:
int countCollisions(string directions) {
int l = 0, r = directions.size() - 1, count = 0;
while (l <= r && directions[l] == 'L') {
l++;
int countCollisions(string s) {
int n = s.size();
int l = 0, r = n - 1;
while (l < n && s[l] == 'L') {
++l;
}
while (l <= r && directions[r] == 'R') {
r--;
while (r >= 0 && s[r] == 'R') {
--r;
}
for (int i = l; i <= r; i++) {
count += directions[i] != 'S';
}
return count;
return r - l + 1 - count(s.begin() + l, s.begin() + r + 1, 'S');
}
};
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
func countCollisions(directions string) int {
d := strings.TrimLeft(directions, "L")
d = strings.TrimRight(d, "R")
return len(d) - strings.Count(d, "S")
}
s := strings.TrimRight(strings.TrimLeft(directions, "L"), "R")
return len(s) - strings.Count(s, "S")
}
19 changes: 8 additions & 11 deletions solution/2200-2299/2211.Count Collisions on a Road/Solution.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
class Solution {
public int countCollisions(String directions) {
char[] ds = directions.toCharArray();
int n = ds.length;
int l = 0;
int r = n - 1;
while (l < n && ds[l] == 'L') {
char[] s = directions.toCharArray();
int n = s.length;
int l = 0, r = n - 1;
while (l < n && s[l] == 'L') {
++l;
}
while (r >= 0 && ds[r] == 'R') {
while (r >= 0 && s[r] == 'R') {
--r;
}
int ans = 0;
int ans = r - l + 1;
for (int i = l; i <= r; ++i) {
if (ds[i] != 'S') {
++ans;
}
ans -= s[i] == 'S' ? 1 : 0;
}
return ans;
}
}
}
21 changes: 21 additions & 0 deletions solution/2200-2299/2211.Count Collisions on a Road/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {string} directions
* @return {number}
*/
var countCollisions = function (directions) {
const n = directions.length;
let [l, r] = [0, n - 1];
while (l < n && directions[l] == 'L') {
++l;
}
while (r >= 0 && directions[r] == 'R') {
--r;
}
let ans = r - l + 1;
for (let i = l; i <= r; ++i) {
if (directions[i] === 'S') {
--ans;
}
}
return ans;
};
Loading
Loading