Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-154

Car Pool Feasibility

Problem

Given trips as [passengers, start, end] and a vehicle capacity, return True if all passengers can be picked up and dropped off without ever exceeding the capacity.

Build a pickup event and a drop-off event for each trip, sort by location with drop-offs before pickups at the same point, and track the running passenger count. An empty trip list is feasible.

Hint

Negative deltas sort before positive ones at the same location, so drop-offs free space first.

Test cases

case 1
in: [[[2,1,5],[3,3,7]],4]
expected: false
case 2
in: [[[2,1,5],[3,3,7]],5]
expected: true
case 3
in: [[[2,1,5],[3,5,7]],3]
expected: true
case 4
in: [[],4]
expected: true
case 5
in: [[[2,0,2]],1]
expected: false
Starter codePython
def car_pool_feasibility(trips, capacity):
    # Your code here
    pass

5 test cases · run your code against them in the editor