Skip to the code
cf.
+25 XP

#56 Can the Shuttle Carry Everyone

medium

A conference shuttle drives one way past stops numbered 0 to 1000 and holds at most capacity people. Each ride request [people, from, to] boards people at stop from and drops them at stop to.

At any stop, people getting off leave before new people get on. Write canCarryEveryone(trips, capacity) that returns true if every request fits without ever going over capacity.

Example 1

Input: trips = [[3,0,4],[2,2,6],[4,4,7]], capacity = 5
Output: false

At stop 4, three people leave and four board, so 6 people ride with 5 seats.

Example 2

Input: trips = [[3,0,4],[2,2,6],[4,4,7]], capacity = 6
Output: true

The busiest moment has 6 riders, which fits.

Constraints

  • 0 <= trips.length <= 1000
  • 1 <= people <= 100
  • 0 <= from < to <= 1000
  • 1 <= capacity <= 10^5
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).