Last Truck to Leave a One-Dimensional Lane
Implement last_exit_time(lane_length, positions, velocities).
Distinct trucks start at integer positions strictly between 0 and lane_length. Truck i moves continuously at constant nonzero integer velocity velocities[i]: positive is right, negative is left. A truck leaves as soon as it crosses either boundary. When two trucks meet, they instantaneously exchange velocities. Trucks have zero length, and the input excludes simultaneous collisions involving three or more trucks.
Return the time when the final truck leaves as a reduced fraction (numerator, denominator) with a positive denominator.
Constraints
-
1 <= lane_length <= 10^9
-
1 <= len(positions) == len(velocities) <= 200,000
-
Initial positions are distinct.
-
1 <= abs(velocities[i]) <= 10^9
-
Every truck initially moves toward one of the two boundaries.
Example behavior
A truck at position 3 moving left at speed 2 would reach the left boundary after 3/2 time units if no label-changing collision affected its identity.
Candidate clarifications
Confirm collision physics, whether truck identity matters to the requested final time, boundary semantics, and the required exact representation.