Problem · Array
Shortest Distance on a Circular Bus Route
Learn this problemProblem statement
For this exercise, assume a bus route has n stops arranged in a circle. The array distance contains the distance from stop i to stop (i + 1) mod n.
Given two distinct stops, start and destination, return the shorter travel distance between them. A bus may travel clockwise or counterclockwise around the circle.
Function
shortestBusRouteDistance(distance: int[], start: int, destination: int) → intExamples
Example 1
distance = [1,2,3,4]start = 0destination = 2return = 3Clockwise travel from stop 0 to stop 2 costs 1 + 2 = 3. The other direction costs 4 + 3 = 7, so the answer is 3.
Example 2
distance = [7,10,1,12]start = 1destination = 3return = 11Travel through stops 1 -> 2 -> 3 costs 10 + 1 = 11. The opposite direction costs 12 + 7 = 19.
Constraints
2 <= distance.length <= 1000001 <= distance[i] <= 100000 <= start, destination < distance.lengthstart != destination