Count Collisions on a Road - Complete Solution Guide
Count Collisions on a Road is LeetCode problem 2211, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 i th 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: When two cars moving in opposite
Detailed Explanation
The problem presents a scenario of cars moving on an infinitely long road. Each car is either moving left ('L'), right ('R'), or stationary ('S'). The task is to calculate the total number of collisions based on the following rules: 1) Opposite directions colliding result in 2 collisions. 2) A moving car colliding with a stationary car results in 1 collision. After a collision, the involved cars become stationary. The input is a string 'directions' representing the movement of each car, and the output is the total number of collisions.
Solution Approach
The provided solution efficiently calculates the number of collisions by first identifying the boundaries of the potentially colliding cars. It ignores the leftmost cars moving left and the rightmost cars moving right as these cars will not collide with anything. Then, it iterates through the remaining cars and counts the number of cars that are not stationary ('S'). This count directly corresponds to the number of collisions because each such car will inevitably collide with either another moving car (resulting in 2 collisions which is simplified to 1 as the net effect is only 1 collision added), or a stationary car (resulting in 1 collision).
Step-by-Step Algorithm
- Step 1: Initialize 'left' to 0. Iterate from the beginning of the 'directions' string, incrementing 'left' until a non-'L' character is encountered or the end of the string is reached.
- Step 2: Initialize 'right' to n-1, where n is the length of 'directions'. Iterate from the end of the string, decrementing 'right' until a non-'R' character is encountered or 'left' exceeds the index.
- Step 3: Initialize 'collisions' to 0.
- Step 4: Iterate from 'left' to 'right' (inclusive). For each character in 'directions' within this range, if the character is not 'S', increment 'collisions'.
- Step 5: Return the final value of 'collisions'.
Key Insights
- Insight 1: Cars moving left at the beginning and cars moving right at the end will never collide, as they are moving away from the main traffic flow.
- Insight 2: Focus on the cars in the 'middle' section of the road. Once you identify the range of cars that *can* collide, the collisions are simply the number of non-stationary cars within that range.
- Insight 3: After a collision, cars become stationary. This simplifies the problem because we don't need to track individual car states after each collision. We just count the total number of cars that are not stationary within the collision-prone range.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: String, Stack, Simulation.
Companies
Asked at: Arcesium.