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 →A new event creates a triple booking exactly when it overlaps a time interval that is already covered by two accepted events. You do not need to remember which two events created that interval; you only need the interval where double coverage currently exists. If the new event touches any such interval, some portion would immediately have coverage three, so the booking must be rejected.
If the new event does not touch an existing double-booked interval, it is safe to accept. It may still overlap ordinary bookings one at a time. Each of those intersections becomes a new double-booked interval for future calls. The important order is therefore: test against double coverage, create new double coverage from single bookings, and only then store the new event.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) per book call | The rejection scan and the scan that creates new pairwise intersections each inspect every accepted booking or stored overlap; both lists have at most O(n^2) entries, but every stored overlap comes from an accepted pair and the direct implementation's first scan can therefore be O(n^2) in the worst case. With at most 1000 calls, this remains bounded by the input limit. |
| Space | O(n^2) extra | The output is the calendar state itself and is excluded from the working-space bound. The bookings list uses O(n) entries, while each pair of accepted events can contribute a distinct double-booked interval, so overlaps can grow to O(n^2) in the worst shape of input. |
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
class Solution {
private:
vector<pair<int, int>> bookings;
vector<pair<int, int>> overlaps;
public:
bool book(int start, int end) {
for (const auto& [s, e] : overlaps) {
if (max(s, start) < min(e, end)) {
return false;
}
}
for (const auto& [s, e] : bookings) {
int overlapStart = max(s, start);
int overlapEnd = min(e, end);
if (overlapStart < overlapEnd) {
overlaps.push_back({overlapStart, overlapEnd});
}
}
bookings.push_back({start, end});
return true;
}
};The two intersection tests have different jobs. The first loop checks whether the candidate enters a region that already has two covering events. The second loop runs only after that check succeeds, so every recorded intersection is safe to mark as double coverage. Keeping these phases separate also guarantees that a rejected call does not partially modify the calendar.
You can instead store a count change at every endpoint: add one at start and subtract one at end. For a candidate, temporarily add its two changes, sort the endpoint positions, and sweep from left to right while tracking active events. If the count ever reaches three, reject and remove the temporary changes; otherwise keep them. This is a valid line-sweep design, but it is not an asymptotic improvement here because each call still scans all stored endpoints.
#include <map>
using namespace std;
class Solution {
private:
map<int, int> changes;
public:
bool book(int start, int end) {
changes[start]++;
changes[end]--;
int active = 0;
for (const auto& [time, delta] : changes) {
active += delta;
if (active >= 3) {
changes[start]--;
changes[end]++;
return false;
}
}
return true;
}
};The sweep version uses O(n) endpoint entries and O(n log n) time per call when the ordered map maintains the endpoints. It is often easier to generalize when the allowed overlap count changes, because the condition is simply active >= limit. The interval-list version is more direct for this problem and avoids rescanning every coordinate boundary, so it is usually the clearer implementation under the given call limit.