MediumEditorial · 7 minGenerated by the editor · Oct 25
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.
Intuitionwhy the earliest deadline is the safest choice
On any day, several events may be available, but you can attend only one. If you attend an event that ends later while another available event ends sooner, you may lose the only chance to attend the sooner event. Choosing the event with the smallest end day protects the events that are most urgent and leaves later-ending events available for future days.
Process days from left to right. Add every event that has started, discard every event that has already ended, and attend the available event with the earliest end day. A min-heap stores those end days, so the greedy choice is always available in logarithmic time. Sorting by start day lets you add each event exactly once as the day advances.
Approach
1Sort the events by start day, using the end day as the tie-breaker, so a single index can introduce events in chronological order without scanning the input repeatedly.
2Create a min-heap of end days and an index at the first event. The heap represents exactly the events that have started but have not yet been attended or expired; without this active set, the greedy choice would require searching all events each day.
3For each day from 1 through the latest possible day, push every event whose start day equals the current day. The while loop is necessary because several events can start on the same day, and skipping one would remove a valid choice from consideration.
4Remove heap entries whose end day is less than the current day. Those events can no longer be attended, and leaving one at the top could make the algorithm choose an impossible event instead of a valid one.
5If the heap is nonempty, pop its smallest end day and increment the answer. This assigns the current day to the most urgent available event, while one pop enforces the rule that no more than one event is attended that day.
6Return the count after all days have been processed. Every event is inserted once and either attended or discarded, so no second pass over the event list is needed.
Complexitythe heap pays for one decision per event
MEASURE
BOUND
WHY
Time
O(n log n + D)
Sorting costs O(n log n). Each event enters the heap once and leaves it once, and each heap operation costs O(log n); the day loop contributes O(D), including days with no new event.
Space
O(n) extra
The sorted input is handled in place apart from the heap, and the heap can contain all n events when their intervals overlap. The returned count is required output and is excluded from extra space; the worst shape is n simultaneously active events.
Here n is the number of events and D is the largest end day considered by the day sweep; in this problem D is at most 100000.
Annotated solutionC++ · sorted sweep with a min-heap
CPPSort by start day, sweep through days, and attend the active event with the smallest end day.
#include <algorithm>
#include <functional>
#include <queue>
#include <vector>
using namespace std;
class Solution {
public:
int maxEvents(vector<vector<int>>& events) {
sort(events.begin(), events.end());
priority_queue<int, vector<int>, greater<int>> endDays;
int n = static_cast<int>(events.size());
int index = 0;
int attended = 0;
for (int day = 1; day <= 100000; ++day) {
while (index < n && events[index][0] == day) {
endDays.push(events[index][1]);
++index;
}
while (!endDays.empty() && endDays.top() < day) {
endDays.pop();
}
if (!endDays.empty()) {
endDays.pop();
++attended;
}
}
return attended;
}
};
The two heap operations express the whole greedy rule. First remove every deadline that is already behind the current day; then remove the smallest remaining deadline to attend it today. The order matters: selecting before removing expired events can consume a day without attending anything, even though a valid event is waiting underneath.
Common mistakesthe wrong line can still pass simple schedules