Car Pooling - Problem
There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).
You are given the integer capacity and an array trips where trips[i] = [numPassengersi, fromi, toi] indicates that the ith trip has numPassengersi passengers and the locations to pick them up and drop them off are fromi and toi respectively.
The locations are given as the number of kilometers due east from the car's initial location.
Return true if it is possible to pick up and drop off all passengers for all the given trips, or false otherwise.
Input & Output
Example 1 — Exceeds Capacity
$
Input:
trips = [[2,1,3],[3,2,4]], capacity = 4
›
Output:
false
💡 Note:
At kilometer 2, we have 2 passengers from trip 1 and 3 passengers from trip 2, totaling 5 passengers which exceeds capacity 4.
Example 2 — Within Capacity
$
Input:
trips = [[2,1,3],[3,2,4]], capacity = 5
›
Output:
true
💡 Note:
Maximum passengers at any point is 5 (at km 2), which equals the capacity, so it's possible.
Example 3 — Sequential Trips
$
Input:
trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11
›
Output:
true
💡 Note:
At km 3-7: 11 passengers (3+8), km 7-9: 11 passengers (3+8). Maximum is 11 which equals capacity 11.
Constraints
- 1 ≤ trips.length ≤ 1000
- trips[i].length == 3
- 1 ≤ numPassengersi ≤ 100
- 0 ≤ fromi < toi ≤ 1000
- 1 ≤ capacity ≤ 100000
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code