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:
2.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: 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:
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.
Constraints:
1 <= directions.length <= 105directions[i] is either 'L', 'R', or 'S'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to counting collisions involves simulating every car's movement one step at a time. We track each car and check for crashes after each simulated step. This is like watching a slow-motion replay of the entire road to see every potential collision.
Here's how the algorithm would work step-by-step:
def count_collisions_brute_force(directions):
total_collisions = 0
road_length = len(directions)
# Iterate through each car
for first_car_index in range(road_length):
for second_car_index in range(first_car_index + 1, road_length):
first_car_direction = directions[first_car_index]
second_car_direction = directions[second_car_index]
# Check if cars are moving towards each other
if (first_car_direction == 'R' and second_car_direction == 'L'):
total_collisions += 1
# Check if the first car is going RIGHT and the second car is stopped.
elif (first_car_direction == 'R' and second_car_direction == 'S'):
total_collisions += 1
# Check if the first car is stopped and the second car is going LEFT.
elif (first_car_direction == 'S' and second_car_direction == 'L'):
total_collisions += 1
return total_collisionsThe optimal strategy avoids simulating every car movement. Instead, we focus on the stable parts of the road where no collisions occur, and count collisions at the boundaries. This drastically reduces the amount of work needed.
Here's how the algorithm would work step-by-step:
def count_collisions(directions):
number_of_cars = len(directions)
collisions = 0
left_index = 0
while left_index < number_of_cars and directions[left_index] == 'L':
left_index += 1
right_index = number_of_cars - 1
while right_index >= 0 and directions[right_index] == 'R':
right_index -= 1
# Eliminate cars that will not collide
directions = directions[left_index:right_index+1]
number_of_cars = len(directions)
for direction in directions:
if direction != 'S':
# Any moving car will collide and stop
collisions += 1
return collisions| Case | How to Handle |
|---|---|
| Null or empty directions string | Return 0, as there are no cars and thus no collisions. |
| Directions string with a single character | Return 0, as a single car cannot collide with anything. |
| Directions string with only 'S' characters | Return 0, since stationary cars cannot collide with each other. |
| Directions string with only 'L' characters | Return 0, all cars are moving left, no collisions are possible in an open road setting. |
| Directions string with only 'R' characters | Return 0, all cars are moving right, no collisions are possible in an open road setting. |
| Very long directions string to test for efficiency | Iterate through the string once, ensuring O(n) time complexity to avoid timeouts. |
| A sequence of 'R' followed by 'L' | Increment collision count by 2, as both cars will collide and become stationary. |
| A sequence of 'R' followed by 'S' followed by 'L' | Increment collision count by 2, the 'R' will collide with 'S', and the 'L' will collide with 'S'. |