1184. Distance Between Bus Stops

Posted whatyouthink

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1184. Distance Between Bus Stops相关的知识,希望对你有一定的参考价值。

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.

利用模运算搞一搞就可以了,求一个遍顺时针的,然后total-顺时针的等于逆时针的,顺时针和逆时针求最小

class Solution(object):
    def distanceBetweenBusStops(self, distance, start, destination):
        """
        :type distance: List[int]
        :type start: int
        :type destination: int
        :rtype: int
        """
        n = len(distance)
        ans = 0
        while start != destination:
            ans += distance[start]
            start = (start + 1) % n
            
        return min(ans, sum(distance) - ans)

 

以上是关于1184. Distance Between Bus Stops的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 1184. 公交站间的距离

每日一题1184. 公交站间的距离

Finding distance between two curves

783. Minimum Distance Between BST Nodes

783. Minimum Distance Between BST Nodes

783. Minimum Distance Between BST Nodes