-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1229-meeting-scheduler.js
More file actions
49 lines (43 loc) · 1.41 KB
/
1229-meeting-scheduler.js
File metadata and controls
49 lines (43 loc) · 1.41 KB
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
/**
* 1229. Meeting Scheduler
* https://leetcode.com/problems/meeting-scheduler/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[][]} slots1
* @param {number[][]} slots2
* @param {number} duration
* @return {number[]}
*/
var minAvailableDuration = function(slots1, slots2, duration) {
slots1.sort((a, b) => a[0] - b[0]);
slots2.sort((a, b) => a[0] - b[0]);
let i = 0;
let j = 0;
while (i < slots1.length && j < slots2.length) {
const overlapStart = Math.max(slots1[i][0], slots2[j][0]);
const overlapEnd = Math.min(slots1[i][1], slots2[j][1]);
if (overlapEnd - overlapStart >= duration) {
return [overlapStart, overlapStart + duration];
}
if (slots1[i][1] < slots2[j][1]) {
i++;
} else {
j++;
}
}
return [];
};