Skip to content
BytePatterns

Car Pooling Capacity

MediumIntervals#intervals#sweep-line~25m

Problem

A car drives east along a straight road and can never turn back. Each trip is (riders, start, end): that many people board at start and leave at end. Given a seat capacity, decide whether every trip can be served without ever exceeding it. Passengers leaving at a point free their seats before anyone boarding at that same point sits down.

Examples

Input:  trips = [(2, 1, 5), (3, 3, 7)], capacity = 4
Output: False
Why:    between 3 and 5 both groups are aboard, needing 5 seats
Input:  trips = [(2, 1, 5), (3, 5, 7)], capacity = 3
Output: True
Why:    the first group leaves at 5 exactly as the second boards
Input:  trips = [], capacity = 0
Output: True
Why:    edge case, no trips can never overflow

Hints

0 / 3

Stuck on the idea rather than the code? Meeting Rooms covers it.