Skip to content
Decoder

Car Pooling

Medium

Problem

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.

Example 1

Input
trips = [[2, 1, 5], [3, 3, 7]], capacity = 4
Output
false
Why
5 people between km 3 and 5

Example 2

Input
trips = [[2, 1, 5], [3, 3, 7]], capacity = 5
Output
true

Example 3

Input
trips = [[2, 1, 5], [3, 5, 7]], capacity = 3
Output
true
Why
the first group leaves at 5

Constraints

  • 1 <= trips.length <= 1000
  • 1 <= num_passengers <= 100
  • 0 <= from < to <= 1000
  • 1 <= capacity <= 10^5

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.