Opening the reading…
Opening the reading…
PREFIX SUM › LINE SWEEP
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →The car's passenger count stays constant while it travels between two event locations. It changes only when someone is picked up or dropped off, so the full trip can be reduced to two updates: add numPassengers at from, and subtract numPassengers at to. If the running total ever exceeds capacity, that point on the route is impossible.
Sort all updates by location and process them from west to east. At the same location, a dropoff must happen before a pickup: passengers who reach to leave the car before passengers waiting at that same kilometer enter it. The running total after each update then represents a valid passenger count throughout the journey, and checking it against capacity is sufficient.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(m log m) | Each of the m trips creates exactly two events, giving k = 2m events. Sorting costs O(k log k), and the final sweep examines each event once, so the sorting term dominates and becomes O(m log m). |
| Space | O(m) extra | The event list stores exactly 2m endpoint changes, while sorting uses at most O(m) additional working space depending on the implementation. The returned boolean is not output storage that needs to be counted, and there is no larger input-shaped structure. |
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
bool carPooling(vector<vector<int>>& trips, int capacity) {
vector<pair<int, int>> events;
for (const vector<int>& trip : trips) {
int passengers = trip[0];
int from = trip[1];
int to = trip[2];
events.emplace_back(from, passengers);
events.emplace_back(to, -passengers);
}
sort(events.begin(), events.end(), [](const pair<int, int>& a,
const pair<int, int>& b) {
if (a.first != b.first) {
return a.first < b.first;
}
return a.second < b.second;
});
int cur = 0;
for (const pair<int, int>& event : events) {
cur += event.second;
if (cur > capacity) {
return false;
}
}
return true;
}
};The event encoding makes the comparator carry an important correctness rule. A pickup is positive and a dropoff is negative, so ascending event values automatically places every dropoff before every pickup at the same location. The check belongs after cur is updated: checking before the addition would skip the exact event that could make the car over capacity.
The locations are bounded from 0 through 1000, so sorting is not necessary. Store each pickup and dropoff as a change in a difference array, then scan locations in increasing order. At one location, adding all changes together naturally combines dropoffs and pickups, so the final count after that location reflects the seats occupied while leaving it.
#include <vector>
using namespace std;
class Solution {
public:
bool carPooling(vector<vector<int>>& trips, int capacity) {
const int maxLocation = 1000;
vector<int> change(maxLocation + 1, 0);
for (const vector<int>& trip : trips) {
int passengers = trip[0];
int from = trip[1];
int to = trip[2];
change[from] += passengers;
change[to] -= passengers;
}
int cur = 0;
for (int location = 0; location <= maxLocation; ++location) {
cur += change[location];
if (cur > capacity) {
return false;
}
}
return true;
}
};This is an optimization made possible by the coordinate bound, not a fundamentally different argument. If U is the largest possible location, it runs in O(m + U) time and uses O(U) extra space instead of sorting O(m) events. It is attractive here because U is only 1000; with large or unbounded locations, the sorted event version avoids allocating an unnecessarily large empty range.