Leetcode #1094: Car Pooling
In this guide, we solve Leetcode #1094 Car Pooling in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).
Quick Facts
- Difficulty: Medium
- Premium: No
- Tags: Array, Prefix Sum, Sorting, Simulation, Heap (Priority Queue)
Intuition
We need to repeatedly access the smallest or largest element as the input changes.
A heap provides fast insertions and removals while keeping order.
Approach
Push candidates into the heap as you scan, and pop when you need the best element.
Keep the heap size bounded if the problem requires a top-k structure.
Steps:
- Push candidates into a heap.
- Pop the best candidate when needed.
- Maintain heap size or invariants.
Example
Input: trips = [[2,1,5],[3,3,7]], capacity = 4
Output: false
Python Solution
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
mx = max(e[2] for e in trips)
d = [0] * (mx + 1)
for x, f, t in trips:
d[f] += x
d[t] -= x
return all(s <= capacity for s in accumulate(d))
Complexity
The time complexity is , and the space complexity is . The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.