Maximum Number of Events
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Heap / Priority Queue.
Maximum Number of Events
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 per day.Return the maximum number of events you can attend.
Visual Representation
events = [[1,2],[2,3],[3,4]]
Day 1: [1,2] available. Attend it.
Day 2: [2,3] available. Attend it.
Day 3: [3,4] available. Attend it.
Total = 3
Key insight: Each day, attend the event that ends SOONEST (greedy).
This leaves later-ending events available for future days.Examples
Input: events = [[1,2],[2,3],[3,4]]
Output: 3
Input: events = [[1,4],[4,4],[2,2],[3,4],[1,1]]
Output: 4
Approach 1
Level I: Greedy - Sort by Start, Attend First Available
Intuition
Sort events by start day. On each day from 1 to maxDay, try to attend the first available event (one that hasn't been attended and whose start day <= current day). This is a greedy approach that ensures we don't miss starting opportunities.
⏱ O(maxDay * N) where maxDay is the maximum end day and N is the number of events💾 O(N) for the used set
Detailed Dry Run
events = [[1,2],[2,3],[3,4]] sorted by start
Day 1: Available (not attended, start<=1, end>=1): [1,2]. Attend [1,2]. Count=1.
Day 2: Available: [2,3]. Attend [2,3]. Count=2.
Day 3: Available: [3,4]. Attend [3,4]. Count=3.
Return 3.
Approach 2
Level II: Sort by Start + Daily Min-End Search
Intuition
To improve on the basic search, we sort events by their start day. Each day, we identify all events that have already started and haven't been attended. Among these available events, we pick the one that ends the soonest (earliest end day). This ensures we use up events that would expire early, leaving more flexibility for the future.
⏱ O(maxDay * N) where maxDay is the duration of events and N is number of events💾 O(N) for the used tracker
Detailed Dry Run
events = [[1,4],[1,1],[2,2]] sorted.
Day 1: Available: [1,4], [1,1]. Ends are 4, 1. Min end is 1. Attend [1,1]. Count=1.
Day 2: Available: [1,4], [2,2]. Ends are 4, 2. Min end is 2. Attend [2,2]. Count=2.
Day 3: Available: [1,4]. Attend [1,4]. Count=3.
Max = 3.
Approach 3
Level III: Sort by Start + Min-Heap of End Days (Optimal)
Intuition
Greedy insight: on each day, among all available events (those that have already started), we should attend the one that ends the soonest. If we sort events by start day and use a Min-Heap of end days, we can efficiently:
- Iterate over each day from 1 to maxDay.
- Add all events that start on this day to the Min-Heap.
- Remove all expired events (heap top end < today).
- If the heap isn't empty, attend the event ending soonest (pop from heap), increment count.
⏱ O(N log N) for sorting and heap operations. Each event is pushed and popped at most once.💾 O(N) for the Min-Heap
Detailed Dry Run
events = [[1,2],[2,3],[3,4]] sorted by start
ptr=0
Day 1: Add events starting on 1: push end=2. Heap=[2]. Pop 2 (attend). Count=1.
Day 2: Add events starting on 2: push end=3. Heap=[3]. Pop 3 (attend). Count=2.
Day 3: Add events starting on 3: push end=4. Heap=[4]. Pop 4 (attend). Count=3.
Return 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.