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
Inputdistance = [1,2,3,4], start = 0, destination = 1
Output1
Distance between 0 and 1 is 1 or 9, so the minimum is 1.
Example 2
Inputdistance = [1,2,3,4], start = 0, destination = 2
Output3
Distance 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

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate