File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments