Passengers Pick-up and Drop-off Algorithms (Car Pooling) via Greedy Algorithm


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 = [[2,1,5],[3,3,7]], capacity = 5
Output: true

Example 3:
Input: trips = [[2,1,5],[3,5,7]], capacity = 3
Output: true

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

Constraints:

  • trips.length <= 1000
  • trips[i].length == 3
  • 1 <= trips[i][0] <= 100
  • 0 <= trips[i][1] < trips[i][2] <= 1000
  • 1 <= capacity <= 100000

Hint:
Sort the pickup and dropoff events by location, then process them in order.

Greedy Algorithm for Car Pooling

The C++ std::map sorts the keys by default. And when pickuping the passengers, we increment the counter for that pooling station, and when dropping the passengers, we decrement the counter similarly. And we process the pooling stations in order to update the global counter which represents the current number of passengers. If at anytime, it exceeds the capacity, we simply return false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    bool carPooling(vector<vector<int>>& trips, int capacity) {
        map<int, int> data;
        for (const auto &n: trips) {
            int start = n[1];
            int end = n[2];
            int num = n[0];
            data[start] += num;
            data[end] -= num;
        }
        int s = 0;
        for (auto it = data.begin(); it != data.end(); ++ it) {
            s += it->second;
            if (s > capacity) return false;
        }
        return true;
    }
};
class Solution {
public:
    bool carPooling(vector<vector<int>>& trips, int capacity) {
        map<int, int> data;
        for (const auto &n: trips) {
            int start = n[1];
            int end = n[2];
            int num = n[0];
            data[start] += num;
            data[end] -= num;
        }
        int s = 0;
        for (auto it = data.begin(); it != data.end(); ++ it) {
            s += it->second;
            if (s > capacity) return false;
        }
        return true;
    }
};

(As the keys are sorted) – the map indicates a O(nlogN) complexity where N is the size of the trips. And the space complexity is O(N).

As the inputs are strictly bounded to limited range, we can use a static array to store the counters for the pooling stations. Then going through them in order requires O(1) constant. The C++ greedy algorithm takes O(1) constant space and O(N) time where N is the size of the trips.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
    bool carPooling(vector<vector<int>>& trips, int capacity) {
        int travel[1001] = { 0 };
        for (const auto n: trips) {
            travel[n[1]] += n[0];
            travel[n[2]] -= n[0];
        }
        int cur = 0;
        for (int i = 0; i < 1001; ++ i) {
            cur += travel[i];
            if (cur > capacity) {
                return false;
            }
        }
        return true;
    }
};
class Solution {
public:
    bool carPooling(vector<vector<int>>& trips, int capacity) {
        int travel[1001] = { 0 };
        for (const auto n: trips) {
            travel[n[1]] += n[0];
            travel[n[2]] -= n[0];
        }
        int cur = 0;
        for (int i = 0; i < 1001; ++ i) {
            cur += travel[i];
            if (cur > capacity) {
                return false;
            }
        }
        return true;
    }
};

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
534 words
Last Post: Maximize Sum Of Array After K Negations using Greedy Algorithm via Priority Queue/Min Element
Next Post: Beginner's Guide to the zip() function in Python3

The Permanent URL is: Passengers Pick-up and Drop-off Algorithms (Car Pooling) via Greedy Algorithm

Leave a Reply