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
.
Example 1:
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 8
Output: [60,68]
Example 2:
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 12
Output: []
Constraints:
1 <= slots1.length, slots2.length <= 10^4
slots1[i].length, slots2[i].length == 2
slots1[i][0] < slots1[i][1]
slots2[i][0] < slots2[i][1]
0 <= slots1[i][j], slots2[i][j] <= 10^9
1 <= duration <= 10^6
// Priority Queue
vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) { //time: O((m + n) * log(m + n)); space: O(m + n)
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
for (const auto& slot : slots1) {
if (slot[1] - slot[0] >= duration) pq.push({slot[0], slot[1]});
}
for (const auto& slot : slots2) {
if (slot[1] - slot[0] >= duration) pq.push({slot[0], slot[1]});
}
while (pq.size() > 1) {
int end = pq.top().second; pq.pop();
int start = pq.top().first;
if (end >= start + duration) return {start, start + duration};
}
return {};
}
// Sort + Two Pointers
vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) { // time: O(nlogn + mlogm); space: O(1)
sort(slots1.begin(), slots1.end());
sort(slots2.begin(), slots2.end());
int i = 0, j = 0;
while(i < slots1.size() && j < slots2.size()) {
int intersect_start = max(slots1[i][0], slots2[j][0]);
int intersect_end = min(slots1[i][1], slots2[j][1]);
if (intersect_start + duration <= intersect_end) return {intersect_start, intersect_start + duration};
else if (slots1[i][1] < slots2[j][1]) ++i;
else ++j;
}
return {};
}
Last updated
Was this helpful?