Maximum Number of Events That Can Be Attended
Master this topic with zero to advance depth.
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
You can attend all 3 events.
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
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
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.
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
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 |
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.