/
1229.meeting-scheduler.cpp
58 lines (55 loc) · 1.45 KB
/
1229.meeting-scheduler.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
using PII = pair<int, int>;
class Solution
{
static bool comp(PII &a, PII &b)
{
if (a.first != b.first)
{
return a.first < b.first;
}
return a.second < b.second;
}
public:
vector<int> minAvailableDuration(vector<vector<int>> &slots1,
vector<vector<int>> &slots2,
int duration)
{
// sweeping line method
// overlap: 1->2 begin time
// overlap: 2->1 end time
// if end - begin >= duration
// return {begin,end}
vector<PII> time;
for (auto &s : slots1)
{
time.push_back({s[0], 1});
time.push_back({s[1], -1});
}
for (auto &s : slots2)
{
time.push_back({s[0], 1});
time.push_back({s[1], -1});
}
sort(time.begin(), time.end(), comp);
int overlap = 0;
PII range;
for (auto &t : time)
{
int x = t.second;
overlap += x;
if (x == 1 && overlap == 2)
{
range.first = t.first;
}
else if (x == -1 && overlap == 1)
{
range.second = t.first;
if (range.second - range.first >= duration)
{
return {range.first, range.first + duration};
}
}
}
return {};
}
};