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

LeetCode 853. Car Fleet

时间:2018-06-18 12:38:07      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:min   cut   only   zip   head   sel   speed   with   amp   

1AC - Yay! A super cute one : ) Again, simulate & play with it in your mind, and you will get it.

Key: for car[i] at its position, it can only pass the target with the slowest car ahead of it. So the problem becomes, count # of `slowest` cars in the list.

class Solution(object):
    def carFleet(self, target, position, speed):
        """
        :type target: int
        :type position: List[int]
        :type speed: List[int]
        :rtype: int
        """
        if len(position) < 2: return len(position)
        
        # Get a sorted list of (position, time-to-target)
        ts = [(target-v)*1.0/s for (v, s) in zip(position, speed)]
        v = sorted(zip(position, ts))
        
        # Count the number of slowest fleets
        ret, slowest = 0, -1.0
        for (_, t) in reversed(v):
            if t > slowest:
                slowest = t
                ret += 1
        return ret
        

 

LeetCode 853. Car Fleet

标签:min   cut   only   zip   head   sel   speed   with   amp   

原文地址:https://www.cnblogs.com/tonix/p/9194820.html

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