2Brute2Best
🔥0

Car Pooling

Medium·LeetCode #1094 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

A car drives in one direction along a road, starting at location 0. It has capacity empty seats and never turns back.

Each trip trips[i] = [numPassengers, from, to] means numPassengers people get in when the car reaches location from and get out when it reaches location to.

Return true if the car can do every trip without ever carrying more than capacity passengers at once, and false otherwise.

People getting out at a location leave before new people get in there. So passengers dropped at 5 never sit next to passengers picked up at 5.

Input

A list trips of [numPassengers, from, to] triples, and an integer capacity.

Output

true if all trips fit, false if the car is ever over capacity.

Example 1
Input: trips = [[2,1,5],[3,3,7]], capacity = 4
Output: false

Between locations 3 and 5, both groups are in the car: 2 + 3 = 5 people, but only 4 seats.

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

The busiest stretch has 5 people, which fits exactly.

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

The first group gets out at 5 before the second group gets in at 5, so there are never more than 3 people in the car.

Constraints
  • • 1 ≤ trips.length ≤ 1000
  • • 1 ≤ numPassengers ≤ 100
  • • 0 ≤ from < to ≤ 1000
  • • 1 ≤ capacity ≤ 10^5
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“get in at from, get out at to”
→ Each trip is an interval [from, to) carrying a weight p.
“never more than capacity at once”
→ Max over time of the active total → sweep, like Meeting Rooms II but adding p instead of 1.
“0 ≤ from < to ≤ 1000”
→ Small coordinates → a difference array replaces sorting.
03

Approach Complexity Ladder

Brute Force — add up every trip at every locationO(N · R)O(1)

For each location, sum the passengers of all trips covering it

Better — sort pick-up and drop-off eventsO(N log N)O(N)

Turn each trip into +p and −p events, sort by location, sweep a running load

Optimal — difference array over locationsO(N + R), R = 1001O(R)

+p at from, −p at to in an array of size 1001; prefix-sum it and check capacity

Brute Force Intuition

For every location x from 0 to 999, look at the stretch [x, x+1) of road. Add up p for every trip with from ≤ x < to. If any sum is more than capacity, return false.

Recognition rule for this sub-pattern:

How many things are active at the same time? Sweep events in time order, or keep a min-heap of end times.
Remember this · Intervals
Trigger
Input is [start, end] pairs; overlap, merge, insert, meeting rooms, free time, minimum removals.
Think
After sorting, do I only ever need to compare against the most recent interval?
Key question
Sort by start or by end — which one makes the greedy choice obvious?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.