1353. Maximum Number of Events That Can Be Attended

1353. Maximum Number of Events That Can Be Attended

Description

You are given an array of events where events[i] = [startDay<sub>i</sub>, endDay<sub>i</sub>]. Every event i starts at startDay<sub>i</sub> and ends at endDay<sub>i</sub>.

You can attend an event i at any day d where startTime<sub>i</sub> <= d <= endTime<sub>i</sub>. You can only attend one event at any time d.

Return the maximum number of events you can attend.

Example 1:

1
2
3
4
5
6
7
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
Input: events= [[1,2],[2,3],[3,4],[1,2]]
Output: 4

Constraints:

  • 1 <= events.length <= 10^5
  • events[i].length == 2
  • 1 <= startDay<sub>i</sub> <= endDay<sub>i</sub> <= 10^5

Hints/Notes

  • 2025/03/29 Q2
  • priority queue
  • No solution from 0x3F or Leetcode

Solution

Language: C++

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
class Solution {
public:
int maxEvents(vector<vector<int>>& events) {
ranges::sort(events);
int start = events[0][0], n = events.size(), res = 0;
priority_queue<int, vector<int>, greater<int>> pq;
int idx = 0;
while (idx != n || !pq.empty()) {
if (pq.empty() && idx < n) {
start = events[idx][0];
}
while (idx < n && events[idx][0] <= start) {
pq.push(events[idx++][1]);
}
while (!pq.empty() && pq.top() < start) {
pq.pop();
}
if (!pq.empty()) {
start = start + 1;
pq.pop();
res++;
}
}
return res;
}
};