Car Pooling
MediumHeap / Priority Queue
Decode it first. Clues, constraints, pattern and plan before any code.A car with capacity empty seats drives east only (it never turns around). trips[i] = [num_passengers, from, to] means a group of num_passengers gets in at kilometer from and gets out at kilometer to.
Return true if the car can complete every trip without ever exceeding capacity, and false otherwise. Passengers getting out at a kilometer free their seats before anyone gets in there.
trips = [[2, 1, 5], [3, 3, 7]], capacity = 4 → false (5 people between km 3 and 5)
trips = [[2, 1, 5], [3, 3, 7]], capacity = 5 → true
trips = [[2, 1, 5], [3, 5, 7]], capacity = 3 → true (the first group leaves at 5)Constraints
1 <= trips.length <= 10001 <= num_passengers <= 1000 <= from < to <= 10001 <= capacity <= 10^5