The Car Pooling problem requires you to determine if a car with a given number of seats (capacity
) can successfully pick up and drop off all passengers for a series of trips. Each trip is represented as [numPassengers, from, to]
, meaning numPassengers
must be picked up at location from
and dropped off at location to
(with from < to
).
capacity
at any point.true
if all trips can be completed without exceeding capacity
, or false
otherwise.At first glance, you might consider simulating each trip one by one, keeping track of passengers in the car at every location. However, this would be inefficient if the locations range is large (e.g., up to 1000), and checking every possible location for every trip would be slow.
Instead, we can notice that the only times the number of passengers changes are at pickup (from
) and drop-off (to
) locations. This leads to a more efficient approach: track the net change in passengers at each location, and then simulate the journey in order, updating the car's occupancy as we go.
This is similar to the "sweep line" or "difference array" technique, where we only record changes at specific points and then process them in order.
from
(add) and dropped off at to
(subtract) using a difference array or a hash map.capacity
. If it does, return false
.capacity
, return true
.We use a map or array because lookups and updates are O(1), and sorting the keys is efficient since the number of unique locations is limited.
Input: trips = [[2,1,5],[3,3,7]], capacity = 4
At location 3, we have 5 passengers, which exceeds the capacity
of 4. Therefore, the function returns false
.
class Solution:
def carPooling(self, trips, capacity):
changes = [0] * 1001 # locations go up to 1000
for num, start, end in trips:
changes[start] += num
changes[end] -= num
curr = 0
for passengers in changes:
curr += passengers
if curr > capacity:
return False
return True
class Solution {
public:
bool carPooling(vector<vector<int>>& trips, int capacity) {
int changes[1001] = {0};
for (const auto& trip : trips) {
changes[trip[1]] += trip[0];
changes[trip[2]] -= trip[0];
}
int curr = 0;
for (int i = 0; i <= 1000; ++i) {
curr += changes[i];
if (curr > capacity) return false;
}
return true;
}
};
class Solution {
public boolean carPooling(int[][] trips, int capacity) {
int[] changes = new int[1001];
for (int[] trip : trips) {
changes[trip[1]] += trip[0];
changes[trip[2]] -= trip[0];
}
int curr = 0;
for (int i = 0; i <= 1000; i++) {
curr += changes[i];
if (curr > capacity) return false;
}
return true;
}
}
var carPooling = function(trips, capacity) {
const changes = Array(1001).fill(0);
for (const [num, start, end] of trips) {
changes[start] += num;
changes[end] -= num;
}
let curr = 0;
for (let i = 0; i <= 1000; i++) {
curr += changes[i];
if (curr > capacity) return false;
}
return true;
};
from
and to
, which is O(n * maxLocation), where n is the number of trips and maxLocation is the largest location value. This is inefficient for large inputs.
The Car Pooling problem is elegantly solved using the difference array (or sweep line) technique. By only recording changes at pick-up and drop-off points, we avoid unnecessary simulation and achieve an efficient solution. The key insight is to recognize that only the locations where passengers get in or out matter, making the solution both fast and easy to implement.