2025-08-02 12:19:46 +0000 UTC

Distance Between Bus Stops

Code

class Solution:
    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:
        length = len(distance)
        forward_dist, back_dist = 0, 0
        forward, back = start, start
        while True:
            if forward == destination:
                return forward_dist
            if back == destination or length + back == destination:
                return back_dist
            forward_dist += distance[forward]
            forward = (forward + 1) % length
            back_dist += distance[back - 1]
            back -= 1
        raise Exception