FastPrepShortest Distance on a Circular Bus Route
Problem · Array

Shortest Distance on a Circular Bus Route

Learn this problem
EasyAmazon logoAmazonFULLTIMEOA
See Amazon hiring insights

Problem 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) → int

Examples

Example 1

distance = [1,2,3,4]start = 0destination = 2return = 3

Clockwise 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 = 11

Travel through stops 1 -> 2 -> 3 costs 10 + 1 = 11. The opposite direction costs 12 + 7 = 19.

Constraints

  • 2 <= distance.length <= 100000
  • 1 <= distance[i] <= 10000
  • 0 <= start, destination < distance.length
  • start != destination

More Amazon problems

drafts saved locally
public int shortestBusRouteDistance(int[] distance, int start, int destination) {
  // Write your code here.
}
distance[1,2,3,4]
start0
destination2
expected3
checking account