Skip to content

Commit 8703f0f

Browse files
chore: add LeetCode daily solution
1 parent 5cbbed6 commit 8703f0f

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Count Collisions on a Road (Medium)
2+
3+
**Problem ID:** 2211
4+
**Date:** 2025-12-04
5+
**Link:** https://leetcode.com/problems/count-collisions-on-a-road/
6+
7+
## Approach
8+
9+
To solve the problem of counting collisions on a road with cars moving in different directions, we can use a straightforward linear traversal approach. The main idea is to iterate through the string of directions and keep track of the collisions based on the rules provided.
10+
11+
### Approach:
12+
13+
1. **Initialization**: Start with a collision counter set to zero.
14+
15+
2. **Iterate through the string**: Loop through each character in the `directions` string:
16+
- If you encounter a 'R' (car moving right), check the next character:
17+
- If it's 'L' (car moving left), a collision occurs, contributing 2 to the collision count. Both cars will stop moving after this collision.
18+
- If it's 'S' (stationary car), a collision occurs, contributing 1 to the collision count.
19+
- If you encounter an 'L', simply continue as it won't cause any additional collisions unless followed by 'R'.
20+
- If you encounter 'S', check the previous character:
21+
- If it's 'R', it has already been counted in the previous step.
22+
- If it's 'L', it will also not contribute to any new collisions.
23+
24+
3. **Handle stationary cars**: When a stationary car ('S') is encountered, it can only collide with cars moving towards it (i.e., 'R' before it or 'L' after it). Thus, ensure that collisions are counted correctly based on the previous and next characters.
25+
26+
### Data Structures:
27+
- A simple integer counter to keep track of the total number of collisions is sufficient. No additional data structures are required since we are processing the string in a single pass.
28+
29+
### Complexity:
30+
- **Time Complexity**: O(n), where n is the length of the `directions` string, as we make a single pass through the string.
31+
- **Space Complexity**: O(1), since we are using a constant amount of space for the collision counter.
32+
33+
This approach is efficient and directly addresses the problem requirements by leveraging the properties of the string and the rules of collision counting.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public int countCollisions(String directions) {
3+
int collisions = 0;
4+
int n = directions.length();
5+
6+
// Convert the string to a character array for easier manipulation
7+
char[] cars = directions.toCharArray();
8+
9+
// Process the cars from left to right
10+
for (int i = 0; i < n; i++) {
11+
if (cars[i] == 'R') {
12+
// Check for collisions with 'L' or 'S'
13+
while (i + 1 < n && cars[i + 1] == 'L') {
14+
collisions += 2; // R and L collide
15+
i++;
16+
}
17+
if (i + 1 < n && cars[i + 1] == 'S') {
18+
collisions += 1; // R and S collide
19+
}
20+
} else if (cars[i] == 'L') {
21+
// Check for collisions with 'S'
22+
if (i - 1 >= 0 && cars[i - 1] == 'S') {
23+
collisions += 1; // L and S collide
24+
}
25+
} else if (cars[i] == 'S') {
26+
// Check for collisions with 'R' or 'L'
27+
if (i - 1 >= 0 && cars[i - 1] == 'R') {
28+
collisions += 1; // R and S collide
29+
}
30+
if (i + 1 < n && cars[i + 1] == 'L') {
31+
collisions += 1; // S and L collide
32+
}
33+
}
34+
}
35+
36+
return collisions;
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var countCollisions = function(directions) {
2+
let collisions = 0;
3+
let n = directions.length;
4+
let i = 0;
5+
6+
while (i < n) {
7+
if (directions[i] === 'R') {
8+
let j = i + 1;
9+
while (j < n && directions[j] === 'S') {
10+
collisions++;
11+
j++;
12+
}
13+
if (j < n && directions[j] === 'L') {
14+
collisions += 2; // R and L collide
15+
i = j + 1; // Move past the collision
16+
} else {
17+
i = j; // Move to the next car
18+
}
19+
} else if (directions[i] === 'L') {
20+
// No need to check further as L cars won't collide with anything to the left
21+
i++;
22+
} else {
23+
// If it's 'S', just count it and move on
24+
i++;
25+
}
26+
}
27+
28+
return collisions;
29+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def countCollisions(self, directions: str) -> int:
3+
collisions = 0
4+
n = len(directions)
5+
6+
# Process from left to right
7+
for i in range(n):
8+
if directions[i] == 'R':
9+
if i + 1 < n and directions[i + 1] == 'L':
10+
collisions += 2
11+
directions = directions[:i] + 'S' + directions[i + 1:i + 1] + 'S' + directions[i + 2:]
12+
elif i + 1 < n and directions[i + 1] == 'S':
13+
collisions += 1
14+
directions = directions[:i] + 'S' + directions[i + 1:]
15+
16+
# Process from right to left
17+
for i in range(n - 1, -1, -1):
18+
if directions[i] == 'L':
19+
if i - 1 >= 0 and directions[i - 1] == 'R':
20+
collisions += 2
21+
directions = directions[:i] + 'S' + directions[i + 1:]
22+
elif i - 1 >= 0 and directions[i - 1] == 'S':
23+
collisions += 1
24+
directions = directions[:i] + 'S' + directions[i + 1:]
25+
26+
return collisions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
294294
- 2025-12-01 — [Maximum Running Time of N Computers](https://leetcode.com/problems/maximum-running-time-of-n-computers/) (Hard) → `Hard/2025-12-01-2141-Maximum-Running-Time-of-N-Computers`
295295
- 2025-12-02 — [Count Number of Trapezoids I](https://leetcode.com/problems/count-number-of-trapezoids-i/) (Medium) → `Medium/2025-12-02-3623-Count-Number-of-Trapezoids-I`
296296
- 2025-12-03 — [Count Number of Trapezoids II](https://leetcode.com/problems/count-number-of-trapezoids-ii/) (Hard) → `Hard/2025-12-03-3625-Count-Number-of-Trapezoids-II`
297+
- 2025-12-04 — [Count Collisions on a Road](https://leetcode.com/problems/count-collisions-on-a-road/) (Medium) → `Medium/2025-12-04-2211-Count-Collisions-on-a-Road`

0 commit comments

Comments
 (0)