FunctionRacerProblemsRace

Jousting Carts

HardStacks and queuesPython, JavaScript, TypeScript, Java, C++

Carts stand on a straight track at the different positions positions. Cart i has strength strengths[i] and moves in the direction directions[i]: L toward smaller positions or R toward larger ones. All carts start at the same moment and move at the same speed.

When two carts meet head on, the weaker one is wrecked, and the stronger one loses as much strength as the weaker one had and moves on. Carts of equal strength wreck each other. Carts moving the same way never meet.

Return the strengths of the carts that are never wrecked, in input order (not in order of position), or an empty list if every cart is wrecked.

Example 1

positions [8,2,5,11]
strengths [3,5,4,6]
directions "LRLR"
returns [2,6]

The carts at 2 and 5 meet first: the 5 beats the 4 and drops to 1. The cart from 8 then beats it and drops to 2. The cart at 11 moves away from everyone.

Example 2

positions [0,10]
strengths [5,5]
directions "RL"
returns []

Equal strengths wreck each other.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems