码迷,mamicode.com
首页 > 其他好文 > 详细

1184. Distance Between Bus Stops

时间:2020-07-03 19:42:49      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:long   int   object   direction   des   顺时针   逆时针   ISE   bsp   

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

标签:long   int   object   direction   des   顺时针   逆时针   ISE   bsp   

原文地址:https://www.cnblogs.com/whatyouthink/p/13232064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!