Skip to content

Commit b7adf90

Browse files
committed
New Problem Solution - "Maximum Number of Events That Can Be Attended"
1 parent b74969f commit b7adf90

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ LeetCode
3030
|1464|[Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/) | [C++](./algorithms/cpp/maximumProductOfTwoElementsInAnArray/MaximumProductOfTwoElementsInAnArray.cpp)|Easy|
3131
|1460|[Make Two Arrays Equal by Reversing Sub-arrays](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/) | [C++](./algorithms/cpp/twoArraysEqualByReversingSubArrays/MakeTwoArraysEqualByReversingSubArrays.cpp)|Easy|
3232
|1376|[Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/) | [C++](./algorithms/cpp/timeNeededToInformAllEmployees/TimeNeededToInformAllEmployees.cpp)|Medium|
33+
|1353|[Maximum Number of Events That Can Be Attended](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/) | [C++](./algorithms/cpp/maximumNumberOfEventsThatCanBeAttended/MaximumNumberOfEventsThatCanBeAttended.cpp)|Medium|
3334
|1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/) | [C++](./algorithms/cpp/filterRestaurantsByVeganFriendlyPriceAndDistance/FilterRestaurantsByVeganFriendlyPriceAndDistance.cpp)|Medium|
3435
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C++](./algorithms/cpp/uniqueNumberOfOccurrences/Unique-Number-of-Occurrences.cpp)|Easy|
3536
|1170|[Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/) | [C++](./algorithms/cpp/compareStringsByFrequencyOfTheSmallestCharacter/CompareStringsByFrequencyOfTheSmallestCharacter.cpp)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Source : https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/
2+
// Author : Hao Chen
3+
// Date : 2021-02-13
4+
5+
/*****************************************************************************************************
6+
*
7+
* Given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi
8+
* and ends at endDayi.
9+
*
10+
* You can attend an event i at any day d where startTimei <= d <= endTimei. Notice that you can only
11+
* attend one event at any time d.
12+
*
13+
* Return the maximum number of events you can attend.
14+
*
15+
* Example 1:
16+
*
17+
* Input: events = [[1,2],[2,3],[3,4]]
18+
* Output: 3
19+
* Explanation: You can attend all the three events.
20+
* One way to attend them all is as shown.
21+
* Attend the first event on day 1.
22+
* Attend the second event on day 2.
23+
* Attend the third event on day 3.
24+
*
25+
* Example 2:
26+
*
27+
* Input: events= [[1,2],[2,3],[3,4],[1,2]]
28+
* Output: 4
29+
*
30+
* Example 3:
31+
*
32+
* Input: events = [[1,4],[4,4],[2,2],[3,4],[1,1]]
33+
* Output: 4
34+
*
35+
* Example 4:
36+
*
37+
* Input: events = [[1,100000]]
38+
* Output: 1
39+
*
40+
* Example 5:
41+
*
42+
* Input: events = [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7]]
43+
* Output: 7
44+
*
45+
* Constraints:
46+
*
47+
* 1 <= events.length <= 105
48+
* events[i].length == 2
49+
* 1 <= startDayi <= endDayi <= 105
50+
******************************************************************************************************/
51+
52+
53+
class Solution {
54+
private:
55+
static const bool comp_start(vector<int>& x, vector<int>& y) {
56+
if ( x[0] != y[0] ) return x[0] < y[0];
57+
return x[1] < y[1];
58+
}
59+
static const bool comp_end(vector<int>& x, vector<int>& y) {
60+
if ( x[1] != y[1] ) return x[1] < y[1];
61+
return x[0] < y[0];
62+
}
63+
64+
65+
//union find
66+
int find(int x, vector<int>& f) {
67+
if(f[x] == x) {
68+
return x;
69+
} else {
70+
return f[x] = find(f[x], f);
71+
}
72+
}
73+
void print(vector<vector<int>>& events){
74+
cout << "[" ;
75+
for(auto e: events) {
76+
cout << "[" << e[0] << "," << e[1] << "]," ;
77+
}
78+
cout << "]" << endl;
79+
}
80+
public:
81+
int maxEvents(vector<vector<int>>& events) {
82+
return maxEvents_priority_queue(events);//332ms
83+
return maxEvents_union_find(events); // 336ms
84+
}
85+
86+
int maxEvents_priority_queue(vector<vector<int>>& events) {
87+
std::sort(events.begin(), events.end(), comp_start);
88+
//print(events);
89+
90+
int start = events[0][0];
91+
int end = 0;
92+
for(auto& e:events){
93+
end = max(end, e[1]);
94+
}
95+
96+
int result = 0;
97+
int i = 0;
98+
priority_queue<int, vector<int>, greater<int>> pq;
99+
100+
for (int day = start; day <= end; day++) {
101+
while (i<events.size() && events[i][0]==day) {
102+
pq.push(events[i][1]); //push the ending day
103+
i++;
104+
}
105+
//remove out-of-date event
106+
while(!pq.empty() && pq.top() < day) {
107+
pq.pop();
108+
}
109+
110+
//if there still has event, then choose current day.
111+
if (!pq.empty()){
112+
pq.pop();
113+
result++;
114+
}
115+
116+
//no more date need to process
117+
if (pq.empty() && i >= events.size()) break;
118+
}
119+
return result;
120+
}
121+
122+
int maxEvents_union_find(vector<vector<int>>& events) {
123+
std::sort(events.begin(), events.end(), comp_end);
124+
125+
126+
int end = events[events.size()-1][1];
127+
int start = end;
128+
for(auto& e:events){
129+
start = min(start, e[0]);
130+
}
131+
132+
vector<int> dict;
133+
for (int i=0; i<=end-start+1; i++){
134+
dict.push_back(i);
135+
}
136+
137+
int result = 0;
138+
139+
for(auto& e : events) {
140+
141+
int x = find(e[0]-start, dict);
142+
if ( x <= e[1]-start ){
143+
result++;
144+
dict[x] = find(x+1, dict);
145+
}
146+
}
147+
148+
return result;
149+
}
150+
};

0 commit comments

Comments
 (0)