A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between all pairs of neighboring stops, where distance[i] is the distance between stop i and stop (i + 1) % n.
Return the shortest distance between the given start and destination stops.
Function Signature
def distance_between_bus_stops(distance: list[int], start: int, destination: int) -> int:
Rules
-
The bus can travel in either direction around the circle.
-
start
may be smaller than, larger than or equal to
destination
. When the two stops are the same, the distance is
0
.
Constraints
-
1 <= n <= 10^4
-
distance.length == n
-
0 <= distance[i] <= 10^4
-
0 <= start, destination < n
Examples
Input: distance = [1,2,3,4], start = 0, destination = 1
Output: 1
The clockwise distance from stop 0 to stop 1 is 1. The counterclockwise distance is 2 + 3 + 4 = 9, so the shortest distance is 1.
Input: distance = [1,2,3,4], start = 0, destination = 2
Output: 3
The clockwise distance from stop 0 to stop 2 is 1 + 2 = 3. The counterclockwise distance is 3 + 4 = 7, so the shortest distance is 3.
Input: distance = [1,2,3,4], start = 3, destination = 0
Output: 4
Going clockwise, stop 3 reaches stop 0 in one step of length distance[3] = 4. The other way passes stops 2 and 1 for 3 + 2 + 1 = 6, so the shortest distance is 4.