# Car Pooling algorithm time complexity

I created a solution for the car pooling algorithm.

### Problem:

You are driving a vehicle that has capacity empty seats initially available for passengers. The vehicle only drives east (ie. it cannot turn around and drive west.)

Given a list of trips, trip[i] = [num_passengers, start_location, end_location] contains information about the i-th trip: the number of passengers that must be picked up, and the locations to pick them up and drop them off. The locations are given as the number of kilometers due east from your vehicle's initial location.

Return true if and only if it is possible to pick up and drop off all passengers for all the given trips.

Example 1:

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


Example 2:

Input: trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11
Output: true


My solution:

var carPooling = function(trips, capacity) {
if(!trips || trips.length === 0)    return true;

trips.sort((a,b) => a[1]-b[1]);
let dropOff = new Map();
let seated = 0;
while(trips.length > 0) {
const trip = trips.shift();
const pickup = trip[1];
for(const [key, value] of dropOff) {
if(key <= pickup) {
seated -= value;
dropOff.delete(key);
}
}
if(seated + trip[0] > capacity)
return false;
seated += trip[0];
if(!dropOff.has(trip[2]))
dropOff.set(trip[2], trip[0]);
else
dropOff.set(trip[2], dropOff.get(trip[2]) + trip[0]);
}

return true;
};


Is the time complexity O(N^2logN)? I sort the input array which gives me O(NlogN). Then, I have a nested loop N^2? I'm not sure about this nested loop because it is a loop inside my hash table. By the way, do you guys have any idea about how to get a better performance?

• The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly. – Mast Aug 13 '20 at 6:58
• Hey, I have yet improved my answer, as I realized that you don't need two lists, one is just enough. I have also added time and memory complexities to individual parts of the algorithm to show where the weakest point is now. So check it out :) – slepic Aug 13 '20 at 17:52
• It's awesome! Thank you – myTest532 myTest532 Aug 13 '20 at 19:12

The sort is not in the loop, so your algorithm is "just" O(n^2) because the loop dominates, the O(n * log(n)) of the sort is neglegable (for big n). You can solve the problem in O(n * log(n)) and without sorting or destroying the input.

1. Walk through the trips and build a Map location => {liftUps, dropOffs} (O(n) in time, O(n) in memory)

2. Iterate the map and build an array of {location, liftUps, dropOffs} (O(n) in time, O(n) in memory)

3. sort the array by location (this is now the slowest part of the algorithm: O(n * log(n)) in time, O(1) in memory)

4. iterate the array adding liftUps and subtracting dropOffs for each location to an aggregate integer that starts at zero. (O(n) in time, O(1) in memory)

5. whenever you surpass capacity return false; return true if the loop finishes

const carPooling = (trips, capacity) => {
let i = 0;
const map = new Map();
for (i = 0; i < trips.length; ++i) {
const passangers = trips[i][0];
const start = trips[i][1];
const end = trips[i][2];

if (map.has(start)) {
map.set(start, map.get(start) + passangers);
} else {
map.set(start, passangers);
}

if (map.has(end)) {
map.set(end, map.get(end) - passangers);
} else {
map.set(end, -passangers);
}
}

const list = new Array(map.size);
i = 0;
for (const [location, exchange] of map) {
list[i++] = {location: location, exchange: exchange}
}

list.sort((a, b) => a.location - b.location);

let occupied = 0;
for (i = 0; i < list.length; ++i) {
occupied += list[i].exchange;
if (occupied > capacity) {
return false;
}
}

return true;
}

console.log(carPooling([[2,1,5],[3,3,7]], 4));
console.log(carPooling([[3,2,7],[3,7,9],[8,3,9]], 11));