Post

1478 Maximum Number Of Events That Can Be Attended

1478 Maximum Number Of Events That Can Be Attended

Maximum Number of Events That Can Be Attended image

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:

image

1
2
3
4
5
6
7
8
9
**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:

1
2
3
4
**Input:** events= [[1,2],[2,3],[3,4],[1,2]]
**Output:** 4

 

Constraints:

1
2
3
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

class Solution:
    def maxEvents(self, events: List[List[int]]) -> int:
        n = len(events)
        max_day = max(event[1] for event in events)
        events.sort()
        pq = []
        ans, j = 0, 0
        for i in range(1, max_day + 1):
            while j < n and events[j][0] <= i:
                heapq.heappush(pq, events[j][1])
                j += 1
            while pq and pq[0] < i:
                heapq.heappop(pq)
            if pq:
                heapq.heappop(pq)
                ans += 1

        return ans



This post is licensed under CC BY 4.0 by the author.