2211. Count Collisions on a Road #2494
-
|
Topics: There are You are given a 0-indexed string The number of collisions can be calculated as follows:
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:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
We need to count collisions between cars moving on a road. The key insight is that collisions only happen when cars are moving towards each other or when moving cars hit stationary cars. ApproachThe solution uses stack simulation. We'll process each car from left to right and maintain a stack of cars that are still "active" (either moving right or stationary after collisions). Key observations:
Algorithm:
Let's implement this solution in PHP: 2211. Count Collisions on a Road <?php
/**
* @param String $directions
* @return Integer
*/
function countCollisions($directions) {
$stack = [];
$collisions = 0;
for ($i = 0; $i < strlen($directions); $i++) {
$car = $directions[$i];
if ($car == 'R') {
// Right-moving car - might collide later
$stack[] = 'R';
}
elseif ($car == 'S') {
// Stationary car - all right-moving cars in stack will collide with it
while (!empty($stack) && end($stack) == 'R') {
array_pop($stack);
$collisions++; // R collides with S = 1 collision
}
$stack[] = 'S'; // Stationary car remains
}
else { // $car == 'L'
if (!empty($stack)) {
if (end($stack) == 'R') {
// R and L collide (opposite directions)
array_pop($stack);
$collisions += 2; // Opposite directions = 2 collisions
// The collision point becomes stationary
// Any other R cars in stack will now collide with this stationary point
while (!empty($stack) && end($stack) == 'R') {
array_pop($stack);
$collisions++; // Each R collides with stationary point = 1 collision
}
$stack[] = 'S'; // Collision point is now stationary
}
elseif (end($stack) == 'S') {
// L collides with stationary car
$collisions++; // L collides with S = 1 collision
// Stationary car remains, L becomes part of stationary point
}
// If top is 'L', no collision (both moving left)
}
// If stack is empty, this L will never collide
}
}
return $collisions;
}
// Test cases
echo countCollisions("RLRSLL") . "\n"; // Output: 5
echo countCollisions("LLRR") . "\n"; // Output: 0
?>Explanation:Let's walk through the example
Time Complexity: O(n) where n is the length of directions string. We process each car once. Space Complexity: O(n) for the stack in worst case. |
Beta Was this translation helpful? Give feedback.
We need to count collisions between cars moving on a road. The key insight is that collisions only happen when cars are moving towards each other or when moving cars hit stationary cars.
Approach
The solution uses stack simulation. We'll process each car from left to right and maintain a stack of cars that are still "active" (either moving right or stationary after collisions).
Key observations:
A…