[220106] Car Pooling

There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).

You are given the integer capacity and an array trips where trips[i] = [numPassengersi, fromi, toi] indicates that the ith trip has numPassengersi passengers and the locations to pick them up and drop them off are fromi and toi respectively. The locations are given as the number of kilometers due east from the car's initial location.

Return true if it is possible to pick up and drop off all passengers for all the given trips, or false otherwise.

class Solution:
    def carPooling(self, trips, capacity):

        i = 0
        people = 0
        max_stop = trips[0][2]
        trip_run = True

        # 确定最大站数
        for each in trips:
            max_stop = max(max_stop, each[2])

        while trip_run:
            # 上下车
            for each in trips:
                if each[1] == i:
                    people += each[0]
                elif each[2] == i:
                    people -= each[0]

            # 超载,break
            if people > capacity:
                trip_run = False
                break

            # 到站,break
            if i == max_stop:
                break
            else:
                i += 1

        return trip_run

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值