Last Truck Exit Time
MediumMath ยท Uber
Trucks drive along a one-lane road from 0 to length. positions[i] is truck i's starting point and speeds[i] its signed velocity in units per hour: positive drives toward length, negative toward 0.
When two trucks meet, they swap speeds and carry on. A truck leaves the road the moment it reaches 0 or length. Return the time at which the last truck leaves as a fraction in lowest terms, formatted "p/q", or just "p" when it is a whole number.
Example 1
- Input:
- length = 10, positions = [2, 5, 8], speeds = [3, -2, 1]
- Output:
- "8/3"
- Explanation
- The first two trucks meet at time 3/5 and swap speeds; the last truck leaves at time 8/3.
Example 2
- Input:
- length = 12, positions = [6], speeds = [-3]
- Output:
- "2"
Example 3
- Input:
- length = 24, positions = [6, 18], speeds = [4, -9]
- Output:
- "9/2"
Constraints
1 <= length <= 10^61 <= positions.length <= 10^5, andspeeds.length == positions.length0 < positions[i] < length, all distinct-10^4 <= speeds[i] <= 10^4,speeds[i] != 0