-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumNumberofEventsThatCanBeAttended.cpp
More file actions
49 lines (47 loc) · 1.34 KB
/
maximumNumberofEventsThatCanBeAttended.cpp
File metadata and controls
49 lines (47 loc) · 1.34 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Source: https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/
// Author: Miao Zhang
// Date: 2021-04-26
class Solution {
public:
int maxEvents(vector<vector<int>>& events) {
sort(begin(events), end(events), [](const auto& a, const auto& b) {
return a[1] < b[1];
});
int res = 0;
int seen[100001] = {0};
for (auto& e: events) {
for (int i = e[0]; i <= e[1]; i++) {
if (seen[i]) continue;
seen[i]++;
res++;
break;
}
}
return res;
}
};
class Solution {
public:
int maxEvents(vector<vector<int>>& events) {
sort(begin(events), end(events), [] (const auto& a, const auto& b) {
return a[1] < b[1];
});
int mind = INT_MAX;
int maxd = INT_MIN;
for (const auto& e: events) {
mind = min(mind, e[0]);
maxd = max(maxd, e[1]);
}
vector<int> days(maxd - mind + 1);
iota(begin(days), end(days), mind);
set<int> s(begin(days), end(days));
int res = 0;
for (const auto& e: events) {
auto it = s.lower_bound(e[0]);
if (it == end(s) || *it > e[1]) continue;
s.erase(it);
res++;
}
return res;
}
};