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.
Each trip adds passengers on \([from,to)\). We need the load never to exceed capacity. Locations are at most \(1000\), so a difference array is enough.
Add at the start, subtract at the end, then prefix-sum and test every position against \(\textit{capacity}\).
The array runs to the latest drop-off; empty stops keep the previous load.
We can use the idea of a difference array, adding the number of passengers to the starting point of each trip and subtracting from the end point. Finally, we just need to check whether the prefix sum of the difference array does not exceed the maximum passenger capacity of the car.
The time complexity is \(O(n)\), and the space complexity is \(O(M)\). Here, \(n\) is the number of trips, and \(M\) is the maximum end point in the trips. In this problem, \(M \le 1000\).