Skip to content

Latest commit

 

History

History
47 lines (32 loc) · 1.23 KB

_853. Car Fleet.md

File metadata and controls

47 lines (32 loc) · 1.23 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 14, 2024

Last updated : July 01, 2024


Related Topics : Array, Stack, Sorting, Monotonic Stack

Acceptance Rate : 52.49 %


Solutions

Python

class Solution:
    def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
        stk = sorted([(pos, spd) for pos, spd in zip(position, speed)], key=lambda x: abs(x[0] - target), reverse=True)

        countFleets = 1
        currPos, currSpd = stk.pop()
        timeTaken = (target - currPos) / currSpd
        longestTimeTaken = timeTaken
        while stk :
            currPos, currSpd = stk.pop()
            timeTaken = (target - currPos) / currSpd

            if (not timeTaken <= longestTimeTaken) :
                countFleets += 1
                longestTimeTaken = timeTaken
        
        return countFleets