-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
MaximumNumberOfEventsThatCanBeAttended.cpp
150 lines (131 loc) · 3.85 KB
/
MaximumNumberOfEventsThatCanBeAttended.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Source : https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/
// Author : Hao Chen
// Date : 2021-02-13
/*****************************************************************************************************
*
* 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.
*
* Example 1:
*
* 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:
*
* Input: events= [[1,2],[2,3],[3,4],[1,2]]
* Output: 4
*
* Example 3:
*
* Input: events = [[1,4],[4,4],[2,2],[3,4],[1,1]]
* Output: 4
*
* Example 4:
*
* Input: events = [[1,100000]]
* Output: 1
*
* Example 5:
*
* Input: events = [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7]]
* Output: 7
*
* Constraints:
*
* 1 <= events.length <= 105
* events[i].length == 2
* 1 <= startDayi <= endDayi <= 105
******************************************************************************************************/
class Solution {
private:
static const bool comp_start(vector<int>& x, vector<int>& y) {
if ( x[0] != y[0] ) return x[0] < y[0];
return x[1] < y[1];
}
static const bool comp_end(vector<int>& x, vector<int>& y) {
if ( x[1] != y[1] ) return x[1] < y[1];
return x[0] < y[0];
}
//union find
int find(int x, vector<int>& f) {
if(f[x] == x) {
return x;
} else {
return f[x] = find(f[x], f);
}
}
void print(vector<vector<int>>& events){
cout << "[" ;
for(auto e: events) {
cout << "[" << e[0] << "," << e[1] << "]," ;
}
cout << "]" << endl;
}
public:
int maxEvents(vector<vector<int>>& events) {
return maxEvents_priority_queue(events);//332ms
return maxEvents_union_find(events); // 336ms
}
int maxEvents_priority_queue(vector<vector<int>>& events) {
std::sort(events.begin(), events.end(), comp_start);
//print(events);
int start = events[0][0];
int end = 0;
for(auto& e:events){
end = max(end, e[1]);
}
int result = 0;
int i = 0;
priority_queue<int, vector<int>, greater<int>> pq;
for (int day = start; day <= end; day++) {
while (i<events.size() && events[i][0]==day) {
pq.push(events[i][1]); //push the ending day
i++;
}
//remove out-of-date event
while(!pq.empty() && pq.top() < day) {
pq.pop();
}
//if there still has event, then choose current day.
if (!pq.empty()){
pq.pop();
result++;
}
//no more date need to process
if (pq.empty() && i >= events.size()) break;
}
return result;
}
int maxEvents_union_find(vector<vector<int>>& events) {
std::sort(events.begin(), events.end(), comp_end);
int end = events[events.size()-1][1];
int start = end;
for(auto& e:events){
start = min(start, e[0]);
}
vector<int> dict;
for (int i=0; i<=end-start+1; i++){
dict.push_back(i);
}
int result = 0;
for(auto& e : events) {
int x = find(e[0]-start, dict);
if ( x <= e[1]-start ){
result++;
dict[x] = find(x+1, dict);
}
}
return result;
}
};