Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Car fleet question better space complecity #973

Open
busi-reddy-karnati opened this issue Aug 30, 2022 · 1 comment
Open

Car fleet question better space complecity #973

busi-reddy-karnati opened this issue Aug 30, 2022 · 1 comment

Comments

@busi-reddy-karnati
Copy link

https://github.com/neetcode-gh/leetcode/blob/main/python/853-Car-Fleet.py

This doesn't actually need a stack to be used, we can just store the maximum time someone on the right takes

class Solution:
    def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
        pos_vel = sorted(zip(position,speed), reverse=True)
        max_seen = float("-inf")
        ans = 0
        for elem in pos_vel:
            vel = elem[1]
            pos = elem[0]
            time = (target-pos)/vel
            if max_seen < time:#I reach after everyone on my right, so i make one fleet
                ans += 1
                max_seen = time
        return ans
        
@Ahmad-A0
Copy link
Collaborator

Thanks, @busi-reddy-karnati. Feel free to submit a PR adding this as a separate solution, I think we should keep the original for anyone coming from the YouTube video.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants