Can a Coach Seat Every Booking Along the Route
A train coach has a fixed seat capacity. Bookings each board at one station and alight at another along a linear route. Decide whether the coach can honour every booking without ever exceeding capacity.
Problem
Given capacity seats and a list bookings where bookings[i] = [numPassengers, from, to] (passengers board at station from and leave at station to, 0-indexed stations along a line), return true if it is possible to carry all passengers without the onboard count exceeding capacity at any point, else false.
Input
A list bookings of [numPassengers, from, to] and an integer capacity.
Output
A boolean: whether all bookings fit within capacity.
Constraints
- 1 <= bookings.length <= 1000
- 1 <= numPassengers <= 100, 0 <= from < to <= 1000
- 1 <= capacity <= 10^5
Examples
Example 1
Input
bookings = [[2,1,5],[3,3,7]], capacity = 4
Output
false
Between stations 3 and 5 the coach holds 5 passengers, over capacity 4.
Example 2
Input
bookings = [[2,1,5],[3,5,7]], capacity = 4
Output
true
The first booking alights at 5 exactly as the second boards.