1353 Maximum Number of Events That Can Be Attended

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:

 1 [x _]
 2   [x _]
 3     [x _]
Day 1 2 3 4

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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
    def maxEvents(self, events: List[List[int]]) -> int:
        n = len(events)
        events.sort()
        heap = []   # heap of on-going events' end time
        res = 0
        d = 0
        i = 0   # index of sorted events

        while i < n or heap:
            # while there are no-yet stated or on-going events

            if not heap:
                # no on-going events, jump to the next event
                d = events[i][0]

            while i < n and events[i][0] <= d:
                # add started events' end time to heap
                heapq.heappush(heap, events[i][1])
                i += 1

            heapq.heappop(heap)
            res += 1
            d += 1
            while heap and heap[0] < d:
                # remove ended events from heap
                heapq.heappop(heap)

        return res