### Daily classic

South Village of The Capital — Cui Hu (Tang)

Last year today in this door, people face peach blossom set off red.

People do not know where to go, peach blossom still smile spring breeze.

### describe

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 trip[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.

Example 1:

``````Input: trips = [[2,1,5],[3,3,7]], capacity = 4
Output: false
Copy the code``````

Example 2:

``````Input: trips = [[2,1,5],[3,3,7]], capacity = 5
Output: true
Copy the code``````

Note:

• 1 <= trips.length <= 1000
• trips[i].length == 3
• 1 <= numPassengersi< = 100
• 0 <= fromi < toi< = 1000
• 1 <= capacity <= 10^5

### parsing

There is an empty car for capacity people, and the car can only drive east. Given an integer capacity and an array TRIPS, where trip[I] = [numersi, fromi, toi], number of passengersi passengers, They will be picked up by Fromi and Toi. These positions are given kilometers east of the car’s initial position. Return true if all passengers can be picked up, false otherwise.

The number of people getting on the bus after they get off the bus at different time points is greater than capacity. In this case, the triadic element LIST TRIPS is split into a binary element list L. [to, -numpassengers] [from, numPassengers] [to, -numpassengers] We sort L by time, so that when we traverse L, we traverse by time, and then calculate whether the number of people on and off the bus exceeds capacity. If so, return False directly, otherwise return True.

``````class Solution(object):
def carPooling(self, trips, capacity):
"""
:type trips: List[List[int]]
:type capacity: int
:rtype: bool
"""
L = []
people = 0
for n,s,e in trips:
L.append([s, n])
L.append([e, -n])
L.sort()
for position,n in L:
people += n
if people>capacity:
return False
return True

Copy the code``````

### The results

``Given in the Python online submissions for Car Pooling. Submissions in Python online submissions for Car Pooling.Copy the code``