소프트웨어에 대한 모든 것

LeetCode 풀기 - 1094. Car Pooling 본문

알고리즘/LeetCode

LeetCode 풀기 - 1094. Car Pooling

앤테바 2022. 1. 7. 07:20
반응형

1094. Car Pooling

https://leetcode.com/problems/car-pooling/

 

Car Pooling - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제)

솔루션1)

class Solution(object):
    def carPooling(self, trips, capacity):
        """
        :type trips: List[List[int]]
        :type capacity: int
        :rtype: bool
        """
        d = defaultdict(int)
        for trip in trips:
            d[trip[1]] += trip[0]
            d[trip[2]] -= trip[0]
        
        p, s, e = zip(*trips)
        occupied = 0
        for location in sorted(list(set(s+e))):
            occupied += d[location]
            if occupied > capacity:
                return False
        return True

 

반응형
Comments