AlgoLounge

Car Pooling

medium

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 integer array trips where trips[i] = [numPassengers[i], from[i], to[i]] indicates that the ith trip has numPassengers[i] passengers and the locations to pick them up and drop them off are from[i] and to[i] 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.

Examples

Example 1

Input:  trips = [[4,1,2],[3,2,4]], capacity = 4
Output: true

Example 2

Input:  trips = [[2,1,3],[3,2,4]], capacity = 4
Output: false

Constraints

  • 1 <= trips.length <= 1000
  • trips[i].length == 3
  • 1 <= numPassengers[i] <= 100
  • 0 <= from[i] < to[i] <= 1000
  • 1 <= capacity <= 100000
TypeScript
Loading editor…
List of test cases (12)
Case 1
trips = [[4,1,2],[3,2,4]]
capacity = 4
Case 2
trips = [[2,1,3],[3,2,4]]
capacity = 4
Case 3
trips = [[2,1,5],[3,3,7]]
capacity = 4
Case 4
trips = [[2,1,5],[3,5,7]]
capacity = 5
Case 5
trips = [[3,2,7],[3,7,9],[8,3,9]]
capacity = 11
Case 6
trips = [[9,0,1],[3,3,4]]
capacity = 10
Case 7
trips = [[1,1,2],[2,2,3],[3,3,4]]
capacity = 3
Case 8
trips = [[1,1,6],[1,4,5],[1,6,7]]
capacity = 2
Case 9
trips = [[5,1,3],[4,2,6],[6,6,8]]
capacity = 9
Case 10
trips = [[1,2,4],[3,1,3],[2,3,5]]
capacity = 4
Case 11
trips = [[7,5,6],[6,7,8],[1,1,5]]
capacity = 8
Case 12
trips = [[20,0,5],[10,1,3],[5,4,6]]
capacity = 30