Skip to content
LC-0853 Medium LeetCode

853. Car Fleet

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 53% Topics: Array, Stack, Sorting, Monotonic Stack
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn)
# Space: O(n)

class Solution(object):
    def carFleet(self, target, position, speed):
        """
        :type target: int
        :type position: List[int]
        :type speed: List[int]
        :rtype: int
        """
        times = [float(target-p)/s for p, s in sorted(zip(position, speed))]
        result, curr = 0, 0
        for t in reversed(times):
            if t > curr:
                result += 1
                curr = t
        return result

Solution from kamyu104/LeetCode-Solutions · MIT