[LeetCode] Maximum Number of Events That Can Be Attended

1353. Maximum Number of Events That Can Be Attended

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. Notice that you can only attend one event at any time d.

Return the maximum number of events you can attend.

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
30
class Solution {
public:
int maxEvents(vector<vector<int>>& events) {
priority_queue<int> pq;
int res = 0;
int left = events.size();
sort(events.begin(), events.end());
auto event = events.begin();

for(int i = 1; left; i++) {
while(event != events.end() && event->front() <= i) {
pq.push(-event->back());
event++;
}

if(pq.empty())
continue;

pq.pop();
++res;
--left;
while(!pq.empty() && -pq.top() == i) {
pq.pop();
--left;
}
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/01/30/PS/LeetCode/maximum-number-of-events-that-can-be-attended/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.