Car Pooling
A car with a fixed capacity drives east and cannot turn around. Given trips where each trip is [numPassengers, from, to], meaning numPassengers board at location from and leave at location to, return true if it is possible to complete all trips without the number of passengers on board ever exceeding capacity, and false otherwise.
Open official problem prompt ↗Decide whether the passenger load ever exceeds capacity at any point along the one-way route.
Like tracking people in a room with a clicker at the door: +1 when someone enters, -1 when they leave. The running clicker value is the crowd size at any moment, and you never recount everyone already inside.
- Input
- trips = [[2,1,5],[3,3,7]], capacity = 4
- Output
- false
- Why
- Between locations 3 and 5 both groups overlap: 2 + 3 = 5 passengers, which exceeds capacity 4
1 <= trips.length <= 10001 <= numPassengers_i <= 1000 <= from_i < to_i <= 10001 <= capacity <= 10^5