Car Pooling
Decide whether one car with a fixed number of seats can carry every group of passengers on its trip.
A car has capacitytrips[numPassengers, from, to]fromtotruecapacityfalse
Example 1
Output: false
Explanation: between locations 3 and 5 there are 2 + 3 = 5 passengers.
Example 2
Output: true
Example 3
Output: true
Explanation: the first group gets off at 5 just as the second gets on.
The Idea
Think of a shuttle that drives down one long road and picks up groups of people along the way. Each group tells the driver where it gets on and where it gets off. The driver only worries about one thing: is there ever a moment when more people want a seat than the car has?
Take Example 1. Two passengers get on at location 1 and ride to 5. Three more get on at 3 and ride to 7. From 3 until 5, both groups are in the car, so it holds 2 + 3 = 5 people. With 4 seats that does not work, and the answer is false
With two trips this is easy to see. With hundreds of trips that overlap in all sorts of ways, how do we find the busiest stretch of the road without comparing every trip with every other one?
Notice that the number of people in the car only changes where someone gets on or off. In between, it stays the same. So write down, for each location, how much the count changes there: +2 at 1 and -2 at 5 for the first trip, +3 at 3 and -3 at 7 for the second. Then drive from location 0 and keep a running total. Look at the picture below.
The two bars at the top are the two trips. The row labelled “change” holds only the numbers we wrote down, and every other location gets 0. The row labelled “in the car” adds those changes up from the left: 0, then 2, 2, then 5 at location 3. The two orange cells, at locations 3 and 4, hold 5 passengers, more than the 4 seats.
A list like the “change” row, which stores how much a count goes up or down at each place instead of the count itself, is called a difference array. Adding its cells up from the start, as the “in the car” row does, is called a prefix sum, and it turns the changes back into the count at every location.
Example 3 shows one more detail. The first group gets off at 5, and the second gets on at 5. Both changes land in the same cell: -2 and +3 make +1, so the count at 5 goes from 2 to 3, never to 5. The people leaving free their seats before the new ones sit down, which is why 3 seats are enough.
Solution Approach
Turning the running total into code starts with a look at the plain way of counting, which is slower, and then settles three things: where to store the changes, what happens at a location where people both get off and get on, and when to stop.
Counting riders at every location
Why not count the riders directly? For each location from 0 to 1000, loop over all the trips and add up the passengers of every trip with from <= location < to
Storing the changes
Where do the changes go? Locations are whole numbers from 0 to 1000, so an int[2, 1, 5]
Getting off and on at the same location
At which cell should the passengers leave? A trip covers the road from fromtototo
Stopping early
When can we answer? As soon as the running total goes above capacityfalsecapacitytrue
Java Solution
The method carPooling
- First, create an
arrayint with 1001 cells, one for each location from 0 to 1000. Java fills a new array with zeros.change - Loop over
. For eachtrips , add its passengers,trip , totrip[0] , the cell where they get on, and subtract them fromchange[trip[1]] , the cell where they get off.change[trip[2]] - Start a running total
at 0. A second loop walks alonginCar from location 0 and adds each cell intochange , which is then the number of passengers right after that location.inCar - After each addition, if
is greater thaninCar , returncapacity .false - If the second loop reaches location 1000 without going over, return
.true
The main
public class CarPooling {
public static boolean carPooling(int[][] trips, int capacity) {
int[] change = new int[1001]; // riders getting on (+) or off (-) at each location
for (int[] trip : trips) {
change[trip[1]] += trip[0]; // they get on at "from"
change[trip[2]] -= trip[0]; // they get off at "to", freeing the seats there
}
int inCar = 0;
for (int delta : change) {
inCar += delta; // passengers in the car after this location
if (inCar > capacity) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(carPooling(new int[][] {{2, 1, 5}, {3, 3, 7}}, 4));
System.out.println(carPooling(new int[][] {{2, 1, 5}, {3, 3, 7}}, 5));
System.out.println(carPooling(new int[][] {{2, 1, 5}, {3, 5, 7}}, 3));
System.out.println(carPooling(new int[][] {{3, 2, 7}, {3, 7, 9}, {8, 3, 9}}, 11));
System.out.println(carPooling(new int[][] {{5, 0, 1000}}, 4));
}
}
Running it prints:
false
true
true
true
false
The first three lines, falsetruetrueinCar
Look at the fourth line. The trip [8, 3, 9][3, 7, 9]true
The last line is false
What if the seats were freed one location late, at to + 1false
Complexity
The work is two array updates for each trip, plus one walk along the 1001 locations.
- Time: O(n + L), because each of the n trips costs two updates to
, and the second loop visits each of the L = 1001 locations at most once.change - Space: O(L), because
has one cell for every possible location, and all trips share those same 1001 cells however many trips there are.change
If locations had no small upper limit, an array with one cell per location would be too big. Sorting the pick-up and drop-off events works then, as in