Skip to content

Commit ccf3c95

Browse files
authored
Create 1353. Maximum Number of Events That Can Be Attended
1 parent 0184408 commit ccf3c95

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class DisjointSet {
2+
vector<int> parent;
3+
public:
4+
DisjointSet(int n) {
5+
parent.resize(n);
6+
for (int i = 0; i < n; i++) {
7+
parent[i] = i;
8+
}
9+
}
10+
11+
int findUPar(int node) {
12+
if (node == parent[node])
13+
return node;
14+
return parent[node] = findUPar(parent[node]);
15+
}
16+
17+
void mark(int day){
18+
parent[day]=day+1;
19+
}
20+
};
21+
22+
class Solution {
23+
public:
24+
int maxEvents(vector<vector<int>>& events) {
25+
vector<pair<int,int>> v;
26+
int maxi=0;
27+
for(int i=0;i<events.size();i++){
28+
v.push_back({events[i][1],events[i][0]});
29+
maxi=max(maxi,events[i][1]);
30+
}
31+
sort(v.begin(),v.end());
32+
int cnt=0;
33+
DisjointSet ds(maxi+2);
34+
for(int i=0;i<v.size();i++){
35+
int s=v[i].second;
36+
int e=v[i].first;
37+
int av=ds.findUPar(s);
38+
if(av<=e){
39+
cnt++;
40+
ds.mark(av);
41+
}
42+
}
43+
return cnt;
44+
}
45+
};

0 commit comments

Comments
 (0)