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 <= 105
events[i].length == 2
1 <= startDayi <= endDayi <= 105
This problem asks to find the maximum number of events that can be attended, given a list of events with start and end days. We can only attend one event per day.
The optimal approach involves a combination of sorting, greedy selection, and a priority queue (min-heap).
1. Sorting:
First, we sort the events by their start days. This allows us to process events in chronological order.
2. Greedy Selection and Priority Queue:
We use a min-heap (priority queue) to store the end days of events that are currently "available" to attend. "Available" means the event's start day is less than or equal to the current day, and the event has not yet ended.
We iterate through the days (from the minimum start day to the maximum end day). For each day:
Time Complexity Analysis:
Space Complexity Analysis:
Code Implementation (Python):
import heapq
def maxEvents(events):
events.sort() # Sort events by start day
pq = [] # Min-heap to store end days of available events
count = 0
i = 0
day = 1 # Current day
while i < len(events) or pq:
while i < len(events) and events[i][0] == day:
heapq.heappush(pq, events[i][1])
i += 1
while pq and pq[0] < day:
heapq.heappop(pq)
if pq:
heapq.heappop(pq)
count += 1
day += 1
return count
The code in other languages (Java, C++, Go) follows a very similar structure, using their respective implementations of priority queues. The core algorithm remains the same.