Suppose there is a vehicle that has capacity empty seats initially available for passengers. The vehicle only drives east, so we cannot turn around and drive west. We have given a list of trips, trip[i] = [num_passengers, start_location, end_location], that contains information about the ith trip:, so that is the number of passengers that must be picked up, and the locations to pick them up and drop them off. Here the locations are given as the number of kilometers due east from our vehicle's initial location. Our module will return true if and only if it is possible to pick up and drop off all passengers for all the given trips. So if trips are like [[2,1,5],[3,3,7]] and capacity is 5, then the output will be true.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution(object): def carPooling(self, trips, capacity): stops = [0 for i in range(1001)] for i in trips: stops[i]+=i stops[i]-=i for i in stops: capacity-=i if capacity<0: return False return capacity>=0 ob = Solution() print(ob.carPooling([[2,1,5],[3,3,7]],5))