1353. Maximum Number of Events That Can Be Attended
Medium ↗ Open on LeetCode Source: Misc
Quick review
- Pattern
- Greedy
- Time
- —
- Space
- —
Standard Solution (C++)
SummaryGiven a list of events each spanning a start and end day (inclusive), find the maximum number of events you can attend by attending at most one event per day and choosing any single day within an event's window.
Approach
- Sort events by start day, then simulate day by day from day 1 up to the maximum end day.
- On each day, push all newly-started events' end days into a min-heap (earliest-ending on top).
- Evict any events from the heap whose end day has already passed, since they can no longer be attended.
- Greedily attend the event that expires soonest (pop the heap top): this maximizes future flexibility by preserving events with later deadlines.
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxEvents(vector<vector<int>>& events) {
// Sort by start day so we can sweep left-to-right
sort(events.begin(), events.end(),
[](const auto& a, const auto& b){ return a[0] < b[0]; });
// Min-heap keyed on end day: always attend the soonest-expiring event
priority_queue<int, vector<int>, greater<int>> pq;
int n = events.size();
int idx = 0; // pointer into sorted events
int attended = 0;
// Find the last possible day we ever need to simulate
int maxDay = 0;
for (const auto& e : events) maxDay = max(maxDay, e[1]);
for (int day = 1; day <= maxDay; ++day) {
// Enqueue every event that starts today
while (idx < n && events[idx][0] == day) {
pq.push(events[idx][1]);
++idx;
}
// Drop events that have already expired (end day < today)
while (!pq.empty() && pq.top() < day) {
pq.pop();
}
// Attend the event ending soonest — greedy choice
if (!pq.empty()) {
pq.pop();
++attended;
}
}
return attended;
}
};
Time O(D + n log n), where D is the maximum end day and n is the number of events Space O(n)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
class Solution {
public:
int maxEvents(vector<vector<int>>& events) {
int len=events.size();
std::sort(events.begin(), events.end(), [](const auto& a, const auto& b){return a[0]<b[0];});
std::priority_queue<int, std::vector<int>, greater<int>> pq;
int idx=0; // event idx
int res=0;
// find max day
int maxDay=0;
for(int i=0; i<len; ++i){
maxDay=std::max(maxDay, events[i][1]);
}
for(int i=1; i<=maxDay; ++i){
while(idx<len && events[idx][0]==i){
pq.push(events[idx][1]);
++idx;
}
if(!pq.empty()){
pq.pop();
++res;
}
while(!pq.empty() && pq.top()<=i){
pq.pop();
}
}
return res;
}
};
// [2,5],[2,3],[3,4]
// idx: 0~2
// maxDay
// for 0~maxDay
// add events start with the current day to the min_heap (store end day)
// check expire from the top
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.