Tap here to switch tabs
Problem
Submissions

Problem: Count Collisions on a Road

med
30 min
Try to solve the Count Collisions on a Road problem.

Statement

There are nn cars located along an infinitely long road, positioned from left to right. You are given a 00 indexed string directions of length nn, where directions[i] denotes the initial state of the car at index i:

  • 'L' indicates that the car is moving left.

  • 'R' indicates that the car is moving right.

  • 'S' indicates that the car is stationary.

All moving cars travel at the same constant speed.

Collisions occur based on the following conditions:

  • If a car moving right collides with a car moving left, the total number of collisions increases by 22, and both cars come to a stop, becoming stationary.

  • If a moving car collides with a stationary car, the total number of collisions increases by 11, and the moving car comes to a stop, becoming stationary.

Once a car becomes stationary, it remains at that position on the road.

Return the total number of collisions that occur on the road.

Constraints:

  • 1≤1 \leq directions.length ≤105\leq 10^5

  • directions[i] is either 'L', 'R', or 'S'.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Count Collisions on a Road

med
30 min
Try to solve the Count Collisions on a Road problem.

Statement

There are nn cars located along an infinitely long road, positioned from left to right. You are given a 00 indexed string directions of length nn, where directions[i] denotes the initial state of the car at index i:

  • 'L' indicates that the car is moving left.

  • 'R' indicates that the car is moving right.

  • 'S' indicates that the car is stationary.

All moving cars travel at the same constant speed.

Collisions occur based on the following conditions:

  • If a car moving right collides with a car moving left, the total number of collisions increases by 22, and both cars come to a stop, becoming stationary.

  • If a moving car collides with a stationary car, the total number of collisions increases by 11, and the moving car comes to a stop, becoming stationary.

Once a car becomes stationary, it remains at that position on the road.

Return the total number of collisions that occur on the road.

Constraints:

  • 1≤1 \leq directions.length ≤105\leq 10^5

  • directions[i] is either 'L', 'R', or 'S'.