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.
Example 1:
Input: trips = [[2,1,5],[3,3,7]], capacity = 4 Output: false
Example 2:
Input: trips = [[2,1,5],[3,3,7]], capacity = 5 Output: true
Constraints:
1 <= trips.length <= 1000
trips[i].length == 3
1 <= numPassengersi <= 100
0 <= fromi < toi <= 1000
1 <= capacity <= 105
This problem asks whether a car with a given capacity can accommodate all passengers from a list of trips. Each trip specifies the number of passengers, the pickup location, and the drop-off location. The car only moves east.
The most efficient solution uses a difference array. This approach avoids iterating through all possible locations, improving efficiency.
Algorithm:
Initialization: Create a difference array d
of size M + 1
, where M
is the maximum drop-off location. Initialize all elements to 0.
Processing Trips: Iterate through each trip [numPassengers, from, to]
:
numPassengers
to d[from]
(passengers get on).numPassengers
from d[to]
(passengers get off).Prefix Sum Calculation: Calculate the prefix sum of the difference array. This represents the number of passengers in the car at each location.
Capacity Check: Iterate through the prefix sum. If at any point the prefix sum exceeds capacity
, return false
. Otherwise, return true
.
Time Complexity: O(n + M), where n is the number of trips and M is the maximum drop-off location. The dominant operation is iterating through the trips and calculating the prefix sum. Since M is at most 1000 in the problem constraints, this simplifies to O(n) for practical purposes.
Space Complexity: O(M), the space used for the difference array. Again, since M is bounded, this is effectively constant space.
from itertools import accumulate
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
mx = max(e[2] for e in trips) # Find maximum drop-off location
d = [0] * (mx + 1) # Initialize difference array
for x, f, t in trips: # Process each trip
d[f] += x # Add passengers at pickup
d[t] -= x # Subtract passengers at drop-off
prefix_sums = list(accumulate(d)) # Calculate prefix sums
return all(s <= capacity for s in prefix_sums) # Check capacity at each location
Java:
class Solution {
public boolean carPooling(int[][] trips, int capacity) {
int[] d = new int[1001]; // Assuming max location <= 1000
for (int[] trip : trips) {
d[trip[1]] += trip[0];
d[trip[2]] -= trip[0];
}
int currentPassengers = 0;
for (int i = 0; i < 1001; i++) {
currentPassengers += d[i];
if (currentPassengers > capacity) return false;
}
return true;
}
}
C++:
class Solution {
public:
bool carPooling(vector<vector<int>>& trips, int capacity) {
vector<int> d(1001, 0); // Assuming max location <= 1000
for (auto& trip : trips) {
d[trip[1]] += trip[0];
d[trip[2]] -= trip[0];
}
int currentPassengers = 0;
for (int i = 0; i <= 1000; ++i) {
currentPassengers += d[i];
if (currentPassengers > capacity) return false;
}
return true;
}
};
Other languages (Javascript, TypeScript, Go, Rust, C#) would follow similar patterns, adapting the syntax for array/vector creation and manipulation accordingly. The fundamental algorithmic approach using the difference array remains consistent.