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.

&nbsp;
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


&nbsp;
Constraints:


	1 &lt;= trips.length &lt;= 1000
	trips[i].length == 3
	1 &lt;= numPassengersi &lt;= 100
	0 &lt;= fromi &lt; toi &lt;= 1000
	1 &lt;= capacity &lt;= 105


class Solution:
    def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
        endheap = []
        startheap = []
        
        for i in range(len(trips)):
            endheap.append((trips[i][2],trips[i][0],trips[i][1]))
            startheap.append((trips[i][1],trips[i][0],trips[i][2]))
        heapify(endheap)
        heapify(startheap)
        cur = 0
        while startheap:
            start,num,end = heappop(startheap)
            while start >= endheap[0][0]:
                newend,newnum,newstart = heappop(endheap)
                cur -= newnum
            cur += num
            print(cur)
            if cur >capacity:
                return False
        return True
                
                
            
        
class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        Map<Integer, Integer> destinationToPassengers = new TreeMap<>();
        for(int[] trip : trips) {
            int currPassengersAtPickup = destinationToPassengers.getOrDefault(trip[1], 0);
            int currPassengersAtDrop = destinationToPassengers.getOrDefault(trip[2], 0);
            destinationToPassengers.put(trip[1], currPassengersAtPickup + trip[0]);
            destinationToPassengers.put(trip[2], currPassengersAtDrop - trip[0]);
        }

        int currPassengers = 0;
        for(int passengers : destinationToPassengers.values()) {
            currPassengers += passengers;

            if(currPassengers > capacity) {
                return false;
            }
        }
        return true;
    }
}
class Solution {
typedef pair<int, int> pd;
public:
    bool carPooling(vector<vector<int>>& trips, int capacity) {
        int seat=0;
        priority_queue<pd, vector<pd>, greater<pd>>pq;
        for(auto it : trips)
        {
              pq.push({it[1], +it[0]});
              pq.push({it[2], -it[0]});
        }
        while(!pq.empty())
        {
            // cout<<pq.top().first<<" "<<pq.top().second<<endl;
            // cout<<"seat-"<<seat<<endl;
            seat+=pq.top().second;
            if(seat>capacity) return false;
            pq.pop();
        }
        return true;
    }
};
/**
 * @param {number[][]} trips
 * @param {number} capacity
 * @return {boolean}
 */
var carPooling = function(trips, capacity) {
    
    // sort trips by destination distance
    trips.sort((a, b) => a[2] - b[2]);
    
    // build result array, using max distance
    const lastTrip = trips[trips.length - 1];
    const maxDistance = lastTrip[lastTrip.length - 1];
    const arr = new Array(maxDistance + 1).fill(0);
    
    // build partial sum array
    for (const [val, start, end] of trips) {
        arr[start] += val;
        arr[end] -= val;
    }

    // build combined sum array
    let sum = 0;
    for (let i = 0; i < arr.length; i++) {
        sum += arr[i];
        if (sum > capacity) return false;
    }
    
    return true;
};
Car Pooling