-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeeting.cpp
58 lines (52 loc) · 1.45 KB
/
meeting.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
// You are given timings of n meetings in the form of (start[i], end[i]) where
// start[i] is the start time of meeting i and end[i] is the finish time of
// meeting i. Return the maximum number of meetings that can be accommodated in
// a single meeting room, when only one meeting can be held in the meeting room
// at a particular time.
//
// Note: The start time of one chosen meeting can't be equal to the end time of
// the other chosen meeting.
#include <bits/stdc++.h>
using namespace std;
struct meeting {
int pos;
int start;
int end;
};
class Solution {
public:
bool static comparator(struct meeting m1, struct meeting m2) {
if (m1.end < m2.end)
return true;
else if (m1.end > m2.end)
return false;
return m1.pos < m2.pos;
}
static int maxMeetings(int n, int start[], int end[]) {
struct meeting meet[n];
for (int i = 0; i < n; i++) {
meet[i].pos = i + 1;
meet[i].start = start[i];
meet[i].end = end[i];
}
sort(meet, meet + n, comparator);
vector<int> answer;
answer.push_back(meet[0].pos);
int limit = meet[0].end;
for (int i = 1; i < n; i++) {
if (meet[i].start > limit) {
answer.push_back(meet[i].pos);
limit = meet[i].end;
}
}
return answer.size();
}
};
int main() {
int n = 6;
int start[] = {1, 3, 0, 5, 8, 5};
int end[] = {2, 4, 5, 7, 9, 9};
int result = Solution::maxMeetings(n, start, end);
cout << result << endl;
return 0;
}