Distance Between Bus Stops

Problem Id: 1184 Difficulty: Easy


Intuition

Solution


class Solution:
    def distanceBetweenBusStops(self, distance: List[int], start: int, end: int) -> int:
        tmp = 0
        index = start
        while index != end:
            tmp += distance[index]
            index = (index + 1) % len(distance)
        ans = tmp

        index = start
        tmp = 0
        while index != end:
            index = (index - 1) % len(distance)
            tmp += distance[index]
        ans = min(ans, tmp)
        return ans