Leetcode 1094. Car Pooling

Problem Explanation:

We are given a situation where we are driving a vehicle with a certain seating capacity. We have to drive strictly in the east direction i.e., we cannot reverse or drive west. A list of trips is also given, each trip consists of [num_passengers, start_location, end_location] which denotes the number of passengers to pick up, and the start and end location of the trip. The locations are given in kilometers from the vehicle's initial location.

Our objective is to find out if it's possible to pick up and drop off all the passengers for all the given trips.

To better understand this, let's walk through an example:

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

In the first trip, we pick up two passengers at location 1 and drop them off on location 5. In the second trip, we pick up three passengers at location 3 and drop them off at location 7. But the capacity of the vehicle is only 4 passengers. So at location 3, we have 4 passengers in the vehicle and attempting to pick up 3 more. Hence we have insufficient capacity and the output is false.

Solution Approach:

The solution works by creating a key-value pair for start and end locations with corresponding passenger pickup and drop-offs at these locations. The concept of a timeline is utilized here where passenger pick-ups add to the total and drop-offs subtract from it.

The algorithm runs through this timeline and adds passenger changes to the variable 'currentPassengers'. If at any point, this variable exceeds the capacity of the vehicle, then it means we can't accomplish all the trips and the algorithm returns false. Otherwise, it continues till the end of the timeline and returns true.

Python Solution:

1
2python
3class Solution:
4    def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
5        timeline = [0] * 1001
6      
7        for trip in trips:
8            timeline[trip[1]] += trip[0]
9            timeline[trip[2]] -= trip[0]
10        
11        currentPassengers = 0
12        for passengerChange in timeline:
13            currentPassengers += passengerChange
14            if currentPassengers > capacity:
15                return False
16
17        return True

Java Solution:

1
2java
3public boolean carPooling(int[][] trips, int capacity) {
4    int[] timeline = new int[1001];
5    for (int[] trip : trips) {
6        timeline[trip[1]] += trip[0];
7        timeline[trip[2]] -= trip[0];
8    }
9    int passengers = 0;
10    for (int time : timeline) {
11        passengers += time;
12        if (passengers > capacity) {
13            return false;
14        }
15    }
16    return true;
17}

JavaScript Solution:

1
2javascript
3var carPooling = function(trips, capacity) {
4    let timeline = [];
5    for (let trip of trips) {
6        timeline[trip[1]] = (timeline[trip[1]] || 0 ) + trip[0];
7        timeline[trip[2]] = (timeline[trip[2]] || 0 ) - trip[0];
8    }
9    let passengers = 0;
10    for (let time of timeline) {
11        passengers += time;
12        if (passengers > capacity) {
13            return false;
14        }
15    }
16    return true;
17};

C++ Solution:

1
2c++
3bool carPooling(vector<vector<int>>& trips, int capacity) {
4    vector<int> timeline(1001, 0);
5    for (auto trip : trips) {
6        timeline[trip[1]] += trip[0];
7        timeline[trip[2]] -= trip[0];
8    }
9    int passengers = 0;
10    for (int time : timeline) {
11        passengers += time;
12        if (passengers > capacity) {
13            return false;
14        }
15    }
16    return true;
17}

C# Solution:

1
2csharp
3public bool CarPooling(int[][] trips, int capacity) {
4    int[] timeline = new int[1001];
5    foreach(var trip in trips) {
6        timeline[trip[1]] += trip[0];
7        timeline[trip[2]] -= trip[0];
8    }
9    int passengers = 0;
10    foreach(var time in timeline) {
11        passengers += time;
12        if (passengers > capacity) {
13            return false;
14        }
15    }
16    return true;
17}

The four languages — Python, Java, Javascript, and C++ — all follow the same algorithm and base logic. They all use an array, timeline, to keep track of the passengers pickup and drop at each kilometer. They differ in syntax and built-in functions, but the approach is almost identical for each.

The C# solution also executes the same algorithm, but in a different way. It firstly generates an array called timeline, which is used to store each passenger pickup and drop-off at each specific kilometer. Then, it iterates over all the trips and adds, for each trip's start location, the number of passengers that are picked up and subtracts the same number from the end location. Following that, it iterates over all kilometers, adding to the total number of passengers onboard at each location and checks if this number is larger than the capacity. If it is, it returns false, otherwise, it returns true.

Each of these implementations provides an efficient solution to the problem and can guide you in creating similar solutions in future programming tasks. The choice of language should basically depend on personal preferences and project requirements. If speed, low memory usage, and high flexibility are essential, C++ can be a suitable choice. If ease of writing, readability, and wide range of libraries are necessary, Python might be the best option. If web development is your major concern, JavaScript would be the most appropriate. Java can also be a good all-rounder, providing a balance between speed, performance, and readability. Lastly, if you're working primarily with .NET technologies, C# would be the best fit.

By understanding these solutions, you can adjust your strategies depending on the language you're using and the specific requirements of the problems you're dealing with.


Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.


TA 👨‍🏫