Arrays and Hashing Difference arrays
594. Car Pooling
A car drives east along a straight road and never turns back. Stops on the road are whole numbers: stop x
is x kilometres from the start. The car has seats for capacity passengers.
You are given an array trips, where trips[i] = [passengers, pickup, dropoff] means that a group of
passengers people gets in at stop pickup and gets out at stop dropoff. Return true if the car can
make every trip without ever carrying more than capacity passengers, and false otherwise.
A group is on board from its pickup stop up to its drop-off stop, but not at the drop-off stop itself. So when one group gets out at a stop where another gets in, the first group's seats are free in time.
Example 1
Input: trips = [[3, 1, 5], [2, 4, 8], [4, 5, 9]]
capacity = 6
trueExplanation: From stop 4 the car carries 3 + 2 = 5 passengers. At stop 5 the first group gets out and the third gets in, so the car carries 2 + 4 = 6. That is exactly the capacity, and it never carries more.
Example 2
Input: trips = [[2, 0, 3], [3, 1, 4], [2, 2, 6]]
capacity = 6
falseExplanation: From stop 2 to stop 3 all three groups are on board, 2 + 3 + 2 = 7 passengers.
Example 3
Input: trips = [[4, 0, 100000], [1, 50000, 50001]]
capacity = 4
falseExplanation: The second passenger rides for just one stop, but the first group is still on board then, so the car carries 5.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ trips.length ≤ 105trips[i].length = 31 ≤ passengers ≤ 1040 ≤ pickup < dropoff ≤ 1051 ≤ capacity ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.