Skip to content
Discussion options

You must be logged in to vote

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:

  1. Cars moving left ('L') will never collide if all cars to their left are also moving left
  2. Cars moving right ('R') will never collide if all cars to their right are also moving right
  3. A moving car collides when it meets an obstacle (either a stationary car or a car moving in the opposite direction)

A…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Dec 4, 2025
Maintainer Author

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Dec 4, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Dec 4, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants