Car Pooling

Try to solve the Car Pooling problem.

Statement

You are given a car with a fixed number of seats, denoted by an integer capacity. The car only travels in one direction — eastward — and does not make any U-turns.

You are also provided with an array, trips, where each element trips[i] =[numPassengersi,fromi,toi]= [\text{numPassengers}_i, \text{from}_i, \text{to}_i] represents a group of numPassengersi\text{numPassengers}_i that must be picked up at location fromi\text{from}_i and dropped off at location toi\text{to}_i. All locations are measured in kilometers east of the starting point.

Your task is to determine whether it is possible to complete all the trips without exceeding the car’s capacity at any point in time.

Return TRUE if all trips can be completed successfully, or FALSE otherwise.

Constraints:

  • 1≤1 \leq trips.length ≤1000\leq 1000

  • trips[i].length ==3== 3

  • 1≤numPassengersi≤1001 \leq \text{numPassengers}_i \leq 100

  • 0≤fromi<toi≤10000 \leq \text{from}_i < \text{to}_i \leq 1000

  • 1≤1 \leq capacity ≤105\leq 10^5

Examples

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.