Car Pooling

Medium

Topics
ArraySortingHeapPrefix SumSimulation

There is a car with capacity empty seats, driving east and never turning around. You are given trips, where trips[i] = [numPassengers, from, to] means the i-th trip picks up numPassengers at kilometre from and drops them at kilometre to.

Return true if every trip can be made without ever exceeding capacity.

Example 1

17[1,5][3,7]
on board together at km 3
Each lane is one trip, drawn from pick-up to drop-off on the kilometre axis, so overlapping lanes are passengers sharing the car. The most on board at once is 5 against a capacity of 4, so the answer is false. A drop-off frees its seats before a pick-up at the same kilometre.
One lane per trip [numPassengers, from, to], positioned by from and to.
Input:  trips = [[2,1,5],[3,3,7]], capacity = 4
Output: false
Explanation: Between kilometre 3 and 5 the car would hold 5 passengers.

Example 2

17[1,5][3,7]
on board together at km 3
Input:  trips = [[2,1,5],[3,3,7]], capacity = 5
Output: true

Example 3

17[1,5][3,7]
Input:  trips = [[2,1,5],[3,5,7]], capacity = 3
Output: true
Explanation: The first group leaves at kilometre 5 exactly as the second boards.

Constraints

  • 1 <= trips.length <= 1000
  • trips[i].length == 3
  • 1 <= numPassengers <= 100
  • 0 <= from < to <= 1000
  • 1 <= capacity <= 10^5
Editorial — intuition, approach, complexity and pitfalls

Intuition

The car only moves forward, so the whole journey is one pass along the kilometre axis. What matters at any point is the number of passengers on board, and that only changes at a pick-up or a drop-off.

A drop-off at the same kilometre as a pick-up frees the seats first — the passengers are gone by the time the next group boards — which is the detail the third example turns on.

Approach

  1. Record the change in occupancy at each kilometre: +numPassengers at from and -numPassengers at to.
  2. Walk the kilometres in increasing order, keeping a running total of those changes.
  3. That running total is the number on board over the stretch beginning at that kilometre. If it ever exceeds capacity, answer false.
  4. Surviving the whole axis answers true. Processing drops before pick-ups at the same kilometre falls out naturally, because both land on the same bucket and sum before being compared.

Walkthrough

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

km:      1   3   5   7
delta:  +2  +3  -2  -3
onboard: 2   5 <- exceeds 4 -> false

Complexity

  • Time — O(n + K): one pass over the trips to record the changes, then one pass over the fixed kilometre range K.
  • Space — O(K): one bucket per kilometre. Sorting the events instead uses O(n).

Common Pitfalls

  • Treating a drop-off at kilometre x as happening after a pick-up at x wrongly rejects trips that hand over seats exactly on the boundary.
  • The capacity is a limit on passengers on board at one time, not on the total carried over the journey.

Full Solution — all languages

class Solution:
    def carPooling(self, matrix, target):
        deltas = {}
        for passengers, start, end in matrix:
            deltas[start] = deltas.get(start, 0) + passengers
            deltas[end] = deltas.get(end, 0) - passengers
        onboard = 0
        for stop in sorted(deltas):
            onboard += deltas[stop]
            if onboard > target:
                return False
        return True
SensAI
Beta
Tap the mic to stop voice mode