All problems
0284MediumArraySortingHeap (Priority Queue)SimulationPrefix Sum

One-Way Tram Capacity Check

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1094Car Pooling

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A heritage tram makes one run along a line of numbered stops, starting at stop 0 and always moving to higher-numbered stops. It never doubles back.

bookings[i] = [party, board, leave] is a reserved party of party people who get on at stop board and get off at stop leave, with leave always after board. The tram is built to hold seats people at any one moment.

At a stop, everyone getting off steps down before anyone gets on, so a party that leaves at a stop hands its space to a party boarding at that same stop.

Return true when the single run can carry every reserved party, and false when some stretch of the line would put more than seats people on board.

Examples

Example 1

Input
bookings = [[2, 1, 5], [3, 5, 7]], seats = 3
Output
true

The party of 2 rides from stop 1 and steps down at stop 5, where the party of 3 gets on, so the tram carries 2 people and then 3, never above 3.

Example 2

Input
bookings = [[2, 1, 5], [3, 3, 7]], seats = 4
Output
false

From stop 3 to stop 5 both parties are on board at once, which is 5 people in a tram built for 4.

Example 3

Input
bookings = [[4, 0, 2], [4, 2, 4], [4, 4, 6]], seats = 4
Output
true

Each party steps down at the very stop where the next one gets on, so the tram carries 4 people on each leg of the run.

Constraints

  • 1 <= bookings.length <= 1000
  • bookings[i].length == 3
  • 1 <= party_i <= 100
  • 0 <= board_i < leave_i <= 1000
  • 1 <= seats <= 10^5

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def tram_run_fits(bookings: list[list[int]], seats: int) -> bool:
Java
public boolean tramRunFits(int[][] bookings, int seats)
September 7
Apply