Car Pooling

A car with capacity empty seats only drives east. You are given trips where trips[i] = [numPassengers_i, from_i, to_i]: that many passengers are picked up at location from_i and dropped off at location to_i (locations are kilometers east of the start). Return true if it is possible to pick up and drop off every passenger of every trip, and false otherwise.

30:00

Car Pooling
medium
Free written solution
Topics
Companies

A car with capacity empty seats only drives east. You are given trips where trips[i] = [numPassengers_i, from_i, to_i]: that many passengers are picked up at location from_i and dropped off at location to_i (locations are kilometers east of the start). Return true if it is possible to pick up and drop off every passenger of every trip, and false otherwise.

Example 1:
Input: {"trips":[[2,1,5],[3,3,7]],"capacity":4}
Output: false
Constraints:
  • 1≤trips.length≤10001 \leq \text{trips.length} \leq 1000

  • trips[i].length==3\text{trips}[i].\text{length} == 3

  • 1≤numPassengersi≤1001 \leq \text{numPassengers}_i \leq 100

  • 0≤fromi<toi≤10000 \leq \text{from}_i < \text{to}_i \leq 1000

  • 1≤capacity≤1051 \leq \text{capacity} \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"trips":[[2,1,5],[3,3,7]],"capacity":4}

Instead of tracking every passenger, record only where the load changes: +num where a group boards, -num where it gets off. The car holds 4.

23012345674capacity0passengers in the car

change[] (only non-zero locations)

all zeros

● boards (+)● gets off (-)■ in the car now- - capacity
Variables
VariableValue
capacity4
load0
stop-
DepthFunction Call
Stack empty
0/12