[LeetCode] Meeting Scheduler

1229. Meeting Scheduler

Given the availability time slots arrays slots1 and slots2 of two people and a meeting duration duration, return the earliest time slot that works for both of them and is of duration duration.

If there is no common time slot that satisfies the requirements, return an empty array.

The format of a time slot is an array of two elements [start, end] representing an inclusive time range from start to end.

It is guaranteed that no two availability slots of the same person intersect with each other. That is, for any two time slots [start1, end1] and [start2, end2] of the same person, either start1 > end2 or start2 > end1.

  • new solution update 2022.02.25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
vector<int> intersection(vector<int>& t1, vector<int>& t2) {
int start = max(t1[0], t2[0]);
int end = min(t1[1], t2[1]);
return {start, end};
}
public:
vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {
sort(slots1.begin(),slots1.end());
sort(slots2.begin(),slots2.end());
for(int i = 0, j = 0; i < slots1.size() and j < slots2.size();) {
vector<int> intersect = intersection(slots1[i], slots2[j]);
if(intersect[1] - intersect[0] >= duration) {
return {intersect[0], intersect[0] + duration};
}

if(slots1[i][0] < slots2[j][0] and slots1[i][1] < slots2[j][1]) i++;
else if(slots1[i][0] > slots2[j][0] and slots1[i][1] > slots2[j][1]) j++;
else if(slots1[i][0] < slots2[j][0] and slots2[i][1] < slots1[i][1]) j++;
else i++;
}
return {};
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
vector<int> minAvailableDuration(vector<vector<int>>& s1, vector<vector<int>>& s2, int duration) {
sort(begin(s1), end(s1));
sort(begin(s2), end(s2));
for(auto i = begin(s1), j = begin(s2); i != end(s1) && i != end(s2);) {
while (i != end(s1) && i->back() - i->front() < duration) i++;
while (j != end(s2) && j->back() - j->front() < duration) j++;
if(i == end(s1) || j == end(s2)) break;
if(max(i->front(), j->front()) + duration <= min(i->back(), j->back())) return {max(i->front(), j->front()), max(i->front(), j->front()) + duration};
if(i->back() > j->back()) j++;
else i++;
}

return {};
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/06/02/PS/LeetCode/meeting-scheduler/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.