You are given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi and ends at endDayi.
You can attend an event i at any day d where startTimei <= d <= endTimei. You can only attend one event at any time d.
Return the maximum number of events you can attend.
Example 1:
Input: events = [[1,2],[2,3],[3,4]] Output: 3 Explanation: You can attend all the three events. One way to attend them all is as shown. Attend the first event on day 1. Attend the second event on day 2. Attend the third event on day 3.
Example 2:
Input: events= [[1,2],[2,3],[3,4],[1,2]] Output: 4
Constraints:
1 <= events.length <= 105events[i].length == 21 <= startDayi <= endDayi <= 105Problem Overview: You are given an array where events[i] = [startDay, endDay]. Each event can be attended on any single day within its range. The goal is to attend the maximum number of events without attending more than one event per day.
Approach 1: Greedy with Sorting and Set (Time: O(n log n + D log D), Space: O(D))
This approach uses a greedy scheduling idea. First sort the events by their ending day so that events that expire earlier are considered first. Maintain a sorted set of available days (typically from 1 to maxDay). For each event, use a set operation such as lower_bound to find the earliest available day that is greater than or equal to its start day. If that day is within the event's end day, attend the event and remove the day from the set.
The key insight is simple: always occupy the earliest valid day for an event so later days remain available for other events. Sorting ensures events that expire sooner get priority. This method relies heavily on ordered set operations and works well when the day range is manageable.
Approach 2: Priority Queue (Min-Heap) Method (Time: O(n log n), Space: O(n))
This is the most common and scalable solution. Start by sorting events by their start day. Iterate through days in increasing order and maintain a min-heap that stores the end days of events that have already started but not yet been attended. When the current day reaches an event's start day, push its end day into the heap.
At each day, remove events from the heap whose end day is earlier than the current day because they are already expired. Then attend the event with the smallest end day by popping the heap. Choosing the event that ends earliest prevents losing opportunities for short intervals. Each event is inserted and removed from the heap at most once, giving an efficient O(n log n) runtime.
This approach combines greedy scheduling with a heap (priority queue) while relying on initial sorting. It handles large day ranges efficiently because it only processes relevant events rather than tracking every possible day explicitly.
Recommended for interviews: The priority queue solution is what most interviewers expect. It demonstrates understanding of greedy scheduling and efficient event selection using a min-heap. Mentioning the set-based greedy idea shows good intuition, but implementing the heap approach proves you can manage dynamic candidate events efficiently with O(n log n) complexity.
The idea is to sort the events by their end days. By doing this, you give preference to events that finish earliest, thus maximizing the number of events you can attend. You use a set to record which days are occupied. For each event, try to attend it on the earliest possible day starting from the event's start day.
The solution begins by sorting the events on their end days. We then iterate over the events and try to attend them at the earliest possible day that is still unoccupied. A boolean array is used to mark the days that have been occupied. This helps efficiently manage which days have been taken up by events, ensuring that no two events are attended on the same day.
Time Complexity: O(N log N), where N is the number of events, due to sorting.
Space Complexity: O(1), considering the constraint that the maximum day value does not exceed 100,000.
In this approach, we leverage a priority queue (implemented using a min-heap) to choose the next event to attend. By adding each event's end day to the heap, we can focus on attending the event that ends first among the available ones. We iterate over the days and use the priority queue to ensure the event attended finishes at the earliest possible time, freeing up subsequent days for potentially more events.
The C++ solution uses a priority queue to manage events by their end times. By popping the smallest element (earliest end time), the code guarantees that it tries to attend the event that leaves most days open for future events, allowing more to be attended overall.
Time Complexity: O(N log N), with N log N for sorting and N log N for priority queue operations.
Space Complexity: O(N) for the priority queue.
We use a hash table g to record the start and end times of each event. The key is the start time of the event, and the value is a list containing the end times of all events that start at that time. Two variables, l and r, are used to record the minimum start time and the maximum end time among all events.
For each time point s from l to r in increasing order, we perform the following steps:
s.s to the priority queue.In this way, we ensure that at each time point s, we always attend the event that ends the earliest, thus maximizing the number of events attended.
The time complexity is O(M times log n), and the space complexity is O(n), where M is the maximum end time and n is the number of events.
| Approach | Complexity |
|---|---|
| Approach 1: Greedy Approach with Sorting and Set | Time Complexity: O(N log N), where N is the number of events, due to sorting. |
| Approach 2: Priority Queue (Min-Heap) Method | Time Complexity: O(N log N), with N log N for sorting and N log N for priority queue operations. |
| Hash Table + Greedy + Priority Queue | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy with Sorting and Ordered Set | O(n log n + D log D) | O(D) | Useful when the day range is small and an ordered set of available days can be maintained efficiently. |
| Greedy with Min-Heap (Priority Queue) | O(n log n) | O(n) | Best general solution for interviews and large inputs. Dynamically tracks active events and always selects the one ending earliest. |
Leetcode 1353. Maximum Number of Events That Can Be Attended • Fraz • 34,231 views views
Watch 9 more video solutions →Practice Maximum Number of Events That Can Be Attended with our built-in code editor and test cases.
Practice on FleetCode