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) =&gt; a[1]-b[1]);
    let dropOff = new Map();
    let seated = 0;
    while(trips.length &gt; 0) {
        const trip = trips.shift();
        const pickup = trip[1];
        for(const [key, value] of dropOff) {
            if(key &lt;= pickup) {
                seated -= value;
                dropOff.delete(key);
            }
        }
        if(seated + trip[0] &gt; 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 sort is not in the loop, so your algorithm is &quot;just&quot; 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.

Walk through the trips and build a Map location =&gt; {liftUps, dropOffs} (O(n) in time, O(n) in memory)

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

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

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)

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


const carPooling = (trips, capacity) =&gt; {
    let i = 0;
  const map = new Map();
  for (i = 0; i &lt; 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) =&gt; a.location - b.location);
  
  let occupied = 0;
  for (i = 0; i &lt; list.length; ++i) {
    occupied += list[i].exchange;
    if (occupied &gt; 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));