Maximum Number of Events That Can Be Attended
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Greedy.
Maximum Number of Events That Can Be Attended
You are given an array of
events where events[i] = [startDay_i, endDay_i]. Every event i starts at startDay_i and ends at endDay_i. You can attend an event i at any day d where startDay_i <= d <= endDay_i.You can only attend one event at any day
d.Return the maximum number of events you can attend.
Visual Representation
Events: [[1, 2], [1, 2], [3, 3]]
Day 1: Attend [1, 2] (Event 1)
Day 2: Attend [1, 2] (Event 2)
Day 3: Attend [3, 3] (Event 3)
Result: 3Examples
Input: events = [[1,2],[1,2],[3,3]]
Output: 3
You can attend all 3 events.
Approach 1
Level I: Brute Force (Recursive Scheduling)
Intuition
Try every possible combination of events you could attend. For each subset of events, check if there exists a valid day for each event such that no two events are attended on the same day and each event is attended within its [start, end] range.
Thought Process
- Use recursion to try and attend or skip each event.
- When attending an event, try every possible day in its range that hasn't been used yet.
- Keep track of the maximum number of events attended.
- This approach is or worse if checking all day combinations.
Pattern: Backtracking / Search
⏱ O(2^N * MaxDay) - Exponential.💾 O(N + MaxDay) for recursion and tracking used days.
Approach 2
Level II: Greedy Simulation (O(N^2))
Intuition
For each day, we scan all available events that have not been attended yet and pick the one that ends the earliest. This is a direct simulation of the optimal strategy without a Priority Queue.
Thought Process
- Sort events by
startDay. - Maintain a
usedboolean array. - For each day
dfromminStarttomaxEnd:- Find an unvisited event
isuch thatevents[i].start <= dandevents[i].end >= dand it has the minimumendtime among all such candidates. - If found, mark
used[i] = trueand incrementcount.
- Find an unvisited event
Pattern: Greedy Scanning
⏱ O(MaxDay * N).💾 O(N) for used array.
Detailed Dry Run
Events: [[1,2],[1,2],[3,3]]
Day 1: Both [1,2] available. Pick one. Mark used.
Day 2: Only other [1,2] available. Pick it. Mark used.
Day 3: [3,3] available. Pick it. Mark used.
Total: 3.
Approach 3
Level III: Optimal Greedy (Min-Heap + Sorting)
Intuition
On any given day, if multiple events are available, we should prioritize the one that ends the earliest. This is because an event with a later end day can potentially be attended on a future day.
Thought Process
- Sort events by
startDay. - Use a Min-Heap to store the end days of currently available events.
- Iterate through days:
- Add all events starting today to the heap.
- Remove events from the heap that have already ended.
- If the heap is not empty, attend the event that ends the earliest (pop from heap) and increment count.
Pattern: Greedy / Priority Queue
⏱ O(N log N + MaxDay log N) - Sorting and heap operations.💾 O(N) for the heap.
Detailed Dry Run
events = [[1,2], [1,2], [3,3]]
| Day | Add to Heap | Expired | Attend | Heap |
|---|---|---|---|---|
| 1 | [2, 2] | - | Attend(2) | [2] |
| 2 | - | - | Attend(2) | [] |
| 3 | [3] | - | Attend(3) | [] |
| Result: 3 |
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.