JuniorArray
Distance Between Bus Stops
A bus has n stops numbered from 0 to n - 1 that form a circle. The distance between neighboring stops is given by distance, where distance[i] is the distance between stop i and stop (i + 1) % n.
The bus can travel in both directions: clockwise and counterclockwise.
Return the shortest distance between the given start and destination stops.
Example 1
Input
distance = [1,2,3,4], start = 0, destination = 1Output
1Distance between 0 and 1 is 1 or 9, so the minimum is 1.
Example 2
Input
distance = [1,2,3,4], start = 0, destination = 2Output
3Distance between 0 and 2 is 3 or 7, so the minimum is 3.
Constraints
- 1 <= n <= 10^4
- distance.length == n
- 0 <= start, destination < n
- 0 <= distance[i] <= 10^4