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 the stops number i and (i + 1) % n.
The bus goes along both directions i.e. 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 = 1 Output: 1 Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1.
Example 2:

Input: distance = [1,2,3,4], start = 0, destination = 2 Output: 3 Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.
Example 3:

Input: distance = [1,2,3,4], start = 0, destination = 3 Output: 4 Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.
Constraints:
1 <= n <= 10^4distance.length == n0 <= start, destination < n0 <= distance[i] <= 10^4When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves checking every possible route between the start and destination bus stops. We will calculate the distance for each possible path and select the shortest one. This guarantees finding the optimal answer but can be inefficient.
Here's how the algorithm would work step-by-step:
def distance_between_bus_stops_brute_force(distances, start, destination):
total_distance = sum(distances)
# Calculate clockwise distance.
clockwise_distance = 0
current_index = start
while current_index != destination:
clockwise_distance += distances[current_index]
current_index = (current_index + 1) % len(distances)
# Calculate counter-clockwise distance.
counterclockwise_distance = total_distance - clockwise_distance
# Compare the two distances and return the minimum.
if clockwise_distance < counterclockwise_distance:
return clockwise_distance
else:
return counterclockwise_distanceTo find the shortest distance between two bus stops on a circular route, we can consider the distance in both directions. We can calculate the distance in one direction and then simply subtract that value from the total route length to get the distance in the other direction and see which one is smaller.
Here's how the algorithm would work step-by-step:
def distance_between_bus_stops(distances, start_stop, destination_stop):
number_of_stops = len(distances)
# Determine forward direction
if start_stop > destination_stop:
start_stop, destination_stop = destination_stop, start_stop
forward_distance = 0
for i in range(start_stop, destination_stop):
forward_distance += distances[i]
# Calculate total distance of the circle
total_distance = sum(distances)
# Find the distance in the reverse direction
backward_distance = total_distance - forward_distance
# Return the shortest distance.
return min(forward_distance, backward_distance)| Case | How to Handle |
|---|---|
| Empty distance array | Return 0 immediately as there are no distances to traverse. |
| Start and destination are the same | Return 0 immediately as the distance between the same location is zero. |
| Array with only one distance value | Return the distance itself if start is 0 and destination is 1; otherwise return 0. |
| Start index is greater than the length of the distance array | Use modulo operator (%) to wrap the index around to the beginning of the array. |
| Destination index is greater than the length of the distance array | Use modulo operator (%) to wrap the index around to the beginning of the array. |
| Negative distances in the array | Take the absolute value of distances before summing them to ensure non-negative distances. |
| Integer overflow when calculating the total distance | Use a larger data type (e.g., long) to store the total distance to prevent overflow. |
| Start index is greater than the destination index. | Calculate both clockwise and counterclockwise distances, then return the minimum of the two. |